Encryption of Strings works, encryption of byte[] array type does not work

橙三吉。 提交于 2019-12-18 10:58:27

问题


I am using the following LINK for encryption and tried it with Strings and it worked. However, since I am dealing with images, I needed the encryption/decryption process to happen with byte arrays. So I modified the code in that 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 the checker class is:

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 output is:

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

So the decrypted text is not the same as plain text. What should I do to fix this knowing that I tried the example in the original link and it worked with Strings?


回答1:


What you're seeing is the result of the array's toString() method. It's not the content of the byte array. Use java.util.Arrays.toString(array) to display the content of the arrays.

[B is the type (array of bytes), and 1b10d42 is the hashCode of the array.




回答2:


However my output is:

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

That's because you're printing out the result of calling toString() on a byte array. That's not going to show you anything useful, other than a suggestion of reference identity.

You should either just compare the plaintext data with the decrypted data byte for byte (which you can do with Arrays.equals(byte[], byte[])), or if you really want to display the contents, print out the hex or base64 representation. [Arrays.toString(byte\[\])][2] will give you a representation, but hex would probably be easier to read. There are loads of hex formatting classes in third-party libraries, or you could find a method on Stack Overflow.




回答3:


I know it's too late but I'm sharing my answer. Here I have used your code with some modification. Try below checker class to encrypt and decrypt a byte array.

Checker class:

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));
    }
}


来源:https://stackoverflow.com/questions/10562908/encryption-of-strings-works-encryption-of-byte-array-type-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!