-1

I try to use AES in Java. I found a code snippet but output is wrong. There is a lot of EF BF BD in output. I could not find what is wrong with my code ?

public class AES
{
static String encryptionKey = "48C3B4286FF421A4A328E68AD9E542A4";
    static String clearText = "00000000000000000000000000000000";

    public static void main(String[] args) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
    {
        encr();
    }

    public static String toHexString(byte[] ba)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ba.length; i++)
        {
            sb.append(String.format("%02X ", ba[i]));
        }
        return sb.toString();
    }

    public static void encr() throws InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
    {
        //Security.addProvider(new com.sun.crypto.provider.SunJCE());
        SecretKeySpec secretKey = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");

        byte[] clearTextBytes = clearText.getBytes("UTF8");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherBytes = cipher.doFinal(clearTextBytes);

        System.out.print("enc1:  ");
        for (int i = 0; i < cipherBytes.length; i++)
        {
            System.out.print(cipherBytes[i]);
        }
        System.out.println("");

        String cipherText = new String(cipherBytes, "UTF8");
        System.out.println("enc2: " + cipherText);

        System.out.println("enc3: " + toHexString(cipherText.getBytes("UTF-8")));
    }
}

output is :

enc1:  51 72 -122 -57 -109 127 57 85 116 63 -89 -35 55 -72 37 -96 51 72 -122 -57 -109 127 57 85 116 63 -89 -35 55 -72 37 -96 -82 103 -117 -60 -102 -91 -51 55 -53 23 33 -82 -70 -14 74 41 
enc2: 3H�Ǔ9Ut?��7�%�3H�Ǔ9Ut?��7�%��g�Ě��7�!���
enc3: 33 48 EF BF BD C7 93 7F 39 55 74 3F EF BF BD EF BF BD 37 EF BF BD 25 EF BF BD 33 48 EF BF BD C7 93 7F 39 55 74 3F EF BF BD EF BF BD 37 EF BF BD 25 EF BF BD EF BF BD 67 EF BF BD C4 9A EF BF BD EF BF BD 37 EF BF BD 17 21 EF BF BD EF BF BD EF BF BD 

but it should be

33  48  86  c7  93  7f  39  55  74  3f  a7  dd  37  b8  25  a0
utarid
  • 1,642
  • 4
  • 24
  • 38
  • enc1 and enc2 outputs are correct, right ? Only issue seems to be in enc2 : toHexString function's sysout statement, right ? – Ajinkya Patil Apr 26 '16 at 15:44
  • 1
    What do you expect new String(cipherBytes, "UTF8"); to return when cipherBytes contains AES encrypted data and not UTF-8 encoded characters? – jarnbjo Apr 26 '16 at 15:47
  • It looks to me that you already have everything that you need. You just have to put it together: `toHexString(cipherBytes)` – Artjom B. Apr 26 '16 at 20:00
  • Possible duplicate of [IllegalBlockSizeException when trying to encrypt and decrypt a string with AES](http://stackoverflow.com/questions/30383736/illegalblocksizeexception-when-trying-to-encrypt-and-decrypt-a-string-with-aes) – Artjom B. Apr 26 '16 at 20:01
  • @ArtjomB. thanks. `toHexString(cipherBytes)` solved my problem. – utarid Apr 27 '16 at 07:59

1 Answers1

1

If you print toHexString(cipherText.getBytes("UTF-8")), the result is

33 48 86 C7 93 7F 39 55 74 3F A7 DD 37 B8 25 A0 33 48 86 C7 93 7F 39 55 74 3F A7 DD 37 B8 25 A0 AE 67 8B C4 9A A5 CD 37 CB 17 21 AE BA F2 4A 29

When you convert to byte[] to String, java drop invalid data base on the charset.

Beck Yang
  • 3,004
  • 2
  • 21
  • 26