String encryption works, byte type encryption [] does not work

I use LINK for encryption and tried it with Strings and it worked. However, since I'm dealing with images, I need the encryption / decryption process to work with byte arrays. Therefore, I changed the code in this link to the following:

public class AESencrp {

 private static final String ALGO = "AES";
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

public static byte[] encrypt(byte[] Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data);
    //String encryptedValue = new BASE64Encoder().encode(encVal);
    return encVal;
}

public static byte[] decrypt(byte[] encryptedData) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);

    byte[] decValue = c.doFinal(encryptedData);
    return decValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;

}

and verification class:

public class Checker {

public static void main(String[] args) throws Exception {

   byte[] array = new byte[]{127,-128,0};
    byte[] arrayEnc = AESencrp.encrypt(array);
    byte[] arrayDec = AESencrp.decrypt(arrayEnc);

    System.out.println("Plain Text : " + array);
    System.out.println("Encrypted Text : " + arrayEnc);
    System.out.println("Decrypted Text : " + arrayDec);
}
}

However, my conclusion:

Plain Text : [B@1b10d42
Encrypted Text : [B@dd87b2
Decrypted Text : [B@1f7d134

therefore, the decrypted text is not plain text. What I have to do to fix this knowledge is that I tried the example in the source link and it worked with strings.

+3
source share
3 answers

, , toString(). . java.util.Arrays.toString(array) .

[B - ( ), 1b10d42 - - .

+6

:

Plain Text : [B@1b10d42
Encrypted Text : [B@dd87b2
Decrypted Text : [B@1f7d134

, toString() . , .

( Arrays.equals(byte[], byte[])), , hex base64. [Arrays.toString(byte\[\])][2] , hex, , . .

+6

, , , . .

:

public class Checker {

public static void main(String[] args) throws Exception {

byte[] array = "going to encrypt".getBytes();
byte[] arrayEnc = AESencrp.encrypt(array);
byte[] arrayDec = AESencrp.decrypt(arrayEnc);

System.out.println("Plain Text : " + array);
System.out.println("Encrypted Text : " + arrayEnc);
System.out.println("Decrypted Text : " + new String(arrayDec));
}
}
+2

All Articles