Advertisement
myapit

php 7.3 mcrypt replacement code

May 15th, 2019
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. private function encrypt( $data, $key ) {
  2.     $salt = 'cH!swe!retReGu7W6bEDRup7usuDUh9THeD2CHeGE*ewr4n39=E@rAsp7c-Ph@pH';
  3.     $iv_size = openssl_cipher_iv_length( "AES-256-CBC-HMAC-SHA256" );
  4.     $hash = hash( 'sha256', $salt . $key . $salt );
  5.     $iv = substr( $hash, strlen( $hash ) - $iv_size );
  6.     $key = substr( $hash, 0, 32 );
  7.     $encrypted = base64_encode( openssl_encrypt( $data, "AES-256-CBC-HMAC-SHA256", $key, OPENSSL_RAW_DATA, $iv ) );
  8.  
  9.     return $encrypted;
  10. }
  11.  
  12. private function decrypt( $data, $key ) {
  13.     $salt = 'cH!swe!retReGu7W6bEDRup7usuDUh9THeD2CHeGE*ewr4n39=E@rAsp7c-Ph@pH';
  14.     $iv_size = openssl_cipher_iv_length( "AES-256-CBC-HMAC-SHA256" );
  15.     $hash = hash( 'sha256', $salt . $key . $salt );
  16.     $iv = substr( $hash, strlen( $hash ) - $iv_size );
  17.     $key = substr( $hash, 0, 32 );
  18.     $decrypted = openssl_decrypt( base64_decode( $data ), "AES-256-CBC-HMAC-SHA256", $key, OPENSSL_RAW_DATA, $iv );
  19.     $decrypted = rtrim( $decrypted, "\0" );
  20.  
  21.     return $decrypted;
  22. }
  23.  
  24.  
  25. // code from here
  26. // https://stackoverflow.com/questions/45246461/convert-framework-from-mcrypt-to-openssl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement