I am trying to encrypt and decrypt a string using java Cipher with the AES algorithm. But somehow it does not want to encrypt the whole string. My goal is not to encrypt something particullary safely, I want that someone just cant read the String very easily (later stored in a file).
To encrypt, I use the following code:
Cipher cipher = Cipher.getInstance("AES");
byte[] input = plain.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(input);
return new String(encrypted);
And to decrypt, I use the following code:
Cipher cipher = Cipher.getInstance("AES");
byte[] input = encrypted.getBytes();
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(input);
return new String(decrypted);
To test the code mentioned above, I use the following parameters:
SecretKeySpec key = new SecretKeySpec(new byte[]{103,38,125,-67,-71,-23,-119,102,78,-3,-33,-23,-5,32,-112,-124}, "AES");
String plain = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur auctor ornare bibendum."
That test results in the encrypted and then again decrypted string:
�T���Ѩ�%���Kr sit amet, consectetur adipiscing elit. Curabitur auctor ornare bibendum.