PHPaes algorithm-PHP source code

Source: Internet
Author: User
Tags mcrypt
Php code of PHPaes algorithm

function aes128cbcEncrypt($key, $text){    /**     * Open the cipher     */    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');    if (! $td)    {        throw new GeneralSecurityException('Invalid mcrypt cipher, check your libmcrypt library and php-mcrypt extention');    }     // replaced MCRYPT_DEV_RANDOM with MCRYPT_RAND since windows doesn't have /dev/rand :)    srand((double)microtime() * 1000000);    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);    /**     * Intialize encryption     */    mcrypt_generic_init($td, $key, $iv);    /**     * Encrypt data     */    $encrypted = mcrypt_generic($td, $text);    /**     * Terminate encryption handler     */    mcrypt_generic_deinit($td);    /**     * AES-128-CBC encryption.  The IV is returned as the first 16 bytes       * of the cipher text.     */    return $iv . $encrypted;} function aes128cbcDecrypt($key, $encrypted_text){    /**     * Open the cipher     */    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');    if (is_callable('mb_substr'))    {        $iv = mb_substr($encrypted_text, 0, Crypto_CIPHER_BLOCK_SIZE, 'latin1');    }     else    {        $iv = substr($encrypted_text, 0, Crypto_CIPHER_BLOCK_SIZE);    }     /**     * Initialize encryption module for decryption     */    mcrypt_generic_init($td, $key, $iv);    /**     * Decrypt encrypted string     */    if (is_callable('mb_substr'))    {        $encrypted = mb_substr($encrypted_text, Crypto_CIPHER_BLOCK_SIZE, mb_strlen($encrypted_text, 'latin1'), 'latin1');    }     else    {        $encrypted = substr($encrypted_text, Crypto_CIPHER_BLOCK_SIZE);    }     $decrypted = mdecrypt_generic($td, $encrypted);    /**     * Terminate decryption handle and close module     */    mcrypt_generic_deinit($td);    mcrypt_module_close($td);    /**     * Show string     */    return trim($decrypted);} define('Crypto_CIPHER_BLOCK_SIZE', 16);$a = aes128cbcEncrypt('pass', 'this is text');echo base64_encode($a) . "/r/n";$b = aes128cbcDecrypt('pass', $a);echo $b . "/r/n";$c = aes128cbcDecrypt('pass', base64_decode(base64_encode($a)));echo $c . "/r/n";?>

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.