Home  >  Article  >  Backend Development  >  php aes encryption and decryption

php aes encryption and decryption

WBOY
WBOYOriginal
2016-08-08 09:31:14981browse
加密的时候先aes加密,在进行base64加密

<?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);
    }
}
?>

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.

Statement:
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