0

I am encrypting a string with AES in Java Netbeans using

     Key aesKey = new SecretKeySpec(key.getBytes(), "AES");

     Cipher cipher = Cipher.getInstance("AES");



     // encrypt the text

     cipher.init(Cipher.ENCRYPT_MODE, aesKey);

     byte[] encrypted = cipher.doFinal(text.getBytes());

     System.err.println(new String(encrypted)); 

The result I am getting are special characters. If I encrypt the text "1111" with a 128 bit key "Bar12345Bar12345" then I am getting the output as []3SU[][][]~a[][]`)

Is this valid? If not what should be the output then and how can i get the correct output?

mhlz
  • 3,497
  • 2
  • 23
  • 35
Gungun
  • 37
  • 7
  • 1
    You can't just display encrypted data as text and expect it to be readable. Encrypted data is in bytes, so you'll need to display the bytes in some readable way (such as hexadecimal). – Kayaman Mar 26 '15 at 09:05
  • 2
    You cannot turn arbitrary bytes into a `String`! See my answer [here](http://stackoverflow.com/a/16238710/2071828) on how to encode a `byte[]` to Base64. Also, DON'T SHOUT. – Boris the Spider Mar 26 '15 at 09:06
  • hey @Kayaman thanku.....now I get it. – Gungun Mar 26 '15 at 11:04
  • hello @BoristheSpider Thanku. Now I getting the correct cipher by encoding byte[] to Base64. – Gungun Mar 26 '15 at 11:06

0 Answers0