在PHP 7上使用openssl_decrypt使用CryptoJS加密到Decypt将始终返回false

时间:2018-03-25 17:13:41

标签: php encryption openssl aes cryptojs

我需要在我的前面(离子)加密UUID以将其发送到后端。

我使用CryptoJS,这是我加密UUID的代码。

const UUID = 'ABCD1234';
const privateKey = 'f38d09938ead31a57eca34d2a0df1c44';   
const salt = CryptoJS.lib.WordArray.random(16);
const iv = CryptoJS.lib.WordArray.random(16);
const key = CryptoJS.PBKDF2(privateKey, salt, {
  hasher: CryptoJS.algo.SHA512,
  keySize: 4,
  iterations: 1
});
const encripted = CryptoJS.AES.encrypt(UUID, key, {
  iv
});
const cp = {};
cp.mid = CryptoJS.enc.Base64.stringify(encripted.ciphertext);
cp.iv = CryptoJS.enc.Hex.stringify(iv);
cp.s = CryptoJS.enc.Hex.stringify(salt);

所以我将标题中的CP发送到服务器(Slim Framework 3)。

我这样收到:

$mid = $request->getHeader('HEADER-MID')[0];
$iv = $request->getHeader('HEADER-IV')[0];
$salt = $request->getHeader('HEADER-S')[0];

//Example Data Received
//mid: losv78Amn1zpgRe5/4hYFA==
//iv: 2d198339d178c053c37e36b7d03e8a3b
//salt: fb07dd1f61d72148bc1423af8cd1f295

$ct = base64_decode($mid);
$salt = hex2bin($salt);
$iv = hex2bin($iv);

$key = hash_pbkdf2("sha512", $privateKey, $salt, 1, 32);

$decrypted = openssl_decrypt($ct, 'AES-256-CBC', hex2bin($key), OPENSSL_RAW_DATA, $iv);

密钥在后面和前面正确生成。

但是openssl_decrypt函数返回false并且使用openssl_error_string()得到这个:

error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

我不知道我做错了什么。

提前致谢。

0 个答案:

没有答案
相关问题