2012-05-12 117 views
2

我正在使用以下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一起工作。

+1

你居然不知道解密的输出正确与否 - 你打印对象的地址,而不是内容:

检查类。比较数组的内容。 – Mat

回答

5

你看到的是数组的toString()方法的结果。这不是字节数组的内容。使用java.util.Arrays.toString(array)来显示阵列的内容。

[B是类型(字节数组),而1b10d42是数组的hashCode。

+0

谢谢:)它的工作。我会接受你的。我仍然有一个问题,你认为我可以在没有任何问题的Android上运行此代码?如果我将图像转换为字节数组,然后加密,接收,解密并再次转换成图像,会不会有问题? – Adroidist

+0

我从来没有在Android上做过任何事情,但如果支持API,我不明白为什么它不起作用。如果图像很大,请避免将整个字节数组加载到内存中以对其进行加密。通过块读取它,并将块发送到CipherOutputStream。 –

+0

如果我不知道我将要收到的图像文件,我该怎么办?我面临着同样的错误,因为我手动设置字节数组的大小,例如byte [] array = new byte [60000],其中接收的图像应该在60 kb左右。 – Adroidist

6

但是我的输出是:

Plain Text : [[email protected] 
Encrypted Text : [[email protected] 
Decrypted Text : [[email protected] 

那是因为你打印出一个字节数组上调用toString()的结果。除了参考身份的建议之外,这不会给你带来任何有用的信息。

您应该只是将明文数据与字节的解密数据字节进行比较(您可以使用Arrays.equals(byte[], byte[])执行此操作),或者如果您确实想要显示内容,请输出十六进制或base64表示形式。 [Arrays.toString(byte\[\])][2]会给你a表示,但十六进制可能会更容易阅读。第三方库中有大量的十六进制格式化类,或者您可以在堆栈溢出中找到一种方法。

+0

谢谢乔恩是的,你是完全正确的,你可以帮我解决我上面发布的问题吗? – Adroidist

+0

@Aroidist:目前还不清楚你的意思是“如果我不知道图像文件” - 这是没有道理的。如果你在谈论* size *,你应该基本上把它当作流 - 把它看成一个块,对它进行加密,读下一个块,直到你用完数据。同上解密。 –

2

我知道它太晚了,但我分享我的答案,在这里我用你的代码进行了一些修改。试试checker类来加密和解密字节数组。

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