/apputils/src/main/java/com/allen/apputils/SecretUtils.java

https://github.com/AllenCoder/SuperUtils · Java · 112 lines · 51 code · 16 blank · 45 comment · 1 complexity · 79d525e92b2e8ef36fcc47abd1d5b5a5 MD5 · raw file

  1. /*
  2. * Copyright 2017 [AllenCoder]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.allen.apputils;
  17. import java.io.UnsupportedEncodingException;
  18. import java.security.NoSuchAlgorithmException;
  19. import javax.crypto.Cipher;
  20. import javax.crypto.NoSuchPaddingException;
  21. import javax.crypto.SecretKey;
  22. import javax.crypto.spec.SecretKeySpec;
  23. /**
  24. * 3DES 加密/解密
  25. */
  26. /**
  27. * SecretUtils {3DES加密解密的工具类 }
  28. */
  29. public class SecretUtils {
  30. //定义加密算法,有DES、DESede(即3DES)、Blowfish
  31. private static final String Algorithm = "DESede";
  32. private static final String PASSWORD_CRYPT_KEY = "2017abcdefghijklmnopQrst123";
  33. /**
  34. * 加密方法
  35. * @param src 源数据的字节数组
  36. * @return
  37. */
  38. public static byte[] encryptMode(byte[] src) {
  39. try {
  40. SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm); //生成密钥
  41. Cipher c1 = Cipher.getInstance(Algorithm); //实例化负责加密/解密的Cipher工具类
  42. c1.init(Cipher.ENCRYPT_MODE, deskey); //初始化为加密模式
  43. return c1.doFinal(src);
  44. } catch (java.security.NoSuchAlgorithmException e1) {
  45. e1.printStackTrace();
  46. } catch (javax.crypto.NoSuchPaddingException e2) {
  47. e2.printStackTrace();
  48. } catch (java.lang.Exception e3) {
  49. e3.printStackTrace();
  50. }
  51. return null;
  52. }
  53. /**
  54. * 解密函数
  55. * @param src 密文的字节数组
  56. * @return
  57. */
  58. public static byte[] decryptMode(byte[] src) {
  59. try {
  60. SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
  61. Cipher c1 = Cipher.getInstance(Algorithm);
  62. c1.init(Cipher.DECRYPT_MODE, deskey); //初始化为解密模式
  63. return c1.doFinal(src);
  64. } catch (java.security.NoSuchAlgorithmException e1) {
  65. e1.printStackTrace();
  66. } catch (javax.crypto.NoSuchPaddingException e2) {
  67. e2.printStackTrace();
  68. } catch (java.lang.Exception e3) {
  69. e3.printStackTrace();
  70. }
  71. return null;
  72. }
  73. /*
  74. * 根据字符串生成密钥字节数组
  75. * @param keyStr 密钥字符串
  76. * @return
  77. * @throws UnsupportedEncodingException
  78. */
  79. public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{
  80. byte[] key = new byte[24]; //声明一个24位的字节数组,默认里面都是0
  81. byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组
  82. /*
  83. * 执行数组拷贝
  84. * System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位)
  85. */
  86. if(key.length > temp.length){
  87. //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
  88. System.arraycopy(temp, 0, key, 0, temp.length);
  89. }else{
  90. //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
  91. System.arraycopy(temp, 0, key, 0, key.length);
  92. }
  93. return key;
  94. }
  95. }