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

PHP中使用DES加密解密

程序员文章站 2022-03-12 19:40:59
...
<?php
namespace Common\ORG\Api;
use Eframe\Logic\ConfigLogic;
use Think\Exception; 

class Descode 
{     
    /** 
     * 解密函数 
     * 算法:des 
     * 加密模式:ecb 
     * 补齐方法:PKCS5 
     * @param unknown_type $input 
     */       
    public static  function encryptForDES($input,$key)   
    {         
       $size = mcrypt_get_block_size('des','ecb');  
       $input = self::pkcs5_pad($input, $size); 
       $td = mcrypt_module_open('des', '', 'ecb', '');
      
       $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);  
       
       @mcrypt_generic_init($td, $key, $iv);  
       $data = mcrypt_generic($td, $input);  
       mcrypt_generic_deinit($td);  
       mcrypt_module_close($td);  
       $data = base64_encode($data);  // bin2hex($data)
       return $data;  
       
       
    }
    
    
    public static  function decryptForDES($input,$key)   
    {   
       $input = base64_decode($input); 
       $size = mcrypt_get_block_size('des','ecb');  
       $td = mcrypt_module_open('des', '', 'ecb', '');  
       $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);  
       @mcrypt_generic_init($td, $key, $iv);  
       $data = mdecrypt_generic($td, $input);  
       mcrypt_generic_deinit($td);  
       mcrypt_module_close($td);  
       $data = self::pkcs5_unpad($data, $size);    
       return $data; 
      
         
    }    
    
             
    public static  function pkcs5_pad ($text, $blocksize)   
    {         
       $pad = $blocksize - (strlen($text) % $blocksize);  
       return $text . str_repeat(chr($pad), $pad);  
    } 
        
    public static  function pkcs5_unpad($text)   
    {         
       $pad = ord($text{strlen($text)-1});  
       if ($pad > strlen($text))  
       {  
           return false;  
       }  
       if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)  
       {  
          return false;  
       }  
       return substr($text, 0, -1 * $pad);  
    }  
}
 
相关标签: php整理