|
@@ -1,124 +0,0 @@
|
|
-package cn.com.qmth.examcloud.commons.util;
|
|
|
|
-
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Map.Entry;
|
|
|
|
-import java.util.Properties;
|
|
|
|
-import java.util.Set;
|
|
|
|
-
|
|
|
|
-import com.google.common.collect.Maps;
|
|
|
|
-import com.google.common.collect.Sets;
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- * 安全工具
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @date 2018年12月4日
|
|
|
|
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
|
- */
|
|
|
|
-public class SecurityUtil {
|
|
|
|
-
|
|
|
|
- private static final String RANDOM = "9451utb@91&vrmio!90iu!89F2QN1V ALAII!K33I*DFA^sA";
|
|
|
|
-
|
|
|
|
- private static final String ENCRYPTED_HEAD = "$encrypted";
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * main
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param args
|
|
|
|
- */
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
- String s = "";
|
|
|
|
- String secretKey = "5220";
|
|
|
|
- System.out.println(encrypt(s, secretKey));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 加密
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param s
|
|
|
|
- * @param secretKey
|
|
|
|
- */
|
|
|
|
- public static String encrypt(String s, String secretKey) {
|
|
|
|
- AES aes = new AES(secretKey + RANDOM);
|
|
|
|
- String encrypted = aes.encrypt(s);
|
|
|
|
- return encrypted;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 解密
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param s
|
|
|
|
- * @param secretKey
|
|
|
|
- */
|
|
|
|
- public static String decrypt(String s, String secretKey) {
|
|
|
|
- AES aes = new AES(secretKey + RANDOM);
|
|
|
|
- String decrypted = aes.decrypt(s);
|
|
|
|
- return decrypted;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 解密
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param props
|
|
|
|
- * @param secretKey
|
|
|
|
- */
|
|
|
|
- public static void decrypt(Properties props, String secretKey) {
|
|
|
|
- AES aes = new AES(secretKey + RANDOM);
|
|
|
|
- Map<String, String> map = Maps.newHashMap();
|
|
|
|
- Set<String> set = Sets.newHashSet();
|
|
|
|
- for (Entry<Object, Object> entry : props.entrySet()) {
|
|
|
|
- Object key = entry.getKey();
|
|
|
|
- Object value = entry.getValue();
|
|
|
|
- if (key instanceof String && value instanceof String) {
|
|
|
|
- if (((String) key).startsWith(ENCRYPTED_HEAD)) {
|
|
|
|
- String decrypted = aes.decrypt((String) value);
|
|
|
|
- map.put(((String) key).substring(ENCRYPTED_HEAD.length() + 1), decrypted);
|
|
|
|
- set.add((String) key);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for (String s : set) {
|
|
|
|
- props.remove(s);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for (Entry<String, String> entry : map.entrySet()) {
|
|
|
|
- props.put(entry.getKey(), entry.getValue());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 解密
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param props
|
|
|
|
- * @param secretKey
|
|
|
|
- */
|
|
|
|
- public static void decrypt(Map<String, String> props, String secretKey) {
|
|
|
|
- AES aes = new AES(secretKey + RANDOM);
|
|
|
|
- Map<String, String> map = Maps.newHashMap();
|
|
|
|
- Set<String> set = Sets.newHashSet();
|
|
|
|
- for (Entry<String, String> entry : props.entrySet()) {
|
|
|
|
- String key = entry.getKey();
|
|
|
|
- String value = entry.getValue();
|
|
|
|
- if (key.startsWith(ENCRYPTED_HEAD)) {
|
|
|
|
- String decrypted = aes.decrypt((String) value);
|
|
|
|
- map.put(key.substring(ENCRYPTED_HEAD.length() + 1), decrypted);
|
|
|
|
- set.add(key);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for (String s : set) {
|
|
|
|
- props.remove(s);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for (Entry<String, String> entry : map.entrySet()) {
|
|
|
|
- props.put(entry.getKey(), entry.getValue());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|