Decrypt AES-256-CFB in PHP with openssl instead mcrypt

独自空忆成欢 提交于 2021-02-08 08:59:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48924727/decrypt-aes-256-cfb-in-php-with-openssl-instead-mcrypt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!