我有一个PHP mcrypt模块的通用实现,它没有解密

时间:2010-06-17 03:38:05

标签: php mcrypt

class Crypt_Data {

    protected $_mcrypt=null;
    protected $_iv=null;
    protected $_key=null;

    public function __construct() {
            $this->_mcrypt = mcrypt_module_open('rijndael_256', '', 'cbc', '');
            $key_size = mcrypt_enc_get_key_size($this->_mcrypt);
            for($i=0;$i<$key_size;$i++) $test_key .= "0";
            $this->_iv = $test_key;
            $this->_key = $test_key;
            mcrypt_generic_init($this->_mcrypt,$this->_key,$this->_iv);
    }

    public function dataEncrypt($data) {
            return base64_encode(mcrypt_generic($this->_mcrypt, $data));
    }
    public function dataDecrypt($data) {
            return mdecrypt_generic($this->_mcrypt, base64_decode($data));
    }
}

$crypt = new Crypt_Data();
$string = "encrypt me";
$encrypted = $crypt->dataEncrypt($string);
echo $encrypted."<BR>";

$decrypted = $crypt->dataDecrypt($encrypted);
echo $decrypted."<BR>";

输出:

JJKfKxZckkqwfZ5QWeyVR + 3PkMQAsP0Gr1hWaygV20I =

qÌÌi_ÖZí(®`iÜ¥wÝÿô€Í6Ÿhf[%ër

不知道为什么这不起作用,我的一切似乎都很好..我试着用mcrypt_cbc()解密它;并且它正确地解密了..所以它与我的mdecrypt_generic有关...有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要运行mcrypt_generic_init以在mdecrypt_generic之前再次重置具有相同值的所有缓冲区。