Home > Backend Development > PHP Tutorial > php aes encryption and decryption

php aes encryption and decryption

WBOY
Release: 2016-08-08 09:31:14
Original
1036 people have browsed it
加密的时候先aes加密,在进行base64加密
Copy after login
Copy after login
<?php
/**
 * 利用mcrypt做AES加密解密
 */

class Aes
{
    /**
     * 算法,另外还有192和256两种长度
     */
    const CIPHER = MCRYPT_RIJNDAEL_128;
    /**
     * 模式 
     */
    const MODE = MCRYPT_MODE_ECB;
    
    /**
     * 加密
     * @param string $key 密钥
     * @param string $str 需加密的字符串
     * @return type 
     */
    public function encode( $key, $str ){
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER,self::MODE),MCRYPT_RAND);
        return mcrypt_encrypt(self::CIPHER, $key, $str, self::MODE, $iv);
    }
    
    /**
     * 解密
     * @param type $key
     * @param type $str
     * @return type 
     */
    public function decode( $key, $str ){
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER,self::MODE),MCRYPT_RAND);
        return mcrypt_decrypt(self::CIPHER, $key, $str, self::MODE, $iv);
    }
}
?>
Copy after login

The above introduces php aes encryption and decryption, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template