SecurityUtil.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * FileName:SecurityUtil.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util;
  20. import cn.com.lzt.common.util.coder.*;
  21. import cn.com.lzt.common.util.security.BASE64Encoder;
  22. import java.util.Map;
  23. /**
  24. * 数据加密辅助类(默认编码UTF-8)
  25. *
  26. * @author chermit
  27. */
  28. public final class SecurityUtil {
  29. private SecurityUtil() {
  30. }
  31. /**
  32. * 默认算法密钥
  33. */
  34. private static final byte[] ENCRYPT_KEY = { -81, 0, 105, 7, -32, 26, -49, 88 };
  35. public static final String CHARSET = "UTF-8";
  36. /**
  37. * BASE64解码
  38. *
  39. * @param key
  40. * @return
  41. * @throws Exception
  42. */
  43. public static final byte[] decryptBASE64(String key) {
  44. try {
  45. return new BASE64Encoder().decode(key);
  46. } catch (Exception e) {
  47. throw new RuntimeException("解密错误,错误信息:", e);
  48. }
  49. }
  50. /**
  51. * BASE64编码
  52. *
  53. * @param key
  54. * @return
  55. * @throws Exception
  56. */
  57. public static final String encryptBASE64(byte[] key) {
  58. try {
  59. return new BASE64Encoder().encode(key);
  60. } catch (Exception e) {
  61. throw new RuntimeException("加密错误,错误信息:", e);
  62. }
  63. }
  64. /**
  65. * 数据解密,算法(DES)
  66. *
  67. * @param cryptData
  68. * 加密数据
  69. * @return 解密后的数据
  70. */
  71. public static final String decryptDes(String cryptData) {
  72. return decryptDes(cryptData, ENCRYPT_KEY);
  73. }
  74. /**
  75. * 数据加密,算法(DES)
  76. *
  77. * @param data
  78. * 要进行加密的数据
  79. * @return 加密后的数据
  80. */
  81. public static final String encryptDes(String data) {
  82. return encryptDes(data, ENCRYPT_KEY);
  83. }
  84. /**
  85. * 基于MD5算法的单向加密
  86. *
  87. * @param strSrc
  88. * 明文
  89. * @return 返回密文
  90. */
  91. public static final String encryptMd5(String strSrc) {
  92. String outString = null;
  93. try {
  94. outString = encryptBASE64(MDCoder.encodeMD5(strSrc.getBytes(CHARSET)));
  95. } catch (Exception e) {
  96. throw new RuntimeException("加密错误,错误信息:", e);
  97. }
  98. return outString;
  99. }
  100. /**
  101. * SHA加密
  102. *
  103. * @param data
  104. * @return
  105. * @throws Exception
  106. */
  107. public static final String encryptSHA(String data) {
  108. try {
  109. return encryptBASE64(SHACoder.encodeSHA256(data.getBytes(CHARSET)));
  110. } catch (Exception e) {
  111. throw new RuntimeException("加密错误,错误信息:", e);
  112. }
  113. }
  114. /**
  115. * HMAC加密
  116. *
  117. * @param data
  118. * @return
  119. * @throws Exception
  120. */
  121. public static final String encryptHMAC(String data) {
  122. return encryptHMAC(data, ENCRYPT_KEY);
  123. }
  124. /**
  125. * 数据解密,算法(DES)
  126. *
  127. * @param cryptData
  128. * 加密数据
  129. * @return 解密后的数据
  130. */
  131. public static final String decryptDes(String cryptData, byte[] key) {
  132. String decryptedData = null;
  133. try {
  134. // 把字符串解码为字节数组,并解密
  135. decryptedData = new String(DESCoder.decrypt(decryptBASE64(cryptData), key));
  136. } catch (Exception e) {
  137. throw new RuntimeException("解密错误,错误信息:", e);
  138. }
  139. return decryptedData;
  140. }
  141. /**
  142. * 数据加密,算法(DES)
  143. *
  144. * @param data
  145. * 要进行加密的数据
  146. * @return 加密后的数据
  147. */
  148. public static final String encryptDes(String data, byte[] key) {
  149. String encryptedData = null;
  150. try {
  151. // 加密,并把字节数组编码成字符串
  152. encryptedData = encryptBASE64(DESCoder.encrypt(data.getBytes(), key));
  153. } catch (Exception e) {
  154. throw new RuntimeException("加密错误,错误信息:", e);
  155. }
  156. return encryptedData;
  157. }
  158. /**
  159. * HMAC加密
  160. *
  161. * @param data
  162. * @return
  163. * @throws Exception
  164. */
  165. public static final String encryptHMAC(String data, byte[] key) {
  166. try {
  167. return encryptBASE64(HmacCoder.encodeHmacSHA512(data.getBytes(CHARSET), key));
  168. } catch (Exception e) {
  169. throw new RuntimeException("加密错误,错误信息:", e);
  170. }
  171. }
  172. /**
  173. * RSA签名
  174. *
  175. * @param data
  176. * 原数据
  177. * @return
  178. */
  179. public static final String signRSA(String data, String privateKey) {
  180. try {
  181. return encryptBASE64(RSACoder.sign(data.getBytes(CHARSET), decryptBASE64(privateKey)));
  182. } catch (Exception e) {
  183. throw new RuntimeException("签名错误,错误信息:", e);
  184. }
  185. }
  186. /**
  187. * RSA验签
  188. *
  189. * @param data
  190. * 原数据
  191. * @return
  192. */
  193. public static final boolean verifyRSA(String data, String publicKey, String sign) {
  194. try {
  195. return RSACoder.verify(data.getBytes(CHARSET), decryptBASE64(publicKey), decryptBASE64(sign));
  196. } catch (Exception e) {
  197. throw new RuntimeException("验签错误,错误信息:", e);
  198. }
  199. }
  200. /**
  201. * 数据加密,算法(RSA)
  202. *
  203. * @param data
  204. * 数据
  205. * @return 加密后的数据
  206. */
  207. public static final String encryptRSAPrivate(String data, String privateKey) {
  208. try {
  209. return encryptBASE64(RSACoder.encryptByPrivateKey(data.getBytes(CHARSET), decryptBASE64(privateKey)));
  210. } catch (Exception e) {
  211. throw new RuntimeException("解密错误,错误信息:", e);
  212. }
  213. }
  214. /**
  215. * 数据解密,算法(RSA)
  216. *
  217. * @param cryptData
  218. * 加密数据
  219. * @return 解密后的数据
  220. */
  221. public static final String decryptRSAPublic(String cryptData, String publicKey) {
  222. try {
  223. // 把字符串解码为字节数组,并解密
  224. return new String(RSACoder.decryptByPublicKey(decryptBASE64(cryptData), decryptBASE64(publicKey)));
  225. } catch (Exception e) {
  226. throw new RuntimeException("解密错误,错误信息:", e);
  227. }
  228. }
  229. public static String encryptPassword(String password) {
  230. return encryptMd5(SecurityUtil.encryptSHA(password));
  231. }
  232. public static void main(String[] args) throws Exception {
  233. System.out.println(encryptDes("SHJR"));
  234. System.out.println(decryptDes("INzvw/3Qc4q="));
  235. System.out.println(encryptMd5("SHJR"));
  236. System.out.println(encryptSHA("1"));
  237. Map<String, Object> key = RSACoder.initKey();
  238. String privateKey = encryptBASE64(RSACoder.getPrivateKey(key));
  239. String publicKey = encryptBASE64(RSACoder.getPublicKey(key));
  240. System.out.println(privateKey);
  241. System.out.println(publicKey);
  242. String sign = signRSA("132", privateKey);
  243. System.out.println(sign);
  244. String encrypt = encryptRSAPrivate("132", privateKey);
  245. System.out.println(encrypt);
  246. String org = decryptRSAPublic(encrypt, publicKey);
  247. System.out.println(org);
  248. System.out.println(verifyRSA(org, publicKey, sign));
  249. // System.out.println("-------列出加密服务提供者-----");
  250. // Provider[] pro = Security.getProviders();
  251. // for (Provider p : pro) {
  252. // System.out.println("Provider:" + p.getName() + " - version:" +
  253. // p.getVersion());
  254. // System.out.println(p.getInfo());
  255. // }
  256. // System.out.println("");
  257. // System.out.println("-------列出系统支持的消息摘要算法:");
  258. // for (String s : Security.getAlgorithms("MessageDigest")) {
  259. // System.out.println(s);
  260. // }
  261. // System.out.println("-------列出系统支持的生成公钥和私钥对的算法:");
  262. // for (String s : Security.getAlgorithms("KeyPairGenerator")) {
  263. // System.out.println(s);
  264. // }
  265. }
  266. }