【发布时间】:2019-05-17 19:13:18
【问题描述】:
如何在 codeigniter 3 加密库中设置我的 IV?
我有下面的加密代码,它在 PHP 5.6 的 Codeigniter 2 中运行顺利,
function encrypt($data, $secret)
{
//Generate a key from a hash
$key = md5(utf8_encode($secret), true);
$data2 = utf8_encode($data);
$iv = utf8_encode("jvz8bUAx");
//Take first 8 bytes of $key and append them to the end of $key.
$key .= substr($key, 0, 8);
//Pad for PKCS7
$blockSize = mcrypt_get_block_size('tripledes', 'cbc');
//Encrypt data
$encData = mcrypt_encrypt('tripledes', $key, $data2, MCRYPT_MODE_CBC, $iv);
return urlencode(base64_encode($encData));
}
当我使用 PHP 7.1 升级到 CI 3 时,mcrypt 已被弃用。因此,我想在 CI 3 中使用加密 library 重新创建该函数,但我无法获得正确的加密字符串。
$this->load->library('encryption');
$key = md5(utf8_encode($secret), true);
$key .= substr($key, 0, 8);
$iv = utf8_encode("jvz8bUAx");
$amount = 1100;
$json = array(
'Amount' => $amount
);
$data = json_encode($json);
$params = array(
'driver' => 'mcrypt',
'cipher' => 'tripledes',
'mode' => 'cbc',
'key' => $key,
'hmac' => false
);
$ciphertext = $this->encryption->encrypt($data, $params);
$ciphertext = urlencode(base64_encode($ciphertext));
【问题讨论】:
-
请指出错误。
-
如何设置 IV
-
问题解决了吗?如果是这样,你能接受答案吗?
标签: php encryption codeigniter-3 mcrypt