2014-11-08 17 views
2

我想通过蒸汽认证here。我被难住的地方是使用公钥的RSA密码加密。可以从here收到编码参数。我使用的加密密码的代码是:试图通过蒸汽认证:难倒RSA ecnryption(js到python)

import base64 
from Crypto.PublicKey import RSA 
mod = long(publickey_mod, 16) 
exp = long(publickey_exp, 16) 
rsa = RSA.construct((mod, exp)) 
encrypted_password = rsa.encrypt(password, '')[0] 
encrypted_password = base64.b64encode(encrypted_password) 

这里的RSA.js:

var RSAPublicKey = function($modulus_hex, $encryptionExponent_hex) { 
    this.modulus = new BigInteger($modulus_hex, 16); 
    this.encryptionExponent = new BigInteger($encryptionExponent_hex, 16); 
} 

var Base64 = { 
    base64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=", 
    encode: function($input) { 
     if (!$input) { 
      return false; 
     } 
     var $output = ""; 
     var $chr1, $chr2, $chr3; 
     var $enc1, $enc2, $enc3, $enc4; 
     var $i = 0; 
     do { 
      $chr1 = $input.charCodeAt($i++); 
      $chr2 = $input.charCodeAt($i++); 
      $chr3 = $input.charCodeAt($i++); 
      $enc1 = $chr1 >> 2; 
      $enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4); 
      $enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6); 
      $enc4 = $chr3 & 63; 
      if (isNaN($chr2)) $enc3 = $enc4 = 64; 
      else if (isNaN($chr3)) $enc4 = 64; 
      $output += this.base64.charAt($enc1) + this.base64.charAt($enc2) + this.base64.charAt($enc3) + this.base64.charAt($enc4); 
     } while ($i < $input.length); 
     return $output; 
    }, 
    decode: function($input) { 
     if(!$input) return false; 
     $input = $input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 
     var $output = ""; 
     var $enc1, $enc2, $enc3, $enc4; 
     var $i = 0; 
     do { 
      $enc1 = this.base64.indexOf($input.charAt($i++)); 
      $enc2 = this.base64.indexOf($input.charAt($i++)); 
      $enc3 = this.base64.indexOf($input.charAt($i++)); 
      $enc4 = this.base64.indexOf($input.charAt($i++)); 
      $output += String.fromCharCode(($enc1 << 2) | ($enc2 >> 4)); 
      if ($enc3 != 64) $output += String.fromCharCode((($enc2 & 15) << 4) | ($enc3 >> 2)); 
      if ($enc4 != 64) $output += String.fromCharCode((($enc3 & 3) << 6) | $enc4); 
     } while ($i < $input.length); 
     return $output; 
    } 
}; 

var Hex = { 
    hex: "abcdef", 
    encode: function($input) { 
     if(!$input) return false; 
     var $output = ""; 
     var $k; 
     var $i = 0; 
     do { 
      $k = $input.charCodeAt($i++); 
      $output += this.hex.charAt(($k >> 4) &0xf) + this.hex.charAt($k & 0xf); 
     } while ($i < $input.length); 
     return $output; 
    }, 
    decode: function($input) { 
     if(!$input) return false; 
     $input = $input.replace(/[^0-9abcdef]/g, ""); 
     var $output = ""; 
     var $i = 0; 
     do { 
      $output += String.fromCharCode(((this.hex.indexOf($input.charAt($i++)) << 4) & 0xf0) | (this.hex.indexOf($input.charAt($i++)) & 0xf)); 
     } while ($i < $input.length); 
     return $output; 
    } 
}; 

var RSA = { 

    getPublicKey: function($modulus_hex, $exponent_hex) { 
     return new RSAPublicKey($modulus_hex, $exponent_hex); 
    }, 

    encrypt: function($data, $pubkey) { 
     if (!$pubkey) return false; 
     $data = this.pkcs1pad2($data,($pubkey.modulus.bitLength()+7)>>3); 
     if(!$data) return false; 
     $data = $data.modPowInt($pubkey.encryptionExponent, $pubkey.modulus); 
     if(!$data) return false; 
     $data = $data.toString(16); 
     return Base64.encode(Hex.decode($data)); 
    }, 

    pkcs1pad2: function($data, $keysize) { 
     if($keysize < $data.length + 11) 
      return null; 
     var $buffer = []; 
     var $i = $data.length - 1; 
     while($i >= 0 && $keysize > 0) 
      $buffer[--$keysize] = $data.charCodeAt($i--); 
     $buffer[--$keysize] = 0; 
     while($keysize > 2) 
      $buffer[--$keysize] = Math.floor(Math.random()*254) + 1; 
     $buffer[--$keysize] = 2; 
     $buffer[--$keysize] = 0; 
     return new BigInteger($buffer); 
    } 
} 

在这里,从蒸汽网站的JS代码:

var pubKey = RSA.getPublicKey(results.publickey_mod, results.publickey_exp); 
var encryptedPassword = RSA.encrypt(password, pubKey); 

我想我必须使用Crypto.PKCS1_v1_5而不是Crypto.PublicKey.RSA.encrypt,因为该函数完全对应一个名称“pkcs1pad2”,但我不知道我在哪里放置publickey_exp以及如何导入现有密钥

+0

你确定你不违反服务条款?如果您禁用了您的帐户,解决您面临的任何技术挑战对您都无能为力。 – user2357112 2014-11-08 21:43:26

+0

@ user2357112,不,我不确定,但我在用户协议规则中没有看到类似的内容。 – mrDoctorWho 2014-11-09 07:37:07

回答

1

当你怀疑,你应该使用Crypto.Cipher.PKCS1_v1_5如下面的代码片段:

import base64 
from Crypto.PublicKey import RSA 
from Crypto.Cipher import PKCS1_v1_5 
mod = long(publickey_mod, 16) 
exp = long(publickey_exp, 16) 
rsa_key = RSA.construct((mod, exp)) 
rsa = PKCS115_Cipher(rsa_key) 
encrypted_password = rsa.encrypt(password) 
encrypted_password = base64.b64encode(encrypted_password) 
+0

谢谢!这样的作品,但有一点修改: 'rsa_key = RSA.construct((MOD,EXP)) RSA = PKCS1_v1_5.PKCS115_Cipher(rsa_key) encrypted_pa​​ssword = base64.b64encode(rsa.encrypt(密码))' – mrDoctorWho 2014-11-09 07:37:58

+0

@mrDoctorWho:谢谢,我修好了。 – 2014-11-09 13:51:58