PHP AES Encryption Class program code sharing _php Tutorial

Source: Internet
Author: User
It's all right. With a PHP AES encryption class program for YII extensions if you do not use the YII framework, the Code Yii::app ()->params[\ ' encryptkey\ ') is replaced by your corresponding default key.

AES encryption algorithm-algorithm principle

The AES algorithm is based on permutation and permutation operations. Permutations are rearranging the data, replacing one unit of data with another. AES uses several different methods to perform permutation and permutation operations.
AES is an iterative, symmetric-key grouping of passwords that can use 128, 192, and 256-bit keys and encrypt and decrypt data in 128-bit (16-byte) packets. Unlike the public key password, which uses the key pair, the symmetric key password encrypts and decrypts the data using the same key. The number of bits of encrypted data returned by a block cipher is the same as the input data. Iterative encryption uses a looping structure that repeats the substitution and substitution of input data in the loop.

The code is as follows Copy Code

/**
* 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);
}
}

How to use

The code is as follows Copy Code

Require_once ('./aes.php ');
$aes = new Aes ();
$aes = new Aes (TRUE);//Store the encrypted string as hexadecimal
$aes = new Aes (true,true);//With debug information and encrypted string stored in hexadecimal
$key = "This is a + byte key";//Key
$keys = $aes->makekey ($key);
$encode = "123456";//The string that is encrypted
$CT = $aes->encryptstring ($encode, $keys);
echo "encode =". $ct. "
";
$CPT = $aes->decryptstring ($ct, $keys);
echo "decode =". $cpt;
?>

http://www.bkjia.com/PHPjc/632843.html www.bkjia.com true http://www.bkjia.com/PHPjc/632843.html techarticle It 's all right. With a PHP AES encryption class program, suitable for yii extension if not in the YII framework, the code in the Yii::app ()->params[\ ' encryptkey\ ') with your corresponding default key can be ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.