drqj8605 2013-12-29 16:46
浏览 100

如何使用伪造在javascript中使用给定的私钥解密rsa-cipher

I trie to encrypt text in php and decrypt with javscript using RSA. In php i use phpseclib. For the client-site i've tried forge by digitalbazaar.

According to the documentation, they create a private and a public key in one object and start the magic.

 /*** Generating 1024-bit key-pair */  
 keys = forge.pki.rsa.generateKeyPair(1024);  

 /*** public key encryption */    
 var ciphertext = keys.publicKey.encrypt("Secret");     

 /*** private key decryption */  
 var plaintext  = keys.privateKey.decrypt(ciphertext); 

Is there a method to use a given private key as a string to decrypt messages in forge?

  • 写回答

1条回答 默认 最新

  • douluogu8713 2013-12-30 09:25
    关注

    You can convert PKCS#8 PEM encoded private keys (i.e. key strings staring with -----BEGIN RSA PRIVATE KEY-----)

    var pem = readPEMKey(...); // read the string
    var privKey = forge.pki.privateKeyFromPem(pem);
    var pubKey = forge.pki.rsa.setPublicKey(privKey.n, privKey.e);
    
    /*** public key encryption */    
    var ciphertext = pubKey.encrypt("Secret");     
    /*** private key decryption */  
    var plaintext  = pkey.decrypt(ciphertext); 
    

    It is also possible (and a bit more complex) to create a private key from scratch with the key parameters:

    var privKey = forge.pki.setPrivateKey(n, e, d, p, q, dP, dQ, qInv);
    

    where :

    • n: key modulus
    • e: private exponent
    • p: the first modulus prime factor
    • q: the second modulus prime factor
    • d: public exponent
    • dP, dQ, qInv: the chinese remainder theorem parameters

    All these params are supposed to be BigInteger created with the forge.util.createBuffer function.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100