我不知道我做错了什么。我只需要能够加密和解密而不会收到奇怪的字符或警告。它说我应该使用长度为 16 的 IV,并且我使用的长度为 9,但“0123456789abcdef”是 16 个字符。
警告:mcrypt_generic_init() [function.mcrypt-generic-init]:IV 大小不正确;提供长度:9,需要:第 10 行 /home/mcondiff/public_html/projects/enc/enc.php 中的 16
见http://www.teamconcept.org/projects/enc/enc.php
我迷失了,困惑,有点头晕目眩。我从这里去吗?我必须使用这种加密并让它为一个项目工作。
<?php
class enc
{
function encrypt($str, $key) {
$key = $this->hex2bin($key);
$td = mcrypt_module_open("rijndael-128", "", "cbc", "fedcba9876543210");
mcrypt_generic_init($td, $key, CIPHER_IV);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return bin2hex($encrypted);
}
function decrypt($code, $key) {
$key = $this->hex2bin($key);
$code = $this->hex2bin($code);
$td = mcrypt_module_open("rijndael-128", "", "cbc", "fedcba9876543210");
mcrypt_generic_init($td, $key, CIPHER_IV);
$decrypted = mdecrypt_generic($td, $code);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return utf8_encode(trim($decrypted));
}
function hex2bin($hexdata) {
$bindata = "";
for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
}
$theEncryption = new enc();
$user = "John Doe";
$email = "john@example.com";
$user = $theEncryption->encrypt($user, "0123456789abcdef");
$email = $theEncryption->encrypt($email, "0123456789abcdef");
echo 'User: '.$user;
echo 'Email: '.$email;
?>
有人可以指出我正确的方向或指出我做错了什么吗?
谢谢
麦克风