douduan5073 2012-12-10 19:34
浏览 279

在Javascript中加密,使用PKCS#1解密PHP

I have trying to do encryption/decryption in Javascript/PHP using PKCS#1. I have the following variables:

e: Public exponent (for encryption)
d: Private exponent (for decryption)
n: modulus

I am using this javascript library to decrypt: http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.js like this:

    var rsa = new RSAKey();

    rsa.setPublic(n, e);

    var cipherText = rsa.encrypt(plainText);

To decrypt in PHP I use PHPSec library:

Question1: How do I convert from d and n to a private key to be used in phpseclib?

Question2: How do I convert the output from the Javascript code to a form that can be used with phpseclib?

  • 写回答

2条回答 默认 最新

  • dougui1977 2012-12-13 17:08
    关注

    For javascript / PHP interoperability check out this:

    http://area51.phpbb.com/phpBB/viewtopic.php?f=84&t=33024&start=0

    It does PKCS#1 v2.1 RSAES-OAEP (which is supposed to offer better security than PKCS#1 v2.1 RSASSA-PKCS1-v1_5).

    $rsa->loadKey( array( 'e' => new Math_BigInteger('...', 256), 'n' => new Math_BigInteger('...', 256) ) );

    If that doesn't work let me know.

    Alternatively, you could try this:

    http://www.frostjedi.com/phpbb3/viewtopic.php?p=331621#p331621

    (see the second code block)

    For converting the js output to a format PHP could use... the proof of concept I linked to is passing the output to char2hex():

    function char2hex(source)
    {
       var hex = "";
       for (var i = 0; i < source.length; i+=1)
       {
          temp = source[i].toString(16);
          switch (temp.length)
          {
             case 1:
                temp = "0" + temp;
                break;
             case 0:
               temp = "00";
          }
          hex+= temp;
       }
       return hex;
    } 
    

    Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作