PHP与Java交互的des加密解密代码

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



[/post]

java代码:
[post]
  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. }



[/post]


浏览825588 / 回复0
举报内容
返回 [发帖] 发回复:评论……

+
提交评论