我正在使用http://www-cs-students.stanford.edu/~tjw/jsbn/上的 JSBN 库。我特别想在 rsa.js 中进行 RSASetPublic() 调用,如下所示:
publicPem = RSASetPublic(N,E);
例如,当 N =“667”和 E =“327”时,我收到警报“对于 RSA 来说消息太长”。
功能是
function RSASetPublic(N,E) {
if(N != null && E != null && N.length > 0 && E.length > 0) {
this.n = parseBigInt(N,16);
this.e = parseInt(E,16);
}
else
alert("Invalid RSA public key");
}
它调用其他函数...
function parseBigInt(str,r) {
return new BigInteger(str,r);
}
function BigInteger(a,b,c) {
if(a != null)
if("number" == typeof a) this.fromNumber(a,b,c);
else if(b == null && "string" != typeof a) this.fromString(a,256);
else this.fromString(a,b);
}
BigInteger fromString() 函数是......
function bnpFromString(s,b) {
var k;
if(b == 16) k = 4;
else if(b == 8) k = 3;
else if(b == 256) k = 8; // byte array
else if(b == 2) k = 1;
else if(b == 32) k = 5;
else if(b == 4) k = 2;
else { this.fromRadix(s,b); return; }
this.t = 0;
this.s = 0;
....
然后,要使用生成的密钥,例如使用 RSAEncrypt(text),n 在文本被填充和加密时很重要......
function RSAEncrypt(text) {
var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
if(m == null) return null;
var c = this.doPublic(m);
if(c == null) return null;
var h = c.toString(16);
if((h.length & 1) == 0) return h; else return "0" + h;
}
function pkcs1pad2(s,n) {
if(n < s.length + 11) { // TODO: fix for utf-8
alert("Message too long for RSA");
return null;
}
var ba = new Array();
var i = s.length - 1;
while(i >= 0 && n > 0) {
var c = s.charCodeAt(i--);
if(c < 128) { // encode using utf-8
ba[--n] = c;
}
....
因此,我从各种来源收集了以下建议:
- N 是模数,两个素数的乘积 (N=pq),长度不超过 k 位
- p 和 q 是两个大素数?
- N用于指定keysize
- 典型的位长为 k = 1024, 2048, 3072, 4096,...
- 从 {2,4,8,16,32,256} 中为 parseBigInt 选择第二个参数
- e 是公共指数,一个小于和互质的数 (p-1)(q-1)
- 从 {3, 5, 17, 257, 65537} 中选择 e 的值
像http://asecuritysite.com/encryption/rsa这样的网站有助于对算法的高级理解,但我仍然不知道他如何转换为特定的参数分配,或者 N 和 E(相对于 n 和 e)明确意思是。那么N和E应该是什么?我该如何选择,这样消息才不会“对于 RSA 来说太长”?