我正在将现有代码迁移到Python3,不幸的是解密显示IV必须为16个字节长的错误。
我尝试用sha1解密密钥,然后尝试解密。
我的php5.6代码是
HttpContext.Current.Server.UrlEncode(<Your URL>);
在我的python3文件中,就像
<?php
define('ENCR_ALGO', MCRYPT_RIJNDAEL_256);
define('ENCR_MODE', MCRYPT_MODE_CBC);
define('KEY',"This is test key");
function encryptData($plaintext){
$iv=generateIv();
$ciphertext = mcrypt_encrypt(ENCR_ALGO, getKey(), $plaintext, ENCR_MODE, $iv);
$ciphertext.=$iv;
return trim(base64_encode($ciphertext));
}
function decryptData($ciphertext){
$iv=getIvFromCiphertext($ciphertext);
$ciphertext=getActualCiphertext($ciphertext);
$plaintext = mcrypt_decrypt(ENCR_ALGO, getKey(), $ciphertext, ENCR_MODE, $iv);
return trim($plaintext);
}
function getIvFromCiphertext($encryptedData){
$encryptedData = base64_decode($encryptedData);
$cipherTextSize = strlen($encryptedData);
$ivStartIndex = $cipherTextSize-ivSize();
return substr($encryptedData, $ivStartIndex , ivSize());
}
function getActualCiphertext($encryptedData){
$encryptedData = base64_decode($encryptedData);
$cipherTextSize = strlen($encryptedData);
return substr($encryptedData, 0,$cipherTextSize-ivSize());
}
function ivSize(){
return mcrypt_get_iv_size(ENCR_ALGO, ENCR_MODE);
}
function keySize(){
return mcrypt_get_key_size(ENCR_ALGO, ENCR_MODE);
}
function generateIv(){
return mcrypt_create_iv(keySize(), MCRYPT_RAND );
}
function getKey(){
return substr(sha1(KEY), 0, keySize());
}
echo "<br/>";
echo encryptData("my pass");
echo decryptData("vuv6kZgweA2YqSU4vMOuYStrbwZayDYaL7UQ+JajFCVc2p4HW1o68OmIm2l3Rbi/IaCWtKD5m6an7LqnvwRYVA==");
?>
错误显示ValueError:IV必须为16个字节长
我期望的结果是“我的通行证”