SecurityUtil.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.iamberry.nuonuo.util;
  2. import org.apache.commons.codec.binary.Base64;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import java.util.BitSet;
  6. /**
  7. * 安全类工具类
  8. * @author sdk.jss.com.cn
  9. * @version 2.0
  10. * @since jdk1.6
  11. */
  12. public class SecurityUtil {
  13. /**
  14. * 极速开票密钥
  15. */
  16. public final static String speedBilling = "F59E1B2970D64B8F926E9932E0A1A1DB";
  17. private static final BitSet UNRESERVED = new BitSet(256);
  18. private static final BitSet URLENCODER = new BitSet(256);
  19. private static final BitSet PUNCT = new BitSet(256);
  20. private static final BitSet USERINFO = new BitSet(256);
  21. private static final BitSet PATHSAFE = new BitSet(256);
  22. private static final BitSet URIC = new BitSet(256);
  23. private static final BitSet RESERVED = new BitSet(256);
  24. static
  25. {
  26. for (int i = 97; i <= 122; i++) {
  27. UNRESERVED.set(i);
  28. }
  29. for (int i = 65; i <= 90; i++) {
  30. UNRESERVED.set(i);
  31. }
  32. for (int i = 48; i <= 57; i++) {
  33. UNRESERVED.set(i);
  34. }
  35. UNRESERVED.set(95);
  36. UNRESERVED.set(45);
  37. UNRESERVED.set(46);
  38. UNRESERVED.set(42);
  39. URLENCODER.or(UNRESERVED);
  40. UNRESERVED.set(33);
  41. UNRESERVED.set(126);
  42. UNRESERVED.set(39);
  43. UNRESERVED.set(40);
  44. UNRESERVED.set(41);
  45. PUNCT.set(44);
  46. PUNCT.set(59);
  47. PUNCT.set(58);
  48. PUNCT.set(36);
  49. PUNCT.set(38);
  50. PUNCT.set(43);
  51. PUNCT.set(61);
  52. USERINFO.or(UNRESERVED);
  53. USERINFO.or(PUNCT);
  54. PATHSAFE.or(UNRESERVED);
  55. PATHSAFE.set(47);
  56. PATHSAFE.set(59);
  57. PATHSAFE.set(58);
  58. PATHSAFE.set(64);
  59. PATHSAFE.set(38);
  60. PATHSAFE.set(61);
  61. PATHSAFE.set(43);
  62. PATHSAFE.set(36);
  63. PATHSAFE.set(44);
  64. RESERVED.set(59);
  65. RESERVED.set(47);
  66. RESERVED.set(63);
  67. RESERVED.set(58);
  68. RESERVED.set(64);
  69. RESERVED.set(38);
  70. RESERVED.set(61);
  71. RESERVED.set(43);
  72. RESERVED.set(36);
  73. RESERVED.set(44);
  74. RESERVED.set(91);
  75. RESERVED.set(93);
  76. URIC.or(RESERVED);
  77. URIC.or(UNRESERVED);
  78. }
  79. /**
  80. * AES加密
  81. * @param sSrc 待加密字符串
  82. * @param sKey 加密Key值
  83. * @return
  84. */
  85. public static Object AESEncrypt(String sSrc, String sKey) {
  86. try {
  87. if (sKey == null) {
  88. System.out.print("Key为空");
  89. throw new Exception();
  90. }
  91. // 判断Key是否为16位
  92. if (sKey.length() != 16) {
  93. System.out.print("Key长度不等于16位");
  94. throw new Exception();
  95. }
  96. byte[] raw = sKey.getBytes("utf-8");
  97. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  98. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  99. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  100. byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
  101. return new Base64().encodeToString(encrypted);
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. return null;
  105. }
  106. }
  107. /**
  108. * AES 解密
  109. * @param sSrc
  110. * @param sKey
  111. * @return
  112. */
  113. public static Object AESDecrypt(String sSrc, String sKey) {
  114. try {
  115. if (sKey == null) {
  116. return null;
  117. }
  118. // 判断Key是否为16位
  119. if (sKey.length() != 16) {
  120. return null;
  121. }
  122. byte[] raw = sKey.getBytes("UTF-8");
  123. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  124. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  125. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  126. byte[] encrypted1 = new Base64().decode(sSrc);
  127. byte[] original = cipher.doFinal(encrypted1);
  128. String originalString = new String(original, "utf-8");
  129. return originalString;
  130. } catch (Exception ex) {
  131. ex.printStackTrace();
  132. return null;
  133. }
  134. }
  135. /**
  136. * 仅针对极速开票MD5加密
  137. * MD5加密
  138. * validator+F59E1B2970D64B8F926E9932E0A1A1DB,然后再进行MD5加密;
  139. * @param source
  140. * @return
  141. */
  142. public static String MD5Encrypt(String randomSixNum){
  143. String signidSource = new StringBuffer(randomSixNum).append(SecurityUtil.speedBilling).toString();
  144. String signidTarget = MD5Util.toMD5(signidSource);
  145. return signidTarget;
  146. }
  147. }