tags:

views:

26

answers:

1
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>";

output:

JJKfKxZckkqwfZ5QWeyVR+3PkMQAsP0Gr1hWaygV20I=

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

No idea why this isn't working, everything seems to be fine on my end.. i tried decrypting it with mcrypt_cbc(); and it decrypted it properly.. so it has something to do with my mdecrypt_generic.. any ideas?

A: 

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

Emil Vikström