我正在使用以下LINK进行加密,并使用字符串尝试了它,并且它工作正常。但是,由于我正在处理图像,因此我需要使用字节数组进行加密/解密处理。所以我修改了代码链接到以下几点:字符串的加密工作,byte []数组类型的加密不起作用
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;
}
和校验器类:
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);
}
}
但是我的输出是:
Plain Text : [[email protected]
Encrypted Text : [[email protected]
Decrypted Text : [[email protected]
所以解密的文本不是纯文本。我应该怎么做才能解决这个问题,因为我知道我在原始链接中试过这个例子,并且它和Strings一起工作。
你居然不知道解密的输出正确与否 - 你打印对象的地址,而不是内容:
检查类。比较数组的内容。 – Mat