当前位置:Gxlcms > PHP教程 > 兼容PHP和Java的des加密解密代码分享_php实例

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

php代码:

  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. ?>

java代码:

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

人气教程排行