nodejs解密Openssl通过PHP进行了加密的加密字符串

发布于 2025-02-08 04:37:15 字数 1797 浏览 0 评论 0原文

我有2个PHP函数来加密和解密消息/令牌,因此我更愿意尝试完成相同的解密函数,而不是加密功能,而是在node.js中进行加密函数。

$this->encryptMethod = 'aes-256-ctr';
$this->encryptKey = hex2bin('KEY123');

    public function token_encrypt($message, $encode = false)
    {
        $nonceSize = openssl_cipher_iv_length($this->encryptMethod);
        $nonce = openssl_random_pseudo_bytes($nonceSize);

        $ciphertext = openssl_encrypt(
            $message,
            $this->encryptMethod,
            $this->encryptKey,
            OPENSSL_RAW_DATA,
            $nonce
        );

        if ($encode) {
            return base64_encode($nonce.$ciphertext);
        }
        return $nonce.$ciphertext;
    }

    public function token_decrypt($message, $encoded = false)
    {
        if ($encoded) {
            $message = base64_decode($message, true);
            if ($message === false) {
                return false;
            }
        }

        $nonceSize = openssl_cipher_iv_length($this->encryptMethod);
        $nonce = mb_substr($message, 0, $nonceSize, '8bit');
        $ciphertext = mb_substr($message, $nonceSize, null, '8bit');

        $plaintext = openssl_decrypt(
            $ciphertext,
            $this->encryptMethod,
            $this->encryptKey,
            OPENSSL_RAW_DATA,
            $nonce
        );

        return $plaintext;
    }

我能够在node.js中获得这么远,而不会完全丢失

const hex2bin = s => s.match(/../g).map(c => String.fromCharCode(parseInt(c, 16))).join``;

function token_decrypt(message) {
    var encryptionKey = Buffer.from(hex2bin("KEY123")).toString('base64');
    var encryptionMethod = "aes-256-ctr";
    var encrypted = Buffer.from(message, 'base64').toString();
    
    const nonceSize = 16;
    
    return false;
}

I have 2 PHP functions to encrypt and decrypt messages/tokens and I am more-so trying to accomplish the same decrypt function rather then the encrypt function but in Node.JS.

$this->encryptMethod = 'aes-256-ctr';
$this->encryptKey = hex2bin('KEY123');

    public function token_encrypt($message, $encode = false)
    {
        $nonceSize = openssl_cipher_iv_length($this->encryptMethod);
        $nonce = openssl_random_pseudo_bytes($nonceSize);

        $ciphertext = openssl_encrypt(
            $message,
            $this->encryptMethod,
            $this->encryptKey,
            OPENSSL_RAW_DATA,
            $nonce
        );

        if ($encode) {
            return base64_encode($nonce.$ciphertext);
        }
        return $nonce.$ciphertext;
    }

    public function token_decrypt($message, $encoded = false)
    {
        if ($encoded) {
            $message = base64_decode($message, true);
            if ($message === false) {
                return false;
            }
        }

        $nonceSize = openssl_cipher_iv_length($this->encryptMethod);
        $nonce = mb_substr($message, 0, $nonceSize, '8bit');
        $ciphertext = mb_substr($message, $nonceSize, null, '8bit');

        $plaintext = openssl_decrypt(
            $ciphertext,
            $this->encryptMethod,
            $this->encryptKey,
            OPENSSL_RAW_DATA,
            $nonce
        );

        return $plaintext;
    }

I was able to get this far in Node.JS without getting completely lost

const hex2bin = s => s.match(/../g).map(c => String.fromCharCode(parseInt(c, 16))).join``;

function token_decrypt(message) {
    var encryptionKey = Buffer.from(hex2bin("KEY123")).toString('base64');
    var encryptionMethod = "aes-256-ctr";
    var encrypted = Buffer.from(message, 'base64').toString();
    
    const nonceSize = 16;
    
    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文