当前位置:首页 > PHP教程 > php应用 > 列表

兼容PHP和Java的des加密解密代码分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-03-03 11:44:12 浏览: 评论:0 

这篇文章主要介绍了兼容PHP和Java的des加密解密代码分享,适合如服务器是JAVA语言编写,客户端是PHP编写,并需要des加密解密的情况,需要的朋友可以参考下

php代码:

  1. <?php 
  2. class DES 
  3. { 
  4.     var $key; 
  5.     var $iv; //偏移量 
  6.    
  7.     function DES($key, $iv=0) 
  8.     { 
  9.         $this->key = $key; 
  10.         if($iv == 0) 
  11.         { 
  12.             $this->iv = $key; 
  13.         } 
  14.         else 
  15.         { 
  16.             $this->iv = $iv; 
  17.         } 
  18.     } 
  19.    
  20.     //加密 
  21.     function encrypt($str) 
  22.     {        
  23.         $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC ); 
  24.         $str = $this->pkcs5Pad ( $str, $size ); 
  25.    
  26.         $data=mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv); 
  27.         //$data=strtoupper(bin2hex($data)); //返回大写十六进制字符串 
  28.         return base64_encode($data); 
  29.     } 
  30.    
  31.     //解密 
  32.     function decrypt($str) 
  33.     { 
  34.         $str = base64_decode ($str); 
  35.         //$strBin = $this->hex2bin( strtolower($str)); 
  36.         $str = mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_DECRYPT, $this->iv ); 
  37.         $str = $this->pkcs5Unpad( $str ); 
  38.         return $str; 
  39.     } 
  40.    
  41.     function hex2bin($hexData) 
  42.     { 
  43.         $binData = ""; 
  44.         for($i = 0; $i < strlen ( $hexData ); $i += 2) 
  45.         { 
  46.             $binData .= chr(hexdec(substr($hexData, $i, 2))); 
  47.         } 
  48.         return $binData; 
  49.     } 
  50.    
  51.     function pkcs5Pad($text, $blocksize) 
  52.     { 
  53.         $pad = $blocksize - (strlen ( $text ) % $blocksize); 
  54.         return $text . str_repeat ( chr ( $pad ), $pad ); 
  55.     } 
  56.    
  57.     function pkcs5Unpad($text) 
  58.     { 
  59.         $pad = ord ( $text {strlen ( $text ) - 1} ); 
  60.         if ($pad > strlen ( $text )) 
  61.             return false; 
  62.         if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) 
  63.             return false; 
  64.         return substr ( $text, 0, - 1 * $pad ); 
  65.     } 
  66. } 
  67. $str = 'abcd'; 
  68. $key= 'asdfwef5'; 
  69. $crypt = new DES($key); 
  70. $mstr = $crypt->encrypt($str); 
  71. $str = $crypt->decrypt($mstr); 
  72.    
  73. echo $str.' <=> '.$mstr; 
  74.    
  75. ?> 

java代码:

  1. package com.test; 
  2.    
  3. import it.sauronsoftware.base64.Base64; 
  4.    
  5. import java.security.Key; 
  6. import java.security.SecureRandom; 
  7. import java.security.spec.AlgorithmParameterSpec; 
  8.    
  9. import javax.crypto.Cipher; 
  10. import javax.crypto.SecretKeyFactory; 
  11. import javax.crypto.spec.DESKeySpec; 
  12. import javax.crypto.spec.IvParameterSpec; 
  13.    
  14. public class Main 
  15. { 
  16.  public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding"; 
  17.  /** 
  18.   * DES算法,加密 
  19.   * 
  20.   * @param data 待加密字符串 
  21.   * @param key 加密私钥,长度不能够小于8位 
  22.   * @return 加密后的字节数组,一般结合Base64编码使用 
  23.   * @throws CryptException 异常 
  24.   */ 
  25.  public static String encode(String key,String data) throws Exception 
  26.  { 
  27.   return encode(key, data.getBytes()); 
  28.  } 
  29.  /** 
  30.   * DES算法,加密 
  31.   * 
  32.   * @param data 待加密字符串 
  33.   * @param key 加密私钥,长度不能够小于8位 
  34.   * @return 加密后的字节数组,一般结合Base64编码使用 
  35.   * @throws CryptException 异常 
  36.   */ 
  37.  public static String encode(String key,byte[] data) throws Exception 
  38.  { 
  39.   try 
  40.   { 
  41.         DESKeySpec dks = new DESKeySpec(key.getBytes()); 
  42.    
  43.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 
  44.    //key的长度不能够小于8位字节 
  45.    Key secretKey = keyFactory.generateSecret(dks); 
  46.    Cipher cipher = Cipher.getInstance(ALGORITHM_DES); 
  47.    IvParameterSpec iv = new IvParameterSpec(key.getBytes()); 
  48.    AlgorithmParameterSpec paramSpec = iv; 
  49.    cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec); 
  50.    
  51.    byte[] bytes = cipher.doFinal(data); 
  52.    
  53.    
  54. //   return byte2hex(bytes); 
  55.    return new String(Base64.encode(bytes)); 
  56.   } catch (Exception e) 
  57.   { 
  58.    throw new Exception(e); 
  59.   } 
  60.  } 
  61.    
  62.  /** 
  63.   * DES算法,解密 
  64.   * 
  65.   * @param data 待解密字符串 
  66.   * @param key 解密私钥,长度不能够小于8位 
  67.   * @return 解密后的字节数组 
  68.   * @throws Exception 异常 
  69.   */ 
  70.  public static byte[] decode(String key,byte[] data) throws Exception 
  71.  { 
  72.   try 
  73.   { 
  74.     SecureRandom sr = new SecureRandom(); 
  75.         DESKeySpec dks = new DESKeySpec(key.getBytes()); 
  76.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 
  77.    //key的长度不能够小于8位字节 
  78.    Key secretKey = keyFactory.generateSecret(dks); 
  79.    Cipher cipher = Cipher.getInstance(ALGORITHM_DES); 
  80.    IvParameterSpec iv = new IvParameterSpec(key.getBytes()); 
  81.    AlgorithmParameterSpec paramSpec = iv; 
  82.    cipher.init(Cipher.DECRYPT_MODE, secretKey,paramSpec); 
  83.    return cipher.doFinal(data); 
  84.   } catch (Exception e) 
  85.   { 
  86.    throw new Exception(e); 
  87.   } 
  88.  } 
  89.    
  90.  /** 
  91.   * 获取编码后的值 
  92.   * @param key 
  93.   * @param data 
  94.   * @return 
  95.   * @throws Exception 
  96.   */ 
  97.  public static String decodeValue(String key,String data)  
  98.  { 
  99.     byte[] datas; 
  100.     String value = null; 
  101.         try { 
  102.    
  103.             datas = decode(key, Base64.decode(data.getBytes())); 
  104.    
  105.             value = new String(datas); 
  106.         } catch (Exception e) { 
  107.             value = ""; 
  108.         } 
  109.     return value; 
  110.  } 
  111.    
  112.  public static void main(String[] args) throws Exception 
  113.  { 
  114.     System.out.println("明:abcd ;密:" + Main.encode("asdfwef5","abcd")); 
  115.  } 
  116. } 

Tags: des加密解密

分享到: