我想将TEA加密(一个字节[])的结果转换为一个字符串,然后再将其转换为一个字节[]并检索相同的字节[]。在Java中,如何正确地将byte []转换为字符串为byte []?
//Encryption in the sending side
String stringToEncrypt = "blablabla"
byte[] encryptedDataSent = tea.encrypt(stringToEncrypt.getBytes());
String dataToSend = new BigInteger(encryptedDataSent).toString());
//Decryption side in the reception side
byte[] encryptedDataReceived = new BigInteger(dataToSend).toByteArray();
然而,当我试试这个:
System.out.println(new String(encryptedDataSent));
System.out.println(new String(encryptedDataReceived));
boolean equality = Arrays.equals(encryptedDataReceived,encryptedDataSent);
System.out.println("Are two byte arrays equal ? : " + equality);
输出是:
& H7" PAtj݄IZ`H-JK 012f
&h 7 “tPAtj݄ I Z`H-jK f
两个字节数组是否相等? :false
因此,看起来两个字节[]在打印时是相同的,但它们与我们看到的“假”不完全相同,这是我在执行解密后出现的问题那。
我还尝试发送String with new String(byte[])
但是当我们想将其转换回一个byte []它有同样的问题
我想有完全相同的字节[]在开始和转换后字节[] - >字符串 - >字节[]
您是否有解决方案或理解我在我的转换中出错?
查看这两个链接,它可能会有所帮助:[字符串到字节,反之亦然](http://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and - 反之亦然),[另一个链接](http://stackoverflow.com/questions/4318693/string-to-byte-and-vice-versa) –