|
/**
* PHP AES Plus decryption class
* Because Java only supports 128-bit encryption, PHP also uses 128-bit encryption, which can be transferred to Java.
* AES is also a 128-bit standard. Only the Rijndael algorithm can support 128,192 and 256-bit encryption.
*
* @author Terry
*
*/
Class Phpaes
{
/**
* This is 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 being loaded in order to use data encryption F Eature. '));
$size = Mcrypt_get_block_size (mcrypt_rijndael_128, MCRYPT_MODE_CBC);
$plaintext = self::P kcs5padding ($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 is 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 being loaded in order to use data encryption F Eature. '));
$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);
}
}
|