java 字符串加密有效,byte[] 数组类型加密无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10562908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encryption of Strings works, encryption of byte[] array type does not work
提问by Adroidist
I am using the following LINKfor 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:
我正在使用以下LINK进行加密,并使用字符串进行了尝试,并且成功了。但是,由于我正在处理图像,因此我需要对字节数组进行加密/解密过程。所以我将该链接中的代码修改为以下内容:
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?
所以解密后的文本和纯文本是不一样的。知道我尝试了原始链接中的示例并且它与字符串一起工作,我应该怎么做才能解决这个问题?
回答by Jon Skeet
However my output is:
Plain Text : [B@1b10d42 Encrypted Text : [B@dd87b2 Decrypted Text : [B@1f7d134
但是我的输出是:
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.
那是因为您要打印出调用toString()
字节数组的结果。除了参考身份的建议之外,这不会向您展示任何有用的东西。
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 arepresentation, 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.
您应该只将明文数据与解密的数据字节逐个字节进行比较(您可以使用Arrays.equals(byte[], byte[])
),或者如果您真的想显示内容,请打印出十六进制或 base64 表示。[Arrays.toString(byte\[\])][2]
会给你一个表示,但十六进制可能更容易阅读。第三方库中有大量的十六进制格式类,或者您可以在 Stack Overflow 上找到一个方法。
回答by JB Nizet
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.
您看到的是数组的 toString() 方法的结果。它不是字节数组的内容。使用java.util.Arrays.toString(array)
显示阵列的内容。
[B
is the type (array of bytes), and 1b10d42
is the hashCode of the array.
[B
是类型(字节数组),1b10d42
是数组的 hashCode。
回答by Arpana
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));
}
}