微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将PHP加密转换为NodeJS

如何解决将PHP加密转换为NodeJS

我目前正在API上实现加密。我必须以用PHP编写的特定脚本对它进行加密和解密的相同方法来实现。

当我将结果输出到base64时,这一切正常。 PHP解密方法针对它运行,并且工作正常。问题是,由于某些要求,我不需要以base64格式输出,而是以二进制格式输出,但是当我从Nodejs端加密为二进制格式时,结果与将PHP加密为二进制格式时应该相同的结果不同。 / p>

PHP加密:

function vd_encrypt($plaintext,$password) {
$method = "AES-256-CBC";
$key = hash('sha256',$password,true);
$iv = openssl_random_pseudo_bytes(16);

$ciphertext = openssl_encrypt($plaintext,$method,$key,OPENSSL_RAW_DATA,$iv);
$hash = hash_hmac('sha256',$ciphertext,true);

return $iv . $hash . $ciphertext;

}

JavaScript加密:

import crypto from 'crypto';

export function encrypt (plain_text: string): string {
    const encryptionMethod = 'AES-256-CBC';
    const iv = crypto.randomBytes(IV_LENGTH);
    const key = crypto.createHash("sha256").update(secret).digest();

    var encryptor = crypto.createCipheriv(encryptionMethod,key,iv);

    const result = iv + encryptor.update(plain_text,'utf8','binary') + encryptor.final('binary');

    return result;
}

解决方法

我已经稍微更新了您的代码以接受iv参数。您可以使用与以前相同的方式(例如openssl_random_pseudo_bytes)来生成它。

出于演示目的,我将使用固定的IV,以便可以显示相同的结果。

PHP

function vd_encrypt($plaintext,$password,$iv) {
    $method = "AES-256-CBC";
    $key = hash('sha256',true);
    $ciphertext = openssl_encrypt($plaintext,$method,$key,OPENSSL_RAW_DATA,$iv);
    $hash = hash_hmac('sha256',$ciphertext,true);
    return $iv . $hash . $ciphertext;
}

// Replace with below code in production
// $iv = openssl_random_pseudo_bytes(16);
$iv = base64_decode("eQMrc61Gt8qRejRjhJOkVw==");
$result = vd_encrypt("He was a man take him for all in all,I shall not look upon his like again","password",$iv);

echo "Result (base64): " . base64_encode($result) . "\n";

Node.js

import crypto from 'crypto';

export function encrypt (plaintext: string,password: string,iv: string): string {
    const encryptionMethod = 'AES-256-CBC';
    const key = crypto.createHash("sha256").update(password).digest();
    const encryptor = crypto.createCipheriv(encryptionMethod,key,iv);

    const encryptedData = Buffer.concat([encryptor.update(plaintext,'utf8'),encryptor.final()]);
    const hash = crypto.createHmac("sha256",key).update(encryptedData).digest();

    return Buffer.concat([iv,hash,encryptedData]);
}

// Replace with below code in production 
//const iv = crypto.randomBytes(16);
const iv = Buffer.from("eQMrc61Gt8qRejRjhJOkVw==","base64");
const result = encrypt("He was a man take him for all in all,iv);
console.log("Result (base64):",result.toString("base64"));

在这种情况下,结果将如下所示:

PHP:

Result (base64): eQMrc61Gt8qRejRjhJOkVxsqZTqUjSUnaL46yZDLGGK5+o7WKLyIiG4UKj0ST93Wi7UlaAyTFIjpIs0C893SFsnHeuVshG+6EJF99GrLSUCMFJG3J1pJnmxF4Pu8ZCbN7Ounp0BjhJKIpu9yQn6uEYylJLXWpzNw+aCwsnIV1h0=

Node.js:

Result (base64): eQMrc61Gt8qRejRjhJOkVxsqZTqUjSUnaL46yZDLGGK5+o7WKLyIiG4UKj0ST93Wi7UlaAyTFIjpIs0C893SFsnHeuVshG+6EJF99GrLSUCMFJG3J1pJnmxF4Pu8ZCbN7Ounp0BjhJKIpu9yQn6uEYylJLXWpzNw+aCwsnIV1h0=

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。