我在我的 android 客户端和服务器(Servlet)中得到一个随机数。我在客户端和服务器中使用相同的 SecureRandom 算法(“SHA1PRNG”)。我的种子值对于两者都是相同的。但是我得到的输出数字在客户端和服务器中都是不同的。可能是什么原因?这是我的代码:
SecureRandom random = new SecureRandom();
try {
random.getInstance("SHA1PRNG");
} catch (Exception e) {
// ...
}
;
random.setSeed(1097327);
byte[] b1 = new byte[3];
random.nextBytes(b1);
long value = 0;
for (int i = 0; i < b1.length; i++) {
value += (b1[i] & 0xff) << (8 * i);
Toast.makeText(getApplicationContext(), Long.toString(value),
Toast.LENGTH_LONG).show();
}
最佳答案
SecureRandom 的 javadoc 说:
Many SecureRandom implementations are in the form of a pseudo-random number generator (PRNG), which means they use a deterministic algorithm to produce a pseudo-random sequence from a true random seed. Other implementations may produce true random numbers, and yet others may use a combination of both techniques.
也许该算法使用另一个随机源来生成随机数。这样做不会破坏 SecureRandom 类的约定。它甚至会更加满足它,因为安全随机生成器的目标是生成随机数,而不是可预测的数字序列。
关于java - SecureRandom 数字中的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9647066/