node js 加密改成php方法实现,有大佬能帮忙看看吗?

新手上路,请多包涵
const crypto = require("crypto");

class AESCipher {
    constructor(key) {
        const hash = crypto.createHash('sha256');
        hash.update(key);
        this.key = hash.digest();
    }

    decrypt(encrypt) {
        const encryptBuffer = Buffer.from(encrypt, 'base64');
        const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, encryptBuffer.slice(0, 16));
        let decrypted = decipher.update(encryptBuffer.slice(16).toString('hex'), 'hex', 'utf8');
        decrypted += decipher.final('utf8');
        return decrypted;
    }
}

encrypt = "P37w+VZImNgPEO1RBhJ6RtKl7n6zymIbEG1pReEzghk="
cipher = new AESCipher("test key")
console.log(cipher.decrypt(encrypt))
// hello world
阅读 2k
2 个回答
<?php

function decrypt($encrypt, $key)
{
    $binKey = hash('sha256', $key, true);

    $binEncrypt = base64_decode($encrypt);
    $binIV      = substr($binEncrypt, 0, 16);

    $data = openssl_decrypt(substr($binEncrypt, 16), 'aes-256-cbc', $binKey, OPENSSL_RAW_DATA, $binIV);

    return $data;
}

$encrypt = "P37w+VZImNgPEO1RBhJ6RtKl7n6zymIbEG1pReEzghk=";
$text    = decrypt($encrypt, 'test key');

echo('[encrypt] '.$encrypt.PHP_EOL);
echo('[   text] '.$text.PHP_EOL);

[encrypt] P37w+VZImNgPEO1RBhJ6RtKl7n6zymIbEG1pReEzghk=
[ text] hello world

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题