AES工具类
package com.house.common.utils;
import com.house.common.exception.UtilException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* java使用AES加密解密 AES-128-ECB加密,与mysql数据库AES加密算法通用
*
* 数据库AES加密解密方式如下
* -- 加密
* SELECT to_base64(AES_ENCRYPT('password','1Ve2t5G%X0uopC81'));
* -- 解密
* SELECT AES_DECRYPT(from_base64('8G9m8VNJZctBNIyz9swKQw=='),'1Ve2t5G%X0uopC81');
*
* @author Joye
*
*/
@Component
public class AesUtil {
private static Logger log = LoggerFactory.getLogger(AesUtil.class);
/**
* 对personKey加密
*/
private static final String AES_PERSON_KEY_SECURITY_KEY = "pisnyMyZYXuCNcRd";
/**
* 加解密密钥, 外部可以
*/
private static String key = "4%YkW!@g5LGcf9Ut";
/**
* 算法/加密模式/填充方式
*/
private static String algorithmstr = "AES/ECB/PKCS5Padding";
/**
* 编码方式
*/
private static String urlEncoding = "UTF-8";
public static String getKey() {
return key;
}
/**
* 校验字符串或key是否合法 && 初始化
* @param str 需要验证的字符串
* @param key 密钥
*/
private static boolean verify(String str, String key){
int keyLength = 16;
if (str == null || (key.length() != keyLength) ){
return false;
}
return true;
}
/**
* 加密
*
* @param str 需要加密的字符串ee
* @param key 密钥
* @return 加密内容
*/
public static String encrypt(String str, String key) {
if (StringUtils.isEmpty(key)) {
throw new UtilException("key不能为空");
}
try {
boolean flag = verify(str, key);
if (!flag){
return null;
}
// "算法/模式/补码方式"
Cipher cipher = Cipher.getInstance(algorithmstr);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(urlEncoding), "AES"));
byte[] encrypted = cipher.doFinal(str.getBytes(urlEncoding));
// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
return new BASE64Encoder().encode(encrypted);
} catch (Exception ex) {
log.error("加密初始化失败");
return null;
}
}
/**
* 解密
*
* @param str 需要解密的字符串
* @param key 密钥
* @return 解密后内容
*/
public static String decrypt(String str, String key) {
System.out.println(str+"-----------"+key);
if (StringUtils.isEmpty(key)) {
throw new UtilException("key不能为空");
}
try {
boolean flag = verify(str, key);
if (!flag){
return null;
}
Cipher cipher = Cipher.getInstance(algorithmstr);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(urlEncoding), "AES"));
// 先用base64解密
byte[] encrypted = new BASE64Decoder().decodeBuffer(str);
byte[] original = cipher.doFinal(encrypted);
return new String(original, urlEncoding);
} catch (Exception ex) {
log.error("解密初始化失败");
return null;
}
}
/**
* 加密
*
* @param str 需要加密的字符串
* @return 加密
*/
public static String encrypt(String str) {
return encrypt(str,key);
}
/**
* 解密
* @param str 需要解密的字符串
* @return 解密
*/
public static String decrypt(String str) {
return decrypt(str,key);
}
/**
* 查询的时候对某些字段解密
*
* @param str 需要解密的字符串
* @return 解密内容
*/
public static String aesDecrypt(String str) {
if (StringUtils.isBlank(str)) {
return " ";
}
return " AES_DECRYPT(from_base64(" + str + ")," + "'" + key + "')";
}
/**
* 对personKey加密
*
* @param personKey 密钥
* @return 加密内容
*/
public static String encryptPersonKey(String personKey) {
return AesUtil.encrypt(personKey, AES_PERSON_KEY_SECURITY_KEY);
}
/**
* 对personKey解密
*
* @param personKey 密钥
* @return 解密内容
*/
public static String decryptPersonKey(String personKey) {
return AesUtil.decrypt(personKey, AES_PERSON_KEY_SECURITY_KEY);
}
}
实现PasswordEncoder
package com.house.framework.config;
import com.house.common.utils.AesUtil;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* 重写AES解密方式
*/
public class MyPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence charSequence) {
return AesUtil.encrypt((String)charSequence);
}
@Override
public boolean matches(CharSequence charSequence, String s) {
return charSequence.equals(AesUtil.decrypt(s));
}
}
修改configure方法
/**
* AES实现
*/
@Bean
public MyPasswordEncoder AesPasswordEncoder(){
return new MyPasswordEncoder();
}
/**
* 身份认证接口
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService).passwordEncoder(AesPasswordEncoder());
}