Home  >  Article  >  Backend Development  >  Replacement scheme for AES encryption and decryption mcrypt_module_open() method in php7.1

Replacement scheme for AES encryption and decryption mcrypt_module_open() method in php7.1

黄舟
黄舟Original
2017-10-18 09:19:431628browse

This article mainly introduces to you the relevant information about the replacement scheme of AES encryption and decryption method mcrypt_module_open() in PHP 7.1. The article introduces it in detail through sample code, which has certain reference learning value for everyone's study or work. Friends who need it, please follow the editor to learn together.

Preface

The mcrypt extension is about 10 years out of date and complicated to use. Therefore it was deprecated and replaced by OpenSSL. Starting with PHP 7.2 it will be removed from the core code and moved to PECL.

The PHP manual gives an alternative on the 7.1 migration page, which is to use OpenSSL to replace MCrypt.

Sample code


/**
 * [AesSecurity aes加密,支持PHP7.1]
 */
class AesSecurity
{
 /**
  * [encrypt aes加密]
  * @param [type]     $input [要加密的数据]
  * @param [type]     $key [加密key]
  * @return [type]       [加密后的数据]
  */
 public static function encrypt($input, $key)
 {
  $data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
  $data = base64_encode($data);
  return $data;
 }
 /**
  * [decrypt aes解密]
  * @param [type]     $sStr [要解密的数据]
  * @param [type]     $sKey [加密key]
  * @return [type]       [解密后的数据]
  */
 public static function decrypt($sStr, $sKey)
 {
  $decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);
  return $decrypted;
 }
}

Can be adapted according to needs.

Summarize

The above is the detailed content of Replacement scheme for AES encryption and decryption mcrypt_module_open() method in php7.1. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn