用PKCS1实现python RSA

用PKCS1实现python RSA,python,rsa,jsbn,Python,Rsa,Jsbn,我在javascript中获得了以下用于RSA实现的代码: 在上面的代码片段中,pkcs1pad2函数似乎用于在消息前面填充一些随机字节,比如0 | 2 | random | 0 我使用PythonRSA包来模拟javascript结果,我是python世界的新手,不知道如何将javascript算法代码转换成python代码 任何帮助都将不胜感激。 Jiee我知道有点晚了,但几天后我将发布我的Python RSA包的新版本。该版本将包括PKCS1 v1.5填充,因此它应该与您的JavaScri

我在javascript中获得了以下用于RSA实现的代码:

在上面的代码片段中,pkcs1pad2函数似乎用于在消息前面填充一些随机字节,比如0 | 2 | random | 0

我使用PythonRSA包来模拟javascript结果,我是python世界的新手,不知道如何将javascript算法代码转换成python代码

任何帮助都将不胜感激。
Jiee

我知道有点晚了,但几天后我将发布我的Python RSA包的新版本。该版本将包括PKCS1 v1.5填充,因此它应该与您的JavaScript代码兼容-

你的问题是什么?您需要了解PKCS1填充吗?找到一个实现它的Python实现吗?将JS代码翻译成Python?谢谢你的评论,Eli,很抱歉这个令人困惑的问题。我需要将JS代码翻译成Python,以获得与JS代码类似的结果。我现在正在开发这个RSA包,但是,它不支持PKCS1。因此,最好是使用一些基于Python包的改进代码。
 // 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);
}