2

The function below correctly decrypt the data in php5

function decrypt_mcrypt($key, $str) {
  $str = base64_decode($str);
  $iv = substr($str, 0, 16);
  $str = substr($str, 16);
  return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CFB, $iv);
}

I tried to use openssl instead of mcrypt (in php7), but got garbage on the output.

function decrypt_openssl($key, $str) {
  $str = base64_decode($str);
  $iv = substr($str, 0, 16);
  $str = substr($str, 16);
  return openssl_decrypt($str, 'AES-256-CFB', $key, OPENSSL_RAW_DATA, $iv);
}

What could be the problem?

vaal
  • 153
  • 3
  • 8

1 Answers1

0

openssl_decrypt uses PKCS#5 padding, where as mcrypt pads messages with zeros.

Try using the OPENSSL_ZERO_PADDING option when using openssl_decrypt as per the docs.

Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
  • tried openssl_decrypt($str, 'AES-256-CFB', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv); and openssl_decrypt($str, 'AES-256-CFB', $key, OPENSSL_ZERO_PADDING, $iv); - did not help – vaal Feb 22 '18 at 13:29