Need to replace existing encrypt and decrypt functions that use mcrypt blowfish cbc mode with it's openssl equivalent.
It needed to be able to handle the older values so the methods need to stay compatible.
Have the decrypt working, and the encrypt is 'almost' ok, but not quite there.
This is the code I have:
$value = "myTextValue";
$key = 'c40f5b7ad3b7c787d400e923e461064b141fa878ce61cb0d1782593a5a2d842832c80fc2';
$enc = @encrypt_openssl($value, $key);
//$enc = @encrypt_mcrypt($value, $key);
$original_openssl = @decrypt_openssl($enc, $key);
$original_mcrypt = @decrypt_mcrypt($enc, $key);
echo $original_mcrypt."\n";
echo $original_openssl."\n";
function encrypt_openssl($string, $key) {
$iv_size = openssl_cipher_iv_length("BF-CBC");
$iv = openssl_random_pseudo_bytes($iv_size);
$enc = openssl_encrypt($string, "BF-CBC", pack('H*',$key), OPENSSL_RAW_DATA, $iv);
return base64_encode($iv.$enc);
}
function encrypt_mcrypt($string, $key) {
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$enc = mcrypt_encrypt(MCRYPT_BLOWFISH, pack('H*', $key), $string, MCRYPT_MODE_CBC, $iv);
return base64_encode($iv.$enc);
}
function decrypt_openssl($enc, $key) {
$iv_size = openssl_cipher_iv_length("BF-CBC");
$dec = base64_decode($enc);
$iv = substr($dec, 0, $iv_size);
$string = openssl_decrypt(substr($dec, $iv_size), "BF-CBC", pack('H*',$key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING | OPENSSL_DONT_ZERO_PAD_KEY, $iv);
return rtrim($string, "\x00");
}
function decrypt_mcrypt($enc, $key) {
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
$dec = base64_decode($enc);
$iv = substr($dec, 0, $iv_size);
$string = mcrypt_decrypt(MCRYPT_BLOWFISH, pack('H*', $key), substr($dec, $iv_size), MCRYPT_MODE_CBC, $iv);
return rtrim($string, "\x00");
}
When encrypting with openssl there is some extra binary data being added.
Not an encryption guru and only get half of it, this was as far as I got with the help of other stackoverflow posts and almighty google
EDIT
Following Topaco advice I came to the following code that now works:
function encrypt_openssl($string, $key) {
$string_padded = $string;
if (strlen($string_padded) % 8) {
$string_padded = str_pad($string_padded,
strlen($string_padded) + 8 - strlen($string_padded) % 8, "\0");
}
$iv_size = openssl_cipher_iv_length("BF-CBC");
$iv = openssl_random_pseudo_bytes($iv_size);
$enc = openssl_encrypt($string_padded, "BF-CBC", pack('H*',$key), OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
return base64_encode($iv.$enc);
}