JavaScript中的RSA解密

时间:2018-10-03 17:50:02

标签: javascript encryption rsa

我有一个使用Tom Wu http://www-cs-students.stanford.edu/~tjw/jsbn/ js库的简单代码,该库在客户端将原始数据加密成密文,然后在phpseclib服务器端解密。有没有办法在JavaScript中对加密的数据进行解密?这是我要加密的代码段:

var PublicKey="public_key_hex"
var RSAEncryption=new RSAKey()
RSAEncryption.setPublic(PublicKey, "10001")
var EncryptedData=RSAEncryption.encrypt("raw_data")

我还查看了http://www-cs-students.stanford.edu/~tjw/jsbn/rsa2.html的来源,但无法弄清楚为什么setPrivateEx()中需要6个值进行解密。也许有人可以帮忙。我也知道包括jsbn2.js和rsa2.js进行解密。这是rsa2.html代码段:

function do_decrypt() {
  do_status("Decrypting...");
  var before = new Date();
  var rsa = new RSAKey();
  var dr = document.rsatest;
  rsa.setPrivateEx(dr.n.value, dr.e.value, dr.d.value, dr.p.value, dr.q.value, dr.dmp1.value, dr.dmq1.value, dr.coeff.value);
if(document.rsatest.ciphertext.value.length == 0) {
do_status("No Ciphertext - encrypt something first");
return;
}
var res = rsa.decrypt(document.rsatest.ciphertext.value);
var after = new Date();
if(res == null) {
document.rsatest.decrypted.value = "*** Invalid Ciphertext ***";
do_status("Decryption failed");
}
else {
document.rsatest.decrypted.value = res;
do_status("Decryption Time: " + (after - before) + "ms");
}
}

0 个答案:

没有答案