|
@@ -0,0 +1,147 @@
|
|
|
|
|
+package com.mooctest.crowd.site.util;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
|
|
+
|
|
|
|
|
+import javax.crypto.Cipher;
|
|
|
|
|
+import javax.crypto.SecretKey;
|
|
|
|
|
+import javax.crypto.SecretKeyFactory;
|
|
|
|
|
+import javax.crypto.spec.DESKeySpec;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.security.MessageDigest;
|
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
|
+import java.security.SecureRandom;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author: Diors.Po
|
|
|
|
|
+ * @Email: 171256175@qq.com
|
|
|
|
|
+ * @date 2019-08-12 01:32
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class EncryptionUtil {
|
|
|
|
|
+ // 定义 加密算法,可用 DES,DESede,Blowfish
|
|
|
|
|
+ private static String Algorithm = "DES";
|
|
|
|
|
+ private static String defaultKey = "witest.net";
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public static String encryptMD5(String str) {
|
|
|
|
|
+ String s = str;
|
|
|
|
|
+ if (s == null) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ String value = null;
|
|
|
|
|
+ MessageDigest md5 = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ md5 = MessageDigest.getInstance("MD5");
|
|
|
|
|
+ } catch (NoSuchAlgorithmException ex) {
|
|
|
|
|
+ log.error("", ex);
|
|
|
|
|
+ }
|
|
|
|
|
+ sun.misc.BASE64Encoder baseEncoder = new sun.misc.BASE64Encoder();
|
|
|
|
|
+ try {
|
|
|
|
|
+ value = baseEncoder.encode(md5.digest(s.getBytes("utf-8")));
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ }
|
|
|
|
|
+ return value;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String encryptDES(String content) throws Exception {
|
|
|
|
|
+ return encryptDES(content, defaultKey);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String decryptDES(String content) throws Exception {
|
|
|
|
|
+ return decryptDES(content, defaultKey);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Description 根据键值进行加密
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param data
|
|
|
|
|
+ * @param key 加密键byte数组
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String encryptDES(String data, String key) throws Exception {
|
|
|
|
|
+ byte[] bt = encryptDES(data.getBytes(), key.getBytes());
|
|
|
|
|
+ String strs = new BASE64Encoder().encode(bt);
|
|
|
|
|
+ return strs;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Description 根据键值进行解密
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param data
|
|
|
|
|
+ * @param key 加密键byte数组
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String decryptDES(String data, String key) throws IOException,
|
|
|
|
|
+ Exception {
|
|
|
|
|
+ if (data == null)
|
|
|
|
|
+ return null;
|
|
|
|
|
+ BASE64Decoder decoder = new BASE64Decoder();
|
|
|
|
|
+ byte[] buf = decoder.decodeBuffer(data);
|
|
|
|
|
+ byte[] bt = decryptDES(buf, key.getBytes());
|
|
|
|
|
+ return new String(bt);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Description 根据键值进行加密
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param data
|
|
|
|
|
+ * @param key 加密键byte数组
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ private static byte[] encryptDES(byte[] data, byte[] key) throws Exception {
|
|
|
|
|
+ // 生成一个可信任的随机数源
|
|
|
|
|
+ SecureRandom sr = new SecureRandom();
|
|
|
|
|
+
|
|
|
|
|
+ // 从原始密钥数据创建DESKeySpec对象
|
|
|
|
|
+ DESKeySpec dks = new DESKeySpec(key);
|
|
|
|
|
+
|
|
|
|
|
+ // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
|
|
|
|
|
+ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(Algorithm);
|
|
|
|
|
+ SecretKey securekey = keyFactory.generateSecret(dks);
|
|
|
|
|
+
|
|
|
|
|
+ // Cipher对象实际完成加密操作
|
|
|
|
|
+ Cipher cipher = Cipher.getInstance(Algorithm);
|
|
|
|
|
+
|
|
|
|
|
+ // 用密钥初始化Cipher对象
|
|
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
|
|
|
|
|
+
|
|
|
|
|
+ return cipher.doFinal(data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Description 根据键值进行解密
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param data
|
|
|
|
|
+ * @param key 加密键byte数组
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ private static byte[] decryptDES(byte[] data, byte[] key) throws Exception {
|
|
|
|
|
+ // 生成一个可信任的随机数源
|
|
|
|
|
+ SecureRandom sr = new SecureRandom();
|
|
|
|
|
+
|
|
|
|
|
+ // 从原始密钥数据创建DESKeySpec对象
|
|
|
|
|
+ DESKeySpec dks = new DESKeySpec(key);
|
|
|
|
|
+
|
|
|
|
|
+ // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
|
|
|
|
|
+ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(Algorithm);
|
|
|
|
|
+ SecretKey securekey = keyFactory.generateSecret(dks);
|
|
|
|
|
+
|
|
|
|
|
+ // Cipher对象实际完成解密操作
|
|
|
|
|
+ Cipher cipher = Cipher.getInstance(Algorithm);
|
|
|
|
|
+
|
|
|
|
|
+ // 用密钥初始化Cipher对象
|
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
|
|
|
|
|
+
|
|
|
|
|
+ return cipher.doFinal(data);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|