duanjiao7440 2019-06-11 15:46
浏览 92

如何在javascript中密封消息以与AES256中的PHP openssl_open进行通信

I have a PHP application which can communicate with other PHP applications. Theses apps exchange encrypted data.
A central app knows all public keys of other apps.
And other apps only know the public key of the central app.
The data are encrypted thanks to the openssl_seal PHP method.
And there are decrypted thanks to the openssl_open PHP method.
I use the "AES256" cypher method and generate an "iv" element.
In PHP no problem at all, everything is for the best in the best of all possible worlds …

But, now, I would like to do the same with an Ionic app (based on cordova and Angular 5).

I tried to use JSEncrypt and CryptoJS, but PHP dos not explain how openssl_seal works and I have some trouble to export my PHP code into Javascript one …

function encrypt($message)
{
    $envKeys = [];
    $encMessage = '';
    $pubKey = "-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----
";
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES256'));
    if (openssl_seal($message, $encMessage, $envKeys, [$pubKey], 'AES256', $iv) === false) {
        throw new Exception("Erreur lors du cryptage du message !");
    }
    return [base64_encode($encMessage), base64_encode($envKeys[0]), base64_encode($iv)];
}
function decrypt($messageCrypte, $key, $iv)
{
    $privateKey = "-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
    ";
    $message = '';
    openssl_open(base64_decode($messageCrypte), $message, base64_decode($key), $privateKey, 'AES256', base64_decode($iv));
    return $message;
}
$crypted = encrypt('Test');
$embed = "<script>var encrypted = '$crypted[0]'; var envelope = '$crypted[1]'; var iv = '$crypted[2]'; </script>";
<html>
<head>
    <script type="text/javascript" src="crypto-js.js"></script>
    <script type="text/javascript" src="jsencrypt.js"></script>
    <?php echo $embed; ?>
    <script>
        var private_key = `-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
        `;

        var crypt = new JSEncrypt();
        crypt.setPrivateKey(private_key);
        var theKey = CryptoJS.enc.Utf8.parse(crypt.decrypt(envelope));
        console.log('The key', theKey);
        console.log('ciphertext', CryptoJS.enc.Base64.parse(encrypted));
        var decrypted = CryptoJS.AES.decrypt(
            {ciphertext: CryptoJS.enc.Base64.parse(encrypted)},
            theKey,
            {iv: CryptoJS.enc.Base64.parse(iv)}
        );
        console.log('Decrypted', decrypted.toString());
    </script>
</head>
<body>

</body>
</html>

I don't have the original message "Test" … Only empty string … I would like to be able to make an openssl_seal (like the PHP one) method in JS and an openssl_open (like the PHP one) using CryptoJS and JSEncrypt. I think it's possible, maybe someone already did that …

Edit

If someone is interested by this problem, I maybe found a lead : http://geekswithblogs.net/Strenium/archive/2013/01/27/converting-phprsquos-ldquoopenssl_sealrdquo-and-ldquoopenssl_openrdquo-into-.net.aspx
It is for .NET but it's a start …

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 c语言,请帮蒟蒻看一个题
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)