PHP symmetric encryption algorithm example_PHP tutorial

WBOY
Release: 2016-07-13 10:30:16
Original
1228 people have browsed it


php symmetric encryption algorithm

KEY is a previously defined constant

Copy code The code is as follows:

Mcrypt::encrypt();
Mcrypt::decrypt();

Copy code The code is as follows:

defined('ROOT') or exit('Access Denied');

class Mcrypt{

public static function encrypt($code){
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(KEY), $code, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MC RYPT_MODE_ECB), MCRYPT_RAND)));
}

public static function decrypt($code){
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(KEY), base64_decode($code), MCRYPT_MODE_ECB, mcrypt_get_iv_size(MCRYPT_RIJ NDAEL_256 , MCRYPT_MODE_ECB), MCRYPT_RAND));
}

}

Commonly used symmetric encryption algorithm (DES/AES) class

xcrypt.php

Copy code The code is as follows:

/**
* Commonly used symmetric encryption algorithm classes
* Supported keys: 64/128/256 bit (byte length 8/16/32)
* Supported algorithms: DES/AES (automatic matching based on key length) Use: DES: 64bit AES: 128/256bit)
* Supported modes: CBC/ECB/OFB/CFB
* Ciphertext encoding: base64 string/hex string/binary string stream
*Padding method: PKCS5Padding (DES)
*
* @author: linvo
* @version: 1.0.0
* @date: 2013/1/10
*/ 
class Xcrypt{ 

    private $mcrypt; 
    private $key; 
    private $mode; 
    private $iv; 
    private $blocksize; 

    /**
* Constructor
*
* @param string key
* @param string mode
* @param string vector ("off": not used / "auto": automatic / other :Specify value, the length is the same as the key)
*/ 
    public function __construct($key, $mode = 'cbc', $iv = "off"){ 
        switch (strlen($key)){ 
        case 8: 
            $this->mcrypt = MCRYPT_DES; 
            break; 
        case 16: 
            $this->mcrypt = MCRYPT_RIJNDAEL_128; 
            break; 
        case 32: 
            $this->mcrypt = MCRYPT_RIJNDAEL_256; 
            break; 
        default: 
            die("Key size must be 8/16/32"); 
        } 

        $this->key = $key; 

        switch (strtolower($mode)){ 
        case 'ofb': 
            $this->mode = MCRYPT_MODE_OFB; 
            if ($iv == 'off') die('OFB must give a IV'); //OFB必须有向量 
            break; 
        case 'cfb': 
            $this->mode = MCRYPT_MODE_CFB; 
            if ($iv == 'off') die('CFB must give a IV'); //CFB必须有向量 
            break; 
        case 'ecb': 
            $this->mode = MCRYPT_MODE_ECB; 
            $iv = 'off'; //ECB不需要向量 
            break; 
        case 'cbc': 
        default: 
            $this->mode = MCRYPT_MODE_CBC; 
        } 

        switch (strtolower($iv)){ 
        case "off": 
            $this->iv = null; 
            break; 
        case "auto": 
            $source = PHP_OS=='WINNT' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM; 
            $this->iv = mcrypt_create_iv(mcrypt_get_block_size($this->mcrypt, $this->mode), $source); 
            break; 
        default: 
            $this->iv = $iv; 
        } 

    
    } 

 
    /**
* Get vector value
* @param string vector value encoding (base64/hex/bin)
* @return string vector value
*/ 
    public function getIV($code = 'base64'){ 
        switch ($code){ 
        case 'base64': 
            $ret = base64_encode($this->iv); 
            break; 
        case 'hex': 
            $ret = bin2hex($this->iv); 
            break; 
        case 'bin': 
        default: 
            $ret = $this->iv; 
        } 
        return $ret; 
    } 

 
    /**
* Encryption
* @param string plaintext
* @param string ciphertext encoding (base64/hex/bin)
* @return string ciphertext
*/ 
    public function encrypt($str, $code = 'base64'){ 
        if ($this->mcrypt == MCRYPT_DES) $str = $this->_pkcs5Pad($str); 

        if (isset($this->iv)) { 
            $result = mcrypt_encrypt($this->mcrypt, $this->key, $str, $this->mode, $this->iv);   
        } else { 
            @$result = mcrypt_encrypt($this->mcrypt, $this->key, $str, $this->mode);   
        } 

        switch ($code){ 
        case 'base64': 
            $ret = base64_encode($result); 
            break; 
        case 'hex': 
            $ret = bin2hex($result); 
            break; 
        case 'bin': 
        default: 
            $ret = $result; 
        } 

        return $ret; 

    } 

    /**
* Decryption
* @param string ciphertext
* @param string ciphertext encoding (base64/hex/bin)
* @return string plaintext
*/ 
    public function decrypt($str, $code = "base64"){     
        $ret = false; 

        switch ($code){ 
        case 'base64': 
            $str = base64_decode($str); 
            break; 
        case 'hex': 
            $str = $this->_hex2bin($str); 
            break; 
        case 'bin': 
        default: 
        } 

        if ($str !== false){ 
            if (isset($this->iv)) { 
                $ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode, $this->iv);   
            } else { 
                @$ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode);   
            } 
            if ($this->mcrypt == MCRYPT_DES) $ret = $this->_pkcs5Unpad($ret); 
            $ret = trim($ret); 
        } 

        return $ret;  
    }

    private function _pkcs5Pad($text){ 
        $this->blocksize = mcrypt_get_block_size($this->mcrypt, $this->mode);   
        $pad = $this->blocksize - (strlen($text) % $this->blocksize); 
        return $text . str_repeat(chr($pad), $pad); 
    } 

    private function _pkcs5Unpad($text){ 
        $pad = ord($text{strlen($text) - 1}); 
        if ($pad > strlen($text)) return false; 
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false; 
        $ret = substr($text, 0, -1 * $pad); 
        return $ret; 
    } 

    private function _hex2bin($hex = false){ 
        $ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i', $hex) ? pack("H*", $hex) : false;     
        return $ret; 
    }

上面类的使用方法

复制代码 代码如下:

header('Content-Type:text/html;Charset=utf-8;'); 

include "xcrypt.php"; 

echo '
';    <br>$a = isset($_GET['a']) ? $_GET['a'] : '测试123';  <br><br>//密钥  <br>$key = '12345678123456781234567812345678'; //256 bit  <br>$key = '1234567812345678'; //128 bit  <br>$key = '12345678'; //64 bit  <br><br>//设置模式和IV  <br>$m = new Xcrypt($key, 'cbc', 'auto');  <br><br>//获取向量值  <br>echo '向量:';  <br>var_dump($m->getIV());  <br><br>//加密  <br>$b = $m->encrypt($a, 'base64');  <br>//解密  <br>$c = $m->decrypt($b, 'base64');  <br><br>echo '加密后:';  <br>var_dump($b);  <br>echo '解密后:';  <br>var_dump($c);  <br>echo '
'; 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/766111.htmlTechArticlephp symmetric encryption algorithm KEY is the constant defined before. Copy the code code as follows: Mcrypt::encrypt(); Mcrypt: :decrypt(); Copy the code as follows: defined('ROOT') or exit('Access Denie...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!