欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php对称加密算法示例

程序员文章站 2023-11-23 17:57:52
php对称加密算法 key 是之前定义的常量复制代码 代码如下:mcrypt::encrypt();mcrypt::decrypt(); 复制代码 代码如下:defin...


php对称加密算法

key 是之前定义的常量

复制代码 代码如下:

mcrypt::encrypt();
mcrypt::decrypt();

复制代码 代码如下:

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, mcrypt_mode_ecb), mcrypt_rand)));
 }

 public static function decrypt($code){
  return mcrypt_decrypt(mcrypt_rijndael_256, md5(key), base64_decode($code), mcrypt_mode_ecb, mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb), mcrypt_rand));
 }

}

常用对称加密算法(des/aes)类

xcrypt.php

复制代码 代码如下:

/**
 * 常用对称加密算法类
 * 支持密钥:64/128/256 bit(字节长度8/16/32)
 * 支持算法:des/aes(根据密钥长度自动匹配使用:des:64bit aes:128/256bit)
 * 支持模式:cbc/ecb/ofb/cfb
 * 密文编码:base64字符串/十六进制字符串/二进制字符串流
 * 填充方式: pkcs5padding(des)
 *
 * @author: linvo
 * @version: 1.0.0
 * @date: 2013/1/10
 */ 
class xcrypt{ 

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

    /**
     * 构造函数
     *
     * @param string 密钥
     * @param string 模式
     * @param string 向量("off":不使用 / "auto":自动 / 其他:指定值,长度同密钥)
     */ 
    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; 
        } 

    
    } 

 
    /**
     * 获取向量值
     * @param string 向量值编码(base64/hex/bin)
     * @return string 向量值
     */ 
    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; 
    } 

 
    /**
     * 加密
     * @param string 明文
     * @param string 密文编码(base64/hex/bin)
     * @return string 密文
     */ 
    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; 

    } 

    /**
     * 解密 
     * @param string 密文
     * @param string 密文编码(base64/hex/bin)
     * @return string 明文
     */ 
    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; 
    }

上面类的使用方法

复制代码 代码如下:

<?php 
header('content-type:text/html;charset=utf-8;'); 

include "xcrypt.php"; 

echo '<pre>';   
$a = isset($_get['a']) ? $_get['a'] : '测试123'; 

//密钥 
$key = '12345678123456781234567812345678'; //256 bit 
$key = '1234567812345678'; //128 bit 
$key = '12345678'; //64 bit 

//设置模式和iv 
$m = new xcrypt($key, 'cbc', 'auto'); 

//获取向量值 
echo '向量:'; 
var_dump($m->getiv()); 

//加密 
$b = $m->encrypt($a, 'base64'); 
//解密 
$c = $m->decrypt($b, 'base64'); 

echo '加密后:'; 
var_dump($b); 
echo '解密后:'; 
var_dump($c); 
echo '</pre>';