PHP与Java使用des加密通讯

http://www.pocketdigi.com/20121112/940.html

原文:http://toptulip.iteye.com/blog/780309 使用php加密字符串,生成密文,java解析密文还原字符串.
Java的部分直接复制调不通,改了下,下面的代码是能正常运行的。
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. $data=mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv);
  26. //$data=strtoupper(bin2hex($data)); //返回大写十六进制字符串
  27. return base64_encode($data);
  28. }
  29. //解密
  30. function decrypt($str)
  31. {
  32. $str = base64_decode ($str);
  33. //$strBin = $this->hex2bin( strtolower($str));
  34. $str = mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_DECRYPT, $this->iv );
  35. $str = $this->pkcs5Unpad( $str );
  36. return $str;
  37. }
  38.  
  39. function hex2bin($hexData)
  40. {
  41. $binData = "";
  42. for($i = 0; $i < strlen ( $hexData ); $i += 2)
  43. {
  44. $binData .= chr(hexdec(substr($hexData, $i, 2)));
  45. }
  46. return $binData;
  47. }
  48.  
  49. function pkcs5Pad($text, $blocksize)
  50. {
  51. $pad = $blocksize - (strlen ( $text ) % $blocksize);
  52. return $text . str_repeat ( chr ( $pad ), $pad );
  53. }
  54.  
  55. function pkcs5Unpad($text)
  56. {
  57. $pad = ord ( $text {strlen ( $text ) - 1} );
  58. if ($pad > strlen ( $text ))
  59. return false;
  60. if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
  61. return false;
  62. return substr ( $text, 0, - 1 * $pad );
  63. }
  64. }
  65. $str = 'abcd';
  66. $key= 'asdfwef5';
  67. $crypt = new DES($key);
  68. $mstr = $crypt->encrypt($str);
  69. $str = $crypt->decrypt($mstr);
  70.  
  71. echo $str.' <=> '.$mstr;
  72.  
  73. ?>
  74.  

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. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    43. //key的长度不能够小于8位字节
    44. Key secretKey = keyFactory.generateSecret(dks);
    45. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
    46. IvParameterSpec iv = new IvParameterSpec(key.getBytes());
    47. AlgorithmParameterSpec paramSpec = iv;
    48. cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec);
    49. byte[] bytes = cipher.doFinal(data);
    50.  
    51. // return byte2hex(bytes);
    52. return new String(Base64.encode(bytes));
    53. } catch (Exception e)
    54. {
    55. throw new Exception(e);
    56. }
    57. }
    58.  
    59. /**
    60. * DES算法,解密
    61. *
    62. * @param data 待解密字符串
    63. * @param key 解密私钥,长度不能够小于8位
    64. * @return 解密后的字节数组
    65. * @throws Exception 异常
    66. */
    67. public static byte[] decode(String key,byte[] data) throws Exception
    68. {
    69. try
    70. {
    71. SecureRandom sr = new SecureRandom();
    72. DESKeySpec dks = new DESKeySpec(key.getBytes());
    73. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    74. //key的长度不能够小于8位字节
    75. Key secretKey = keyFactory.generateSecret(dks);
    76. Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
    77. IvParameterSpec iv = new IvParameterSpec(key.getBytes());
    78. AlgorithmParameterSpec paramSpec = iv;
    79. cipher.init(Cipher.DECRYPT_MODE, secretKey,paramSpec);
    80. return cipher.doFinal(data);
    81. } catch (Exception e)
    82. {
    83. throw new Exception(e);
    84. }
    85. }
    86. /**
    87. * 获取编码后的值
    88. * @param key
    89. * @param data
    90. * @return
    91. * @throws Exception
    92. */
    93. public static String decodeValue(String key,String data)
    94. {
    95. byte[] datas;
    96. String value = null;
    97. try {
    98.  
    99. datas = decode(key, Base64.decode(data.getBytes()));
    100. value = new String(datas);
    101. } catch (Exception e) {
    102. value = "";
    103. }
    104. return value;
    105. }
    106.  
    107. public static void main(String[] args) throws Exception
    108. {
    109. System.out.println("明:abcd ;密:" + Main.encode("asdfwef5","abcd"));
    110. }
    111. }
    112.  
原文地址:https://www.cnblogs.com/jukan/p/5694462.html