I have looked at this entry and tested some of the recommendations: recieving error Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize
I have a very small pdf file (141.06 kb) that was created from a webpage using wkhtmltopdf and encrypted using this function:
public function encrypt($data) {
if ($data !== '') {
$iv = mcrypt_create_iv($this->iv_size, MCRYPT_DEV_URANDOM);
$plaintext_utf8 = utf8_encode($data);
return base64_encode($iv) . base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, Configure::read('Cryptable.key'), $plaintext_utf8, MCRYPT_MODE_CBC, $iv));
} else {
return '';
}
}
As I mentioned above, I tested some of the recommendations on a SO post and found this:
iv_size = 16
length of iv = 18
Any other larger document encrypted with the same function decrypts properly so what I'm trying to figure out is how to handle very small files. Could it be that when calculating the iv on small amounts of data the result can be unpredictable?
This is the decrypt function:
public function decrypt($data, $data2 = null) {
if (is_object($data)) {
unset($data);
$data = $data2;
}
if ($data != '') {
$iv = base64_decode(substr($data, 0, strlen(base64_encode(mcrypt_create_iv($this->iv_size, MCRYPT_DEV_URANDOM)))));
$data = base64_decode(substr($data, strlen(base64_encode(mcrypt_create_iv($this->iv_size, MCRYPT_DEV_URANDOM)))));
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, Configure::read('Cryptable.key'), $data, MCRYPT_MODE_CBC, $iv));
} else {
return '';
}
}
IV size is calculated like this:
$this->iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
Before I forget, I'm using CakePHP 2.6.4