Version 7.2 of PHP replaces mcrypt_encrypt with openssl_encrypt, which makes the Aes encryption classes written by ourselves useless.
The client side of this project used AES-128-ECB encryption, and I used online AES tools to test, and found that the encryption and decryption methods I wrote had different values. The key encrypted is not 16 bits long, which results in inconsistency between the encryption and decryption of ios client and server php. After the discussion, the key was 16 bits long, and the problem was solved. The following is the AES-128-ECB encryption class.
class Aes { //The key must be 16 bits public $key ; /** * Decrypted string * @param string $data Character string * @return string */ public function __construct() { $this->key = '1234567890123456'; } public function decode($str) { return openssl_decrypt(base64_decode($str),"AES-128-ECB",$this->key,OPENSSL_RAW_DATA); } /** * Encrypted string * @param string $data Character string * @return string */ public function encode($str) { return base64_encode(openssl_encrypt($str,"AES-128-ECB",$this->key,OPENSSL_RAW_DATA)); } }
If you use CBC encryption, you also need to sort the $iv offset, as follows: AES-128-CBC encryption and decryption class:
class Aes { //The key must be 16 bits public $key ; //Offset public $iv = '1234567890123456'; /** * Decrypted string * @param string $data Character string * @return string */ public function __construct() { $this->key = '1234567890123456'; } public function decode($str) { return openssl_decrypt(base64_decode($str),"AES-128-CBC",$this->key,OPENSSL_RAW_DATA, $this->iv); } /** * Encrypted string * @param string $data Character string * @return string */ public function encode($str) { return base64_encode(openssl_encrypt($str,"AES-128-CBC",$this->key,OPENSSL_RAW_DATA, $this->iv)); } }