How can I encrypt an entire string with AES. The code that I have below only encrypts up to the first space recognized :(. How can I fix this? Thanks
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
cipher.init(Cipher.ENCRYPT_MODE, key);
String result = new String(cipher.doFinal(message.getBytes()));
System.out.println("Encrypted:" + result);
EDIT OMG I CANT BELIEVE THIS, HOW COULD I MISS THIS :( ITS BECAUSE MY SCANNER WAS TAKING next instead of nextLine... how embarrassing this bugged me all day and only now did i actually think about checking that. Problem solved :) Thanks everyone
I don't see anything wrong with your code except trying to print an arbitrary
byte[]
usingnew String(byte[])
. Try this on for size:Which prints:
I think we can all agree that those are two different byte arrays.