0

i have a quick question that will hopefully be a quick fix.

I currently have a client/server program which takes input from the client, encodes this using UTF-8 into a byte Array, Encrypts the Array using AES, send it to the server, and reverses the process for decryption.

The only issue is, if the message has a space from the user, it will only display the word before the first space as the decrypted message.

E.G:

Input: Hello i am Tom

Output: Hello

But if my Input is all one word, it decrypts perfectly

Input: HelloIamTom

Output: HelloIamTom

I cannot find information on this anywhere!

Any help would be appreciated,

CLIENT CODE

 System.out.println("Please type a message to be encrypted:");
       message = scanner.next();


     //create iv array
     byte[] iv = toByteArray("a11f001ed2dec0de6e6f6e73656e7365");

     Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
     SecretKey key = new SecretKeySpec(decryptedKey, "AES");
     IvParameterSpec ivParSpec = new IvParameterSpec(iv);
     aesCipher.init(Cipher.ENCRYPT_MODE, key, ivParSpec);
     byte[] encryptedMessage = aesCipher.doFinal(message.getBytes("UTF-8"));


     dos.writeInt(encryptedMessage.length);
     dos.write(encryptedMessage);

SERVER CODE

  int length = dis.readInt();//recieve length of byte array for incoming message
        byte[] encryptedMessage = new byte[length];//create a byte array to the length recieved
        dis.readFully(encryptedMessage);//fill the byte array with incoming data

        //decrypt using AES

        Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");//create a cipher with correct parameters
        IvParameterSpec ivParaSpec = new IvParameterSpec(iv);//create IvParameter spec using IV provided in assignment brief

        aesCipher.init(Cipher.DECRYPT_MODE,key,ivParaSpec);//initialise the Cipher in DECRYPT mode
        byte[] decryptedMessage = aesCipher.doFinal(encryptedMessage);//create decryptedMessage and put in byte array

        String decMess = new String(decryptedMessage,"UTF-8");

        System.out.println("User ID:");
        System.out.println(uid);

        System.out.println("Decrypted Message:");
        System.out.println(decMess);
1
  • Instead of message = scanner.next(); try... message = scanner.nextLine(); Commented Feb 16, 2016 at 21:26

2 Answers 2

2

See Scanner:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

and Scanner#next():

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

If you want to scan the hole line, your modified client code is:

System.out.println("Please type a message to be encrypted:");
message = scanner.nextLine();
Sign up to request clarification or add additional context in comments.

Comments

0

You are doing message = scanner.next();. Instead of this you should be doing message = scanner.nextLine(); to take the complete line as input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.