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);
message = scanner.next();try...message = scanner.nextLine();