PageRenderTime 109ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/qq/qqbuy/common/util/SecretUtils.java

https://gitlab.com/JingYing/paipaiapp
Java | 152 lines | 78 code | 21 blank | 53 comment | 1 complexity | 6dae11eb7610c1c1eb5735b6bb069485 MD5 | raw file
  1. package com.qq.qqbuy.common.util;
  2. import java.io.UnsupportedEncodingException;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.SecretKey;
  5. import javax.crypto.spec.SecretKeySpec;
  6. /**
  7. * SecretUtils {3DES加密解密的工具类 }
  8. * @author lhn
  9. * @date 2015-05-04
  10. */
  11. public class SecretUtils {
  12. //定义加密算法,有DES、DESede(即3DES)、Blowfish
  13. private static final String Algorithm = "DESede";
  14. private static final String PASSWORD_CRYPT_KEY = "2015PPAPPSubmitOrder0504";
  15. /**
  16. * 加密方法
  17. * @param src 源数据的字节数组
  18. * @return
  19. */
  20. public static byte[] encryptMode(byte[] src) {
  21. try {
  22. SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm); //生成密钥
  23. Cipher c1 = Cipher.getInstance(Algorithm); //实例化负责加密/解密的Cipher工具类
  24. c1.init(Cipher.ENCRYPT_MODE, deskey); //初始化为加密模式
  25. return c1.doFinal(src);
  26. } catch (java.security.NoSuchAlgorithmException e1) {
  27. e1.printStackTrace();
  28. } catch (javax.crypto.NoSuchPaddingException e2) {
  29. e2.printStackTrace();
  30. } catch (java.lang.Exception e3) {
  31. e3.printStackTrace();
  32. }
  33. return null;
  34. }
  35. /**
  36. * 解密函数
  37. * @param src 密文的字节数组
  38. * @return
  39. */
  40. public static byte[] decryptMode(byte[] src) {
  41. try {
  42. SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
  43. Cipher c1 = Cipher.getInstance(Algorithm);
  44. c1.init(Cipher.DECRYPT_MODE, deskey); //初始化为解密模式
  45. return c1.doFinal(src);
  46. } catch (java.security.NoSuchAlgorithmException e1) {
  47. e1.printStackTrace();
  48. } catch (javax.crypto.NoSuchPaddingException e2) {
  49. e2.printStackTrace();
  50. } catch (java.lang.Exception e3) {
  51. e3.printStackTrace();
  52. }
  53. return null;
  54. }
  55. /**
  56. * 根据字符串生成密钥字节数组
  57. * @param keyStr 密钥字符串
  58. * @Description 随便拿一个String.getBytes()是不行的,会报如下错误
  59. * java.security.InvalidKeyException: Invalid key length: 31 bytes
  60. * 解决方法有很多:①按密钥固定长度重新定义字符串;
  61. * ②先把字符串用Base64或者MD5加密,然后截取固定长度的字符转成byte数组;
  62. * ③字符串转成Byte数组,针对该数组进行修改,若长度过长则只截取一部分,若长度不够则补零
  63. * @return
  64. * @throws UnsupportedEncodingException
  65. */
  66. public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{
  67. byte[] key = new byte[24]; //声明一个24位的字节数组,默认里面都是0
  68. byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组
  69. System.out.println(temp.length+"|"+key.length);
  70. /*
  71. * 执行数组拷贝
  72. * System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位)
  73. */
  74. if(key.length > temp.length){
  75. //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
  76. System.arraycopy(temp, 0, key, 0, temp.length);
  77. }else{
  78. //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
  79. System.arraycopy(temp, 0, key, 0, key.length);
  80. }
  81. return key;
  82. }
  83. /**
  84. *
  85. * @Title: 解密
  86. * @Description:该方法返回解密后的内容
  87. * @param 加密后的内容
  88. * @return String 解密后的信息
  89. * @throws
  90. */
  91. public static String DESdecode(String value){
  92. try {
  93. byte[] decoded = decryptMode(value.getBytes());
  94. return new String(decoded);
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return "";
  99. }
  100. /**
  101. *
  102. * @Title: 加密
  103. * @Description: 对传入数据进行3DES加密
  104. * @param value:未加密内容
  105. * @return String 加密后的信息
  106. * @throws
  107. */
  108. public static String DESencode(String value){
  109. try {
  110. byte[] decoded = encryptMode(value.getBytes());
  111. return new String(decoded);
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. return "";
  116. }
  117. public static void main(String[] args) {
  118. String msg = "3DES加密解密案例";
  119. System.out.println("【加密前】:" + msg);
  120. //加密
  121. byte[] secretArr = SecretUtils.encryptMode(msg.getBytes());
  122. System.out.println("【加密后】:" + new String(secretArr));
  123. //解密
  124. byte[] myMsgArr = SecretUtils.decryptMode(secretArr);
  125. System.out.println("【解密后】:" + new String(myMsgArr));
  126. //加密
  127. System.out.println("【加密后】:" + DESencode(msg));
  128. //解密
  129. System.out.println("【解密后】:" + DESdecode(msg));
  130. }
  131. }