/** * Php AES encryption/decryption class * Because java only supports 128-bit encryption, php also uses 128-bit encryption and can interoperate with java. * At the same time, the AES standard is also 128 bits. Only the RIJNDAEL algorithm supports 128,192-bit and 256-bit encryption. * * @ Author Terry * */ Class PhpAes { /** * This was AES-128/CBC/ZeroBytePadding encrypted. * Return base64_encode string * @ Author Terry * @ Param string $ plaintext * @ Param string $ key */ Public static function AesEncrypt ($ plaintext, $ key = null) { If ($ plaintext = '') return ''; If (! Extension_loaded ('mcrypt ')) Throw new CException (Yii: t ('yii', 'aesencrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature .')); $ Size = mcrypt_get_block_size (MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC ); $ Plaintext = self: PKCS5Padding ($ plaintext, $ size ); $ Module = mcrypt_module_open (MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC ,''); $ Key = self: substr ($ key = null? Yii: app ()-> params ['encryptkey']: $ key, 0, mcrypt_enc_get_key_size ($ module )); /* Create the IV and determine the keysize length, use MCRYPT_RAND * On Windows instead */ $ Iv = substr (md5 ($ key), 0, mcrypt_enc_get_iv_size ($ module )); /* Intialize encryption */ Mcrypt_generic_init ($ module, $ key, $ iv ); /* Encrypt data */ $ Encrypted = mcrypt_generic ($ module, $ plaintext ); /* Terminate encryption handler */ Mcrypt_generic_deinit ($ module ); Mcrypt_module_close ($ module ); Return base64_encode (trim ($ encrypted )); } /** * This was AES-128/CBC/ZeroBytePadding decrypted. * @ Author Terry * @ Param string $ encrypted base64_encode encrypted string * @ Param string $ key * @ Throws CException * @ Return string */ Public static function AesDecrypt ($ encrypted, $ key = null) { If ($ encrypted = '') return ''; If (! Extension_loaded ('mcrypt ')) Throw new CException (Yii: t ('yii', 'aesdecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature .')); $ Ciphertext_dec = base64_decode ($ encrypted ); $ Module = mcrypt_module_open (MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC ,''); $ Key = self: substr ($ key = null? Yii: app ()-> params ['encryptkey']: $ key, 0, mcrypt_enc_get_key_size ($ module )); $ Iv = substr (md5 ($ key), 0, mcrypt_enc_get_iv_size ($ module )); /* Initialize encryption module for decryption */ Mcrypt_generic_init ($ module, $ key, $ iv ); /* Decrypt encrypted string */ $ Decrypted = mdecrypt_generic ($ module, $ ciphertext_dec ); /* Terminate decryption handle and close module */ Mcrypt_generic_deinit ($ module ); Mcrypt_module_close ($ module ); Return self: UnPKCS5Padding ($ decrypted ); } Private static function strlen ($ string) { Return extension_loaded ('mbstring ')? Mb_strlen ($ string, '8bit '): strlen ($ string ); }
Private static function substr ($ string, $ start, $ length) { Return extension_loaded ('mbstring ')? Mb_substr ($ string, $ start, $ length, '8bit '): substr ($ string, $ start, $ length ); } Private static function PKCS5Padding ($ text, $ blocksize ){ $ Pad = $ blocksize-(self: strlen ($ text) % $ blocksize ); Return $ text. str_repeat (chr ($ pad), $ pad ); } Private static function UnPKCS5Padding ($ text) { $ Pad = ord ($ text {self: strlen ($ text)-1 }); If ($ pad> self: strlen ($ text) return false; If (strspn ($ text, chr ($ pad), self: strlen ($ text)-$ pad )! = $ Pad) return false; Return substr ($ text, 0,-1 * $ pad ); } } |