gpt4 book ai didi

java - getByte() 为不同的字符串返回相同的字节[]

转载 作者:行者123 更新时间:2023-12-02 03:35:36 26 4
gpt4 key购买 nike

我正在生成一个 session key ,每次运行程序时该 key 都会发生变化。但是当我将其转换为字节数组时,每次运行程序时生成的字节数组都是相同的。 IT应该有所不同吧?这是我的代码

Key key;
SecureRandom rand = new SecureRandom();
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(rand);
generator.init(256);
key = generator.generateKey();
String key1=key.toString();
byte[] genratesessionKey1 = key1.getBytes();
System.out.println("SESSION KEY IS(Byte format) "+genratesessionKey1.toString());

然后我还使用了一根虚拟字符串。然后我生成了它的Byte[]。然后我更改了该字符串中的值并再次生成了它的 Byte[] 。它仍然返回相同的结果。

String test2="yadav";
String key1=key.toString();
byte[] genratesessionKey1 = key1.getBytes();
byte[] g2=test.getBytes("UTF-8");
byte[] g3=test.getBytes();
System.out.println("Session key in String "+key1);
System.out.println("Testing Byte Format "+g2);
System.out.println("Testing Byte Format 2 "+g3);

为什么会发生。任何建议将不胜感激

First Execution

Second Execution

最佳答案

您不能依赖对字节数组调用 toString() 来检查其内容。返回的值不会告诉您字节是什么。

如果您确实想检查字节数组的内容并查看它是否更改,请改用 Arrays.toString(byteArray) 。然后您应该能够验证字节数组确实发生了变化。

关于java - getByte() 为不同的字符串返回相同的字节[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com