doushi1900 2019-06-14 13:10
浏览 520
已采纳

如何在Javascript中编码的PHP RSA OAEP中进行解码

So, I have some encryption/decryption issues …

I encrypt data in javascript thanks to node-forge and I try to decrypt it into PHP thanks to openssl_private_decrypt.

On the PHP side, I use the «OPENSSL_PKCS1_OAEP_PADDING» padding. So, on the javascript side I tried to configure forge to encrypt data with RSA-OAEP.

And when I try to decsypt the message on the PHP side, I have these errors :

error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error error:04065072:rsa routines:rsa_ossl_private_decrypt:padding check failed

I've tried to configure the encryption with sha1 message digest and sha1 for mgf1 option. I've also tried without any option (if I remember, forge use SHA256 by default). But there is nothing to do, I always have the same error …

javascript

const pubkey = `-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----
`;
const privkey = `-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
`;
let publicKey = forge.pki.publicKeyFromPem(pubkey);
let key = forge.random.getBytesSync(32);
let encKey = publicKey.encrypt(key, 'RSA-OAEP', {
        md: forge.md.sha1.create(),
        mgf1: {
          md: forge.md.sha1.create()
        }
      });
let b64Key = encodeURIComponent(btoa(enckey));

Next, I send the key in url with the "xcem" param, thanks to HttpClient. And I receive it in PHP.

php

$privKey = "";
$b64Key = urldecode($_GET['xcem']);
$encKey = base64_decode($b64Key);
$key = null;
if (!openssl_private_decrypt($encKey, $key, file_get_contents('/keys/openssl_private.key'), OPENSSL_PKCS1_OAEP_PADDING)) 
{
    $errorssl = [];
    while ($error = openssl_error_string()) {
        $errorssl[] = $error;
    }
    throw new Exception("Erreur lors du décryptage du message ! " . json_encode($errorssl));
}

When I send Data between 2 PHP servers, there is no problem …
But I can't make it work between JS and PHP … I need some help ^^

  • 写回答

1条回答 默认 最新

  • doubinchou4219 2019-06-14 15:25
    关注

    Sooo …
    After some tests, and headaches I found the problem … In my original code (not the one here) I sent the key, the init vector, the signature and the encrypted data …
    But in my PHP, I tried to decode the signature … I didn't send data in the right order …
    My bad …

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况