CryptoJS AES 加密(密钥大小为 128 / 8)等同于 PHP
CryptoJS AES encrypt (with key size 128 / 8) equivalent in PHP
我是 PHP 的新手,我试图在 PHP 中实现等效的 CryptoJS AES 加密。我看到了这个 post 但不幸的是我没能达到同样的效果。我在 PHP 代码中得到了不同的输出作为加密字符串。
我哪里错了?
Javascript代码
const customerObject = {
CustomerName: "test",
EmailID: "tester@test.com",
Street: "Test",
City: "London",
Country: "United Kingdom",
ZipCode: "XXX XXX",
};
const token = "8056483646328123";
const key = CryptoJS.enc.Utf8.parse(token);
const iv = CryptoJS.enc.Utf8.parse(token);
const returnEncrypted = CryptoJS.AES.encrypt(
CryptoJS.enc.Utf8.parse(customerObject),
key,
{
iv: iv,
keySize: 128 / 8,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
}
);
PHP代码
<?php
$customer = [
'CustomerName' => "test",
'EmailID' => "tester@test.com",
'Street' => "Test",
'City' => "London",
'Country' => "United Kingdom",
'ZipCode' => "XXX XXX",
];
$plaintext = json_encode($customer);
$method = 'AES-128-CBC';
$key = hex2bin("8056483646328123");
$iv = hex2bin("8056483646328123");
$ciphertext = openssl_encrypt(
$plaintext,
$method,
$key,
OPENSSL_RAW_DATA,
$iv
);
$ciphertext = base64_encode($ciphertext);
echo $ciphertext;
?>
正如 @Topaco 在评论中指出的那样,key 和 IV 不能进行十六进制解码,即删除两个 hex2bin ().
更正后的代码如下。
<?php
$customer = [
'CustomerName' => "test",
'EmailID' => "tester@test.com",
'Street' => "Test",
'City' => "London",
'Country' => "United Kingdom",
'ZipCode' => "XXX XXX",
];
$plaintext = json_encode($customer);
$method = 'AES-128-CBC';
$key = "8056483646328123";
$iv = "8056483646328123";
$ciphertext = openssl_encrypt(
$plaintext,
$method,
$key,
OPENSSL_RAW_DATA,
$iv
);
$ciphertext = base64_encode($ciphertext);
echo $ciphertext;
?>
我是 PHP 的新手,我试图在 PHP 中实现等效的 CryptoJS AES 加密。我看到了这个 post 但不幸的是我没能达到同样的效果。我在 PHP 代码中得到了不同的输出作为加密字符串。
我哪里错了?
Javascript代码
const customerObject = {
CustomerName: "test",
EmailID: "tester@test.com",
Street: "Test",
City: "London",
Country: "United Kingdom",
ZipCode: "XXX XXX",
};
const token = "8056483646328123";
const key = CryptoJS.enc.Utf8.parse(token);
const iv = CryptoJS.enc.Utf8.parse(token);
const returnEncrypted = CryptoJS.AES.encrypt(
CryptoJS.enc.Utf8.parse(customerObject),
key,
{
iv: iv,
keySize: 128 / 8,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
}
);
PHP代码
<?php
$customer = [
'CustomerName' => "test",
'EmailID' => "tester@test.com",
'Street' => "Test",
'City' => "London",
'Country' => "United Kingdom",
'ZipCode' => "XXX XXX",
];
$plaintext = json_encode($customer);
$method = 'AES-128-CBC';
$key = hex2bin("8056483646328123");
$iv = hex2bin("8056483646328123");
$ciphertext = openssl_encrypt(
$plaintext,
$method,
$key,
OPENSSL_RAW_DATA,
$iv
);
$ciphertext = base64_encode($ciphertext);
echo $ciphertext;
?>
正如 @Topaco 在评论中指出的那样,key 和 IV 不能进行十六进制解码,即删除两个 hex2bin ().
更正后的代码如下。
<?php
$customer = [
'CustomerName' => "test",
'EmailID' => "tester@test.com",
'Street' => "Test",
'City' => "London",
'Country' => "United Kingdom",
'ZipCode' => "XXX XXX",
];
$plaintext = json_encode($customer);
$method = 'AES-128-CBC';
$key = "8056483646328123";
$iv = "8056483646328123";
$ciphertext = openssl_encrypt(
$plaintext,
$method,
$key,
OPENSSL_RAW_DATA,
$iv
);
$ciphertext = base64_encode($ciphertext);
echo $ciphertext;
?>