我正在使用此函数在javascript中加密Cryptojs。
function encode(text, skey) {
var base64Iv = "0123456789abcdef";
var key = CryptoJS.enc.Base64.parse(skey);
var iv = CryptoJS.enc.Base64.parse(base64Iv);
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), key, {iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
var encryptedData = encrypted.toString();
return encryptedData;
}
并尝试使用php_mcrypt使用此函数解密它
function decrypt($crypt, $key, $type){
$enc = MCRYPT_RIJNDAEL_128;
$mode = MCRYPT_MODE_CBC;
$iv = "0123456789abcdef";
$crypt = base64_decode($crypt);
$padtext = mcrypt_decrypt($enc, base64_decode($key) , $crypt, $mode, $iv);
echo ("PADTEXT"+$padtext);
$pad = ord($padtext
{
strlen($padtext) - 1});
if ($pad > strlen($padtext)) return false;
if (strspn($padtext, $padtext{ strlen($padtext) - 1}, strlen($padtext) - $pad) != $pad)
{
$text = "Error";
}
$text = substr($padtext, 0, -1 * $pad);
echo ("TEXT "+$text);
return $text;
}
我正在使用相同的密钥而iv但是它没有正确地解密字符串的开头。
预期结果 00paygate | 201710270001 | 12345 | 16 | USD | INR | SALE | http://localhost/Response.php|http://localhost/Response.php|WEB
实际结果 00-ܗڀxRTWQ0001 | 12345 | 16 | USD | INR |销售| http://localhost/Response.php|http://localhost/Response.php|WEB