EncryptUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  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 me.zhengjie.utils;
  17. import javax.crypto.Cipher;
  18. import javax.crypto.SecretKey;
  19. import javax.crypto.SecretKeyFactory;
  20. import javax.crypto.spec.DESKeySpec;
  21. import javax.crypto.spec.IvParameterSpec;
  22. import java.nio.charset.StandardCharsets;
  23. /**
  24. * 加密
  25. * @author Zheng Jie
  26. * @date 2018-11-23
  27. */
  28. public class EncryptUtils {
  29. private static final String STR_PARAM = "Passw0rd";
  30. private static Cipher cipher;
  31. private static final IvParameterSpec IV = new IvParameterSpec(STR_PARAM.getBytes(StandardCharsets.UTF_8));
  32. private static DESKeySpec getDesKeySpec(String source) throws Exception {
  33. if (source == null || source.length() == 0){
  34. return null;
  35. }
  36. cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  37. String strKey = "Passw0rd";
  38. return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
  39. }
  40. /**
  41. * 对称加密
  42. */
  43. public static String desEncrypt(String source) throws Exception {
  44. DESKeySpec desKeySpec = getDesKeySpec(source);
  45. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  46. SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  47. cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV);
  48. return byte2hex(
  49. cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
  50. }
  51. /**
  52. * 对称解密
  53. */
  54. public static String desDecrypt(String source) throws Exception {
  55. byte[] src = hex2byte(source.getBytes(StandardCharsets.UTF_8));
  56. DESKeySpec desKeySpec = getDesKeySpec(source);
  57. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  58. SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  59. cipher.init(Cipher.DECRYPT_MODE, secretKey, IV);
  60. byte[] retByte = cipher.doFinal(src);
  61. return new String(retByte);
  62. }
  63. private static String byte2hex(byte[] inStr) {
  64. String stmp;
  65. StringBuilder out = new StringBuilder(inStr.length * 2);
  66. for (byte b : inStr) {
  67. stmp = Integer.toHexString(b & 0xFF);
  68. if (stmp.length() == 1) {
  69. // 如果是0至F的单位字符串,则添加0
  70. out.append("0").append(stmp);
  71. } else {
  72. out.append(stmp);
  73. }
  74. }
  75. return out.toString();
  76. }
  77. private static byte[] hex2byte(byte[] b) {
  78. int size = 2;
  79. if ((b.length % size) != 0){
  80. throw new IllegalArgumentException("长度不是偶数");
  81. }
  82. byte[] b2 = new byte[b.length / 2];
  83. for (int n = 0; n < b.length; n += size) {
  84. String item = new String(b, n, 2);
  85. b2[n / 2] = (byte) Integer.parseInt(item, 16);
  86. }
  87. return b2;
  88. }
  89. }