crypt | |--annotation | |--DecryptFiled | |--EncryptFiled |--crypt | |--EncryptDecryptInterface |--domain | |--BaseInfo | |--SimpleDomain |--utils | |--MySqlUtils
/**
* Created by bright on 2017/2/22.
*
* @author :
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptFiled {
String value() default "";
}
自定义注解
/**
* Created by bright on 2017/2/22.
*
* @author :
*/
public interface EncryptDecryptInterface {
public <T> T encryptSelf();
public <T> T decryptSelf();
}
自定义接口
/**
* Created by bright on 2017/2/22.
*
* @author :
*/
@Component
public class MySqlUtils {
private static final String ENCRYPTTYPE= "AES";//加密方式
private static final String ENCODING = "UTF-8";//加密时编码
private static String MYSQLUTILSKEY = "aaa";//加密密盐
private static MySqlUtils mysqlUtils;//单例
private static Cipher encryptCipher ;//加密cipher
private static Cipher decryptChipher;//解密chipher
/**
* 该方法可用在spring项目中使用配置文件设置密盐,默认值为123
* @param key
*/
@Value("${mysql.column.crypt.key:123}")
public void setMysqlutilskey(String key){
MySqlUtils.MYSQLUTILSKEY = key;
}
/**
* encryptCipher、decryptChipher初始化
*/
public static void init(){
try {
encryptCipher = Cipher.getInstance(ENCRYPTTYPE);
decryptChipher = Cipher.getInstance(ENCRYPTTYPE);
encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(MYSQLUTILSKEY, ENCODING));
decryptChipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(MYSQLUTILSKEY, ENCODING));
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
}
}
/**
* 单例获取方法实现
* @return
*/
public synchronized static MySqlUtils getInstance(){
if(mysqlUtils == null){
mysqlUtils = new MySqlUtils();
init();
}
return mysqlUtils;
}
/**
* 加密算法
* @param encryptString
* @return
*/
public String mysqlAESEncrypt(String encryptString) {
try{
return new String(Hex.encodeHex(encryptCipher.doFinal(encryptString.getBytes(ENCODING)))).toUpperCase();
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
}
}
/**
* 解密算法
* @param decryptString
* @return
*/
public String mysqlAESDecrypt(String decryptString){
try {
return new String(decryptChipher.doFinal(Hex.decodeHex(decryptString.toCharArray())));
} catch (DecoderException nspe) {
throw new RuntimeException(nspe);
} catch (BadPaddingException nsae) {
throw new RuntimeException(nsae);
} catch (IllegalBlockSizeException ike) {
throw new RuntimeException(ike);
}
}
/**
* 产生mysql-aes_encrypt
* @param key 加密的密盐
* @param encoding 编码
* @return
*/
public static SecretKeySpec generateMySQLAESKey(final String key, final String encoding) {
try {
final byte[] finalKey = new byte[16];
int i = 0;
for(byte b : key.getBytes(encoding))
finalKey[i++] ^= b;
return new SecretKeySpec(finalKey, "AES");
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
MysqlUtils
/**
* Created by bright on 2017/2/22.
*
* @author :
*/
public class BaseInfo implements Cloneable, EncryptDecryptInterface {
/**
* 拷贝一个对象,并对新对象进行加密
* 该方法主要用在日志打印上,可防止原对象被加密而影响程序执行
* @param <T>
* @return
*/
public <T extends BaseInfo> T cloneAndEncrypt() {
T cloneT = null;
try {
cloneT = (T) this.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
if(cloneT !=null)
return cloneT.encryptSelf();
throw new RuntimeException("拷贝对象异常");
}
/**
* 重写clone方法
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
/**
* 实现自加密
*
* @param <T>
* @return
*/
public <T> T encryptSelf() {
Field[] declaredFields = this.getClass().getDeclaredFields();
try {
if (declaredFields != null && declaredFields.length > 0) {
for (Field field : declaredFields) {
if (field.isAnnotationPresent(EncryptFiled.class) && field.getType().toString().endsWith("String")) {
field.setAccessible(true);
String fieldValue = (String) field.get(this);
if (StringUtils.isNotEmpty(fieldValue)) {
field.set(this, MySqlUtils.getInstance().mysqlAESEncrypt(fieldValue));
}
field.setAccessible(false);
}
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return (T) this;
}
/**
* 实现自解密
*
* @param <T>
* @return
*/
public <T> T decryptSelf() {
Field[] declaredFields = this.getClass().getDeclaredFields();
try {
if (declaredFields != null && declaredFields.length > 0) {
for (Field field : declaredFields) {
if (field.isAnnotationPresent(DecryptFiled.class) && field.getType().toString().endsWith("String")) {
field.setAccessible(true);
String fieldValue = (String)field.get(this);
if(StringUtils.isNotEmpty(fieldValue)) {
field.set(this, MySqlUtils.getInstance().mysqlAESDecrypt(fieldValue));
}
}
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return (T) this;
}
}
BaseInfo
/**
* Created by bright on 2017/2/22.
*
* @author :
*/
public class SimpleDomain extends BaseInfo{
@EncryptFiled
@DecryptFiled
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
SimpleDomain
public class Client {
@Test
public void test(){
SimpleDomain sd = new SimpleDomain();//要进行加密解密的实体类
sd.setId("6029131988005021537");//注入身份证号
System.out.println(JSON.toJSONString(sd.encryptSelf()));//执行自加密后输出
System.out.println(JSON.toJSONString(sd.decryptSelf()));//执行自解密后输出
}
}
Client
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有