0
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Ýÿ ô0€Í6Ÿhf[%ër

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

4

1 回答 1

0

You need to run mcrypt_generic_init to reset all buffers with the same values again before mdecrypt_generic.

于 2010-06-17T04:31:40.480 回答