我发布了一个问题here,询问是否有可能创建PRNG,在该PRNG中,非对称私钥可以使PRNG前进,而公钥只能使PRNG反向。 DannyNiu建议使用RSA密钥的方法,为此我正在研究概念证明here。使用此方法前进和后退PRNG之后,我期望PRNG的开始和结束状态是相同的,但是在我的实现中,它们并不相同。我在做什么错了?
// requires BigInteger.min.js: https://github.com/peterolson/BigInteger.js/
// Using 256-bit RSA keys for fast demo only
// Keys generated using script found here: // https://en.wikipedia.org/wiki/RSA_(cryptosystem)
var rsaPrivateKey = bigInt("24776132865927824498491212731968501748100480067637351152890073639930475656193");
var rsaPublicKey = bigInt("74211765065553557319818035137797975277750578756934646327508787135523363995803");
var rsaModulus = 65537;
// Initial state for the PRNG
// Cannot be 0 or 1
var state = bigInt.randBetween(2, rsaModulus-1);
print("0: " + state.toString());
// Advance the PRNG 5x using the private key
for (var i = 1; i < 6; i++) {
state = state.modPow(rsaPrivateKey, rsaModulus);
print(i + ": " + state.toString());
}
// Reverse the PRNG 5x using the public key
for (var i = 4; i >= 0; i--) {
state = state.modPow(rsaPublicKey, rsaModulus);
print(i + ": " + state.toString());
}
function print(str){
document.body.insertAdjacentHTML("beforeend", str + "<br>\r\n");
}
答案 0 :(得分:0)
变量rsaModulus
和rsaPublic
被交换。下面的更正代码似乎可以满足需要。
// requires BigInteger.min.js: https://github.com/peterolson/BigInteger.js/
// Using 256-bit RSA keys for fast demo only
// Keys generated using script found here: // https://en.wikipedia.org/wiki/RSA_(cryptosystem)
var bigInt = require("big-integer");
var rsaPrivateKey = bigInt("24776132865927824498491212731968501748100480067637351152890073639930475656193");
var rsaModulus = bigInt("74211765065553557319818035137797975277750578756934646327508787135523363995803");
var rsaPublicKey = 65537;
// Initial state for the PRNG
// Cannot be 0 or 1
var state = bigInt.randBetween(2, rsaModulus-1);
print("0: " + state.toString());
// Advance the PRNG 5x using the private key
for (var i = 1; i < 6; i++) {
state = state.modPow(rsaPrivateKey, rsaModulus);
print(i + ": " + state.toString());
}
// Reverse the PRNG 5x using the public key
for (var i = 4; i >= 0; i--) {
state = state.modPow(rsaPublicKey, rsaModulus);
print(i + ": " + state.toString());
}
function print(str){
console.log(str);
// document.body.insertAdjacentHTML("beforeend", str + "<br>\r\n");
}
产生输出(在Node.js中,我没有在浏览器中进行测试):
0: 3089889900716331070935914834855269746958619454008171918802934456826278805869
1: 29294271228731490548225349341396330182559853938616577145390725955076346471738
2: 26514699849481326763107659510545065888424675390354763649355047607623510843283
3: 43142677973722044074820378370391067407717125958268872808246349065241317072133
4: 36861272951268123086050613298534678401193075212756094490295892940224420130435
5: 6529151801265964225108415545430092089926156264721909235696182044061658877417
4: 36861272951268123086050613298534678401193075212756094490295892940224420130435
3: 43142677973722044074820378370391067407717125958268872808246349065241317072133
2: 26514699849481326763107659510545065888424675390354763649355047607623510843283
1: 29294271228731490548225349341396330182559853938616577145390725955076346471738
0: 3089889900716331070935914834855269746958619454008171918802934456826278805869