Today wrote an AES encryption class for PHP. Applies to the extension of Yii.
If you do not need to replace the Yii::app ()->params[' Encryptkey '] in the YII framework with your corresponding default key.
Class Code:
<?php/** * PHP AES plus decryption class * If you want to share with Java, the key length should be 16-bit length * Because Java only supports 128-bit encryption, PHP also uses 128-bit encryption, you can go with java. * AES is also the standard of 128-bit.
Only the Rijndael algorithm can support 128,192 and 256-bit encryption. * Java to use the Aes/cbc/nopadding standard to add and decrypt * * @author Terry * * */class Phpaes {/** * * This is aes-128/cbc/nopadding E
Ncrypted. * Return base64_encode String * @author Terry * @param string $plaintext * @param string $key/public static FU
Nction Aesencrypt ($plaintext, $key = null) {$plaintext = Trim ($plaintext);
if ($plaintext = = ") return"; if (!extension_loaded (' MCrypt ')) throw new CException (Yii::t (' Yii ', ' aesencrypt requires PHP mcrypt extension to be Loade
D in order to use data encryption feature. ');
$size = Mcrypt_get_block_size (mcrypt_rijndael_128, MCRYPT_MODE_CBC);
$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 ($encrypted);
}/** * This is aes-128/cbc/nopadding 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 lo
Aded 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 RTrim ($decrypted, "the");
}/** * Returns the length of the given string.
* If available uses the multibyte string function Mb_strlen. * @param string $string The string being measured for length * @return integer The length of the string */Private St
atic function strlen ($string) {return extension_loaded (' mbstring ') Mb_strlen ($string, ' 8bit '): strlen ($string);
}/** * Returns the portion of string specified by the start and length parameters. * If available uses the multIbyte string Function Mb_substr * @param string $string the input string.
Must be one character or longer. * @param integer $start The starting position * @param integer $length the desired portion length * @return string The
Extracted part of string, or FALSE on failure or a empty string. * * private static function substr ($string, $start, $length) {return extension_loaded (' mbstring ')? Mb_substr ($string, $s
Tart, $length, ' 8bit '): substr ($string, $start, $length); }
}