提交 f5c2ec91 authored 作者: liyinqiao's avatar liyinqiao

增加AES加解密类型

上级 3d1f8258
...@@ -31,11 +31,6 @@ ...@@ -31,11 +31,6 @@
<artifactId>estateplat-common</artifactId> <artifactId>estateplat-common</artifactId>
<version>1.2.4-SNAPSHOT</version> <version>1.2.4-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-cas-client</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
......
package cn.gtmap.bdcdj.core.encrypt.executor;
import cn.gtmap.bdcdj.core.encrypt.CryptProperties;
import cn.gtmap.bdcdj.core.encrypt.enums.SaltsEnum;
import cn.gtmap.bdcdj.core.encrypt.utils.AESutil;
import cn.gtmap.bdcdj.core.encrypt.utils.CryptUtil;
import org.apache.commons.lang3.StringUtils;
/**
* AES CBC 加密解密
*
*/
public class AesCryptExecutor implements CryptExecutor {
@Override
public String encrypt(CryptProperties cryptProperties, String value) {
if (StringUtils.isBlank(value)) {
return value;
}
if (StringUtils.startsWithIgnoreCase(value, SaltsEnum.SALTS.getSalt())) {
return value;
}
String separatorChars = CryptUtil.getSeparatorChars(value);
if (StringUtils.isNotEmpty(separatorChars)) {
String[] paramArray = StringUtils.split(value, separatorChars);
StringBuilder encryptParam = new StringBuilder();
for (String temp : paramArray) {
encryptParam.append(CryptUtil.matchHex(temp) ? temp : AESutil.encodeToHexString(temp)).append(separatorChars);
}
return StringUtils.substring(encryptParam.toString(), 0, encryptParam.length() - 1);
}
return AESutil.encodeToHexString(value);
}
@Override
public String decrypt(CryptProperties cryptProperties, String param) {
if (StringUtils.isBlank(param)) {
return param;
}
try {
String separatorChars = CryptUtil.getSeparatorChars(param);
if (StringUtils.isNotEmpty(separatorChars)) {
String[] paramArray = StringUtils.split(param, separatorChars);
StringBuilder decryptParam = new StringBuilder();
for (String temp : paramArray) {
decryptParam.append(CryptUtil.matchHex(temp) ? AESutil.decodeFromHexString(temp) : temp).append(separatorChars);
}
return StringUtils.substring(decryptParam.toString(), 0, decryptParam.length() - 1);
}
if (CryptUtil.matchHex(param)) {
return AESutil.decodeFromHexString(param);
}
} catch (Exception e) {
e.printStackTrace();
}
return param;
}
}
...@@ -17,7 +17,7 @@ public class CryptExecutorFactory { ...@@ -17,7 +17,7 @@ public class CryptExecutorFactory {
} }
private static void registerCryptExcutor() { private static void registerCryptExcutor() {
// cryptExecutorMap.put(CryptType.AES_CBC, new AesCryptExecutor()); cryptExecutorMap.put(CryptType.AES_CBC, new AesCryptExecutor());
// cryptExecutorMap.put(CryptType.GM_SM2, new GmSm2CryptExecutor()); // cryptExecutorMap.put(CryptType.GM_SM2, new GmSm2CryptExecutor());
cryptExecutorMap.put(CryptType.GM_SM4, new GmSm4CryptExecutor()); cryptExecutorMap.put(CryptType.GM_SM4, new GmSm4CryptExecutor());
} }
......
package cn.gtmap.bdcdj.core.encrypt.utils;
import cn.gtmap.bdcdj.core.encrypt.enums.SaltsEnum;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
/**
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @version 1.0 2020-4-16
* @description aes 加密解密工具类
*/
public class AESutil {
private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
private static final String KEY_ALGORITHM = "AES";
private static final String base64Key = "1B0EEF110D358E31409FA45A38A1E20D";
private static byte[] iv = {0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF,
0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF};
/**
* AES 密钥生成方法
*
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
*/
public static String initKey() {
KeyGenerator kg = null;
try {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
kg.init(128);
SecretKey secretKey = kg.generateKey();
return parseByte2HexStr(secretKey.getEncoded());
}
/**
* AES 加密方法
*
* @param data 待加密字符串
* @return 加密后的字节数组
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
*/
public static byte[] encrypt(String data) {
Cipher cipher = null;
try {
cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
byte[] key = parseHexStr2Byte(base64Key);
Key k = new SecretKeySpec(key, KEY_ALGORITHM);
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, k, paramSpec);
return cipher.doFinal(data.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* AES 解密方法
*
* @param data 待解密字节数组
* @return 解密后的字节数组
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
*/
public static byte[] decrypt(byte[] data) {
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
byte[] key = parseHexStr2Byte(base64Key);
Key k = new SecretKeySpec(key, KEY_ALGORITHM);
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, k, paramSpec);
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* AES 加密方法 加密成16进制字符串
* 为增加破解难度 做加盐处理
*
* @param data 待加密字符串
* @return 加密字符串16进制字符串形式输出
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @since :14:06 2020-5-18
*/
public static String encodeToHexString(String data) {
return SaltsEnum.SALTS.getSalt() + Hex.encodeHexString(encrypt(data));
}
/**
* AES 解密方法 将加密后的16进制字符串 解密成明文
*
* @param data 待解密的加密字符串
* @return 解密后的明文字符串
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @since :14:09 2020-5-18
*/
public static String decodeFromHexString(String data) throws DecoderException {
if (StringUtils.startsWithIgnoreCase(data, SaltsEnum.SALTS.getSalt())) {
if (StringUtils.endsWith(data, Constants.DENG)) {
//存在一种情况,在sql语句对证件号加 “等”字 截取去除前面拼接的 与后边的等字
data = StringUtils.removeEnd(StringUtils.removeStartIgnoreCase(data, SaltsEnum.SALTS.getSalt()), Constants.DENG);
data = new String(decrypt(Hex.decodeHex(data.toCharArray())));
return data + Constants.DENG;
} else {
return new String(decrypt(Hex.decodeHex((StringUtils.removeStartIgnoreCase(data, SaltsEnum.SALTS.getSalt())).toCharArray())));
}
}
return new String(decrypt(Hex.decodeHex(data.toCharArray())));
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
package cn.gtmap.bdcdj.core.encrypt.utils;
/**
* 静态常量类
*
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @version 1.0 2020-5-18
*/
public class Constants {
private Constants() {
}
/**
* systemVersion 配置的默认标准版本
*/
public static final String SYSTEM_VERSION_STANDARD = "standard";
/**
* 加密的加盐处理
*/
public static final String SALTS = "apmtg04b";
/**
* GM Sm2 salts
*/
public static final String GM_SM2_SALTS = "b2a1fdd";
/**
* gm sm4 salts
*/
public static final String GM_SM4_SALTS = "4mspamtg";
/**
* 等
*/
public static final String DENG = "等";
/**
* 动态参数 加密参数 识别标识
*/
public static final String DYNAMIC_ENCRYPTKEYS = "encryptKeys";
/**
* 动态参数 解密参数 识别标识
*/
public static final String DYNAMIC_DECRYPTKEYS = "decryptKeys";
/**
* 国密SM2 私钥
*/
public static final String GM2PSK = "AKPflclLAvlxbX8yb8okfknP6FKF4Cq2nCYXRodZF7ZV";
/**
* 国密SM2 公钥
*/
public static final String GM2PUK = "Ak3tRLNHcQCg4a1NTXldbx1c9RVE5SIUW17ETKgFq3Wa";
/**
* 国密 Sm4 秘钥
*/
public static final String GM_SM4_ECK = "645B0A010D939BD614A620BCAF645470";
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论