phpseclib/jsbn:PHP中用公钥加密,jsbn中用私钥解密 [英] phpseclib/jsbn: encrypt with public key in PHP, decrypt with private key in jsbn

查看:162
本文介绍了phpseclib/jsbn:PHP中用公钥加密,jsbn中用私钥解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 jsbn 成功生成公钥/私钥对客户端.使用这些密钥加密和解密客户端是可以的.我还可以将该公钥传输到服务器并通过 PHP 将其存储在 MySQL 中.

I can successfully generate a public/private keypair using jsbn on the client side. Encrypting and decrypting client side with these keys is fine. I can also transmit that public key to the server and store it in MySQL via PHP.

我无法使用 jsbn 公钥在 PHP 中加密某些内容,然后使用 jsbn 私钥在客户端对其进行解密.

I cannot encrypt something in PHP using the jsbn public key, and then decrypt it client side using the jsbn private key.

// attempting to encrypt in PHP using the jsbn public key.
// (this public key came from jsbn client side)
$jsbn_public_key = '763989d1f75a779dae752ac236b011e85f9496bb414d72f5e89bf44274a942277fab2d4f5c58a57634d4000eecc8009d2efaeff17aa4a0efae2c4d41f3423be88be043628c6bac86f97deaadf23231793e6fa02550fb2ca65b2600e074205d23338e28ab3c5e92265e6bd7995c173085e3dc042e59ef464c5ed058c3ad863911';
$rsa = new Crypt_RSA();
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1); // tried various options here.
$public_key = array(
    'n' => new Math_BigInteger($jsbn_public_key, 16),
    'e' => new Math_BigInteger('65537', 10), // same value as jsbn
);
$rsa->loadKey($public_key,CRYPT_RSA_PUBLIC_FORMAT_RAW);  // tried various options here.
$ciphertext = $rsa->encrypt($value);
$ciphertext_web_safe = bin2hex($ciphertext); // I think this is where the problem is!
// $ciphertext_web_safe looks something like this:
// 1b02bee0422028accba74f37e6e6974125fd16bdf83a72b8d6462e140ee6e85adae869fddc5e83635aaf90bc6074a3128890eeadf9537c33ebdfd665e16a3f1b617fa2fa5454f469e84f86d77ffcbc234dfc8a32291fbc84df61a0098c97fd90bb10204f68e783d9996678cd7853f3cbd932a4a067cb7f4f9eb62ca0542964f6
// which is exactly the same length as an encrypted string generated by jsbn

这里是客户端加密:

var my_rsa = {
    ....
    e: "65537",
    public_key: '763989d1f75a779dae752ac236b011e85f9496bb414d72f5e89bf44274a942277fab2d4f5c58a57634d4000eecc8009d2efaeff17aa4a0efae2c4d41f3423be88be043628c6bac86f97deaadf23231793e6fa02550fb2ca65b2600e074205d23338e28ab3c5e92265e6bd7995c173085e3dc042e59ef464c5ed058c3ad863911',
    encrypt: function(value){
        var rsakey = new RSAKey();
        rsakey.setPublic(this.public_key, this.e);
        return rsakey.encrypt(value);
    },
    decrypt: function(ciphertext){
        var rsakey = new RSAKey();
        rsakey.setPrivateEx(this.public_key, this.e, this.private_key.d, this.private_key.p, this.private_key.q, this.private_key.dmp1, this.private_key.dmq1, this.private_key.coeff);
        return rsakey.decrypt(ciphertext);
    },
    ....
};

当我尝试在 JavaScript 中解密 '$ciphertext_web_safe' 值时,我只是得到一个空字符串.控制台日志等中没有错误.

When I try to decrypt the '$ciphertext_web_safe' value in JavaScript I simply get an empty string. No errors in console log etc..

PHP 中使用的bin2hex"有问题吗?在将加密的二进制数据转换为十六进制之前,是否应该应用某种填充等?

Is there a problem with the "bin2hex" used in PHP? Should there be some sort of padding etc.. applied before converting the encrypted binary data to hex?

干杯,戴夫

推荐答案

我的问题是 javascript 中的e".

My problem was "e" in javascript.

我将65537"更改为010001",现在我可以使用公钥和 PHP 加密服务器端.呼!

I changed "65537" to "010001" and now I can encrypt server side using the public key and PHP. Phew!

这篇关于phpseclib/jsbn:PHP中用公钥加密,jsbn中用私钥解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆