我想从字节数组创建一个字符串,但它给了我一些随机值。字节数组是加密的,所以我不确定我是否正确解密。随机值看起来像 - [B @ 1uy3798。每次它给不同的随机值。我该如何解决这个问题?String.valueOf()给出随机值
public class MainActivity {
public static void main(String[] args) {
Key publicKey = null;
Key privateKey = null;
byte[] encoded;
byte[] text = new byte[0];
try {
text = "This is my secret message".getBytes();
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, publicKey);
encoded = c.doFinal(text);
c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privateKey);
text = c.doFinal(encoded);
} catch (Exception e) {
System.out.println("Exception encountered. Exception is " + e.getMessage());
}
System.out.println(String.valueOf(text)); //get random values here
}
}
它看起来像你在字节数组上调用'toString()'。 – SLaks
你应该报告你所遇到的任何异常,而不是默默地忽略它们。也许你错过了一个简单的错误? –