0
我在JavaScript下面的代码为RSA FPGA实现http://www-cs-students.stanford.edu/~tjw/jsbn/:蟒RSA FPGA实现与PKCS1
// Return the PKCS#1 RSA encryption of "text" as an even-length hex string
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;
}
// PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
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;
}
else if((c > 127) && (c < 2048)) {
ba[--n] = (c & 63) | 128;
ba[--n] = (c >> 6) | 192;
}
else {
ba[--n] = (c & 63) | 128;
ba[--n] = ((c >> 6) & 63) | 128;
ba[--n] = (c >> 12) | 224;
}
}
ba[--n] = 0;
var rng = new SecureRandom();
var x = new Array();
while(n > 2) { // random non-zero pad
x[0] = 0;
while(x[0] == 0) rng.nextBytes(x);
ba[--n] = x[0];
}
ba[--n] = 2;
ba[--n] = 0;
return new BigInteger(ba);
}
在上面的代码段,它似乎是pkcs1pad2功能被用于与一些随机字节填充该消息(也许像0 | 2 | random | 0)在消息前面。我使用Python的rsa包(http://stuvel.eu/rsa)模仿javascript的结果,我是一个新手到python世界,并没有想法将JavaScript算法代码传递给python代码。
任何帮助,将不胜感激。
Jiee
你的问题是兼容?你需要了解PKCS#1填充吗?找到一个可以做到的Python实现?将JS代码翻译成Python? – 2010-04-02 05:35:08
感谢您的评论,Eli,并为这个令人困惑的问题感到抱歉。 我需要将JS代码翻译成Python以获得类似的JS代码结果。我现在正在研究这个RSA包(http://stuvel.eu/rsa),但是它不支持PKCS1。所以对于基于Python包的一些改进代码来说最好。 – user307016 2010-04-02 05:54:05