溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php中如何實(shí)現(xiàn)一個(gè)對(duì)稱加密算法

發(fā)布時(shí)間:2021-07-26 14:36:57 來(lái)源:億速云 閱讀:153 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)php中如何實(shí)現(xiàn)一個(gè)對(duì)稱加密算法,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。


php對(duì)稱加密算法

KEY 是之前定義的常量

復(fù)制代碼 代碼如下:


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

復(fù)制代碼 代碼如下:


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

}

常用對(duì)稱加密算法(DES/AES)類

xcrypt.php

復(fù)制代碼 代碼如下:


/**
 * 常用對(duì)稱加密算法類
 * 支持密鑰:64/128/256 bit(字節(jié)長(zhǎng)度8/16/32)
 * 支持算法:DES/AES(根據(jù)密鑰長(zhǎng)度自動(dòng)匹配使用:DES:64bit AES:128/256bit)
 * 支持模式:CBC/ECB/OFB/CFB
 * 密文編碼:base64字符串/十六進(jìn)制字符串/二進(jìn)制字符串流
 * 填充方式: PKCS5Padding(DES)
 *
 * @author: linvo
 * @version: 1.0.0
 * @date: 2013/1/10
 */ 
class Xcrypt{ 

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

    /**
     * 構(gòu)造函數(shù)
     *
     * @param string 密鑰
     * @param string 模式
     * @param string 向量("off":不使用 / "auto":自動(dòng) / 其他:指定值,長(zhǎng)度同密鑰)
     */ 
    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; 
    }

上面類的使用方法

復(fù)制代碼 代碼如下:


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

include "xcrypt.php"; 

echo '<pre>';   
$a = isset($_GET['a']) ? $_GET['a'] : '測(cè)試123'; 

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

//設(shè)置模式和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>'; 

以上就是php中如何實(shí)現(xiàn)一個(gè)對(duì)稱加密算法,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI