非对称 RSA PRNG 实现未按预期工作
Asymmetric RSA PRNG Implementation Not Working as Expected
我发布了一个问题 here asking if it was possible to create a PRNG in which an asymmetric private key could advance the PRNG while the public key could only reverse the PRNG. DannyNiu suggested an approach using RSA keys, for which I am working on a proof of concept 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");
}
交换了变量 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
我发布了一个问题 here asking if it was possible to create a PRNG in which an asymmetric private key could advance the PRNG while the public key could only reverse the PRNG. DannyNiu suggested an approach using RSA keys, for which I am working on a proof of concept 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");
}
交换了变量 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