【发布时间】:2020-10-26 03:42:43
【问题描述】:
我在 PHP 中有下一个代码:
$plain = 'some string to encode';
$key = '01234567891234567890123456789012';
$cipherSuite = 'aes-128-cbc';
$iv = null; // I have to use null, I know it's not safe
$result = @openssl_encrypt($plain, $cipherSuite, $key, null, $iv); // Suppress warning of an empty IV
dd($result); // result is 9VK02Mt8IaS+Bng8SbqhCVXUc5TteHKqt3y/EbaJZ1w=
我正在尝试在在线工具中进行相同的编码 - https://www.devglan.com/online-tools/aes-encryption-decryption。工具说密钥必须是 16 字节,所以我只使用一半的密钥 - 0123456789123456
它返回 与 PHP 完全相同的结果。请注意 IV 为空。
我需要在 JS 中使用 Crypto-js
进行相同的加密(而不是解密)const CryptoJS = require('crypto-js');
var key = CryptoJS.lib.WordArray.create('01234567891234567890123456789012');
var iv = CryptoJS.lib.WordArray.create('');
//var iv = null;
// var iv = CryptoJS.enc.Hex.parse("");
// var iv = CryptoJS.enc.Base64.parse('');
let cfg = {
mode: CryptoJS.mode.CBC,
keySize: 128,
iv: iv
};
const body = 'some string to encode';
const encryptedBody = CryptoJS.AES.encrypt(body, key, cfg).toString();
console.log( encryptedBody );
// result is VYCEPSx9nmb0FJGf1RiU/daL5nIk/qaJZU82jrlGQws=
https://jsfiddle.net/pj76d5ov/ 的类似示例
JS 中的结果与 PHP不同。有没有办法在没有 IV 的情况下使用 CryptoJS?
如果我使用 key 作为 string,CryptoJS 会根据我的 key 生成 IV,所以我必须使用 WordArray 输入。
然后我尝试将 iv 更改为一些值,但没有帮助。将 iv 设置为 false 或 null,或者根本不发送 iv 会导致错误。
【问题讨论】:
标签: javascript aes cryptojs