开门见山直接贴上代码

.AESUtil加密解密工具类
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; /**
* @description: AES加密工具类
* @author: maojialong
* @date: 2017年11月7日 上午10:11:02
*/
public class AESUtils { //实例化密钥
private static Key key; //原始密钥
private static String KEY_STR = "my-springmvc-2017-11-07"; //编码
private static String CHARSETNAME = "UTF-8"; //密钥算法
private static String KEY_ALGORITHM = "AES"; //加密-解密算法 / 工作模式 / 填充方式
private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; /**
* 初始化key
*/
static {
try {
KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY_STR.getBytes());
kgen.init(, random);
key = kgen.generateKey();
kgen = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
} /**
* @description: AES对称加密字符串,并通过Jdk自带Base64转换为ASCII
* @author: Administrator
* @date: 2017年11月7日 上午9:37:48
* @param str
* @return
*/
public static String getEncryptString(String str) {
try {
byte[] bytes = str.getBytes(CHARSETNAME);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return Base64.getEncoder().encodeToString(doFinal);
} catch (Exception e) {
throw new RuntimeException(e);
}
} /**
* @description: 对AES加密字符串进行解密
* @author: maojialong
* @date: 2017年11月7日 上午10:14:00
* @param str
* @return
*/
public static String getDecryptString(String str) {
try {
byte[] bytes = Base64.getDecoder().decode(str);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return new String(doFinal, CHARSETNAME);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} .自定义配置文件解析类
import java.util.List; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /**
* @description: 自定义AES解密
* @author: maojialong
* @date: 2017年11月7日 上午10:26:50
*/
public class EncodeAESPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private List<String> encodeProperties; /**
* @description: 重写convertProperty方法,通过encodeProperties判断是否是经过AES加密
* @author: maojialong
* @date: 2017年11月7日 上午10:27:24
* @param propertyName
* @param propertyValue
* @return
* @see org.springframework.beans.factory.config.PropertyResourceConfigurer#convertProperty(java.lang.String, java.lang.String)
* TODO
*/
@Override
protected String convertProperty(String propertyName, String propertyValue) {
if(encodeProperties != null && encodeProperties.contains(propertyName)) {
propertyValue = AESUtils.getDecryptString(propertyValue);
}
return super.convertProperty(propertyName, propertyValue);
} public List<String> getEncodeProperties() {
return encodeProperties;
} public void setEncodeProperties(List<String> encodeProperties) {
this.encodeProperties = encodeProperties;
}
} .配置文件中加载配置文件
<!-- 自定义AES加密解密 -->
<bean id="propertyConfigurer" class="util.EncodeAESPlaceholderConfigurer">
<!-- 配置文件地址 -->
<property name="locations">
<list>
<value>classpath:conf/jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
<!-- 配置需要解密的配置项 -->
<property name="encodeProperties">
<array>
<value>jdbc.url</value>
<value>jdbc.username</value>
<value>jdbc.password</value>
</array>
</property>
</bean>

Java加密AES算法及spring中应用的更多相关文章

  1. java加密解密算法位运算

    一.实例说明 本实例通过位运算的异或运算符 “ ^ ” 把字符串与一个指定的值进行异或运算,从而改变每个字符串中字符的值,这样就可以得到一个加密后的字符串.当把加密后的字符串作为程序输入内容,异或运算 ...

  2. C#与Java互通AES算法加密解密

    /// <summary>AES加密</summary> /// <param name="text">明文</param> /// ...

  3. .NET与Java互通AES算法加密解密

    /// <summary>AES加密</summary> /// <param name="text">明文</param> /// ...

  4. Java 加密 AES 对称加密算法

    版权声明:本文为博主原创文章,未经博主允许不得转载. [AES] 一种对称加密算法,DES的取代者. 加密相关文章见:Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES R ...

  5. Java使用AES算法进行加密解密

    一.加密 /** * 加密 * @param src 源数据字节数组 * @param key 密钥字节数组 * @return 加密后的字节数组 */ public static byte[] En ...

  6. java普通类如何得到spring中的bean类

    在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方法.例如: ApplicationContext ...

  7. 【经验总结】Java在ACM算法竞赛编程中易错点

    一.Java之ACM易错点 1. 类名称必须采用public class Main方式命名 2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非 ...

  8. 使用java实现AES算法的加解密(亲测可用)

    话不多说,直接上代码 import javax.crypto.Cipher;   import javax.crypto.spec.IvParameterSpec; import javax.cryp ...

  9. AES算法在Python中的使用

    Python有很多开源库,使用AES等加密算法时可以找对应的开源库.我记录一下安装方法: (1)下载开源库pycrypto 下载地址:https://pypi.python.org/pypi/pycr ...

随机推荐

  1. C# 判断当前请求是GET、还是POST ?

    方法一: HttpContext.Current.Request.RequestType == "POST"   //当前请求为:POST 方法二: if(Request.Serv ...

  2. Ajax请求参数传到后台为空

    1.编码格式 $.ajax({ method:'POST', url:'/midservice/studentAction/addStudent', data:$.toJSON(userDate), ...

  3. JS 日期比较

    Js 日期比较方法 第一种方式 function compareDate(s1,s2){ return ((new Date(s1.replace(/-/g,"\/")))> ...

  4. CodeChef TRIPS-Children Trips 树上分块

    参考文献国家集训队2015论文<浅谈分块在一类在线问题的应用>-邹逍遥 题目链接 题目大意 一棵n个节点的树,树的每条边长度为1或2,每次询问x,y,z. 要求输出从x开始走,每次只能走到 ...

  5. Java中将字符串转为驼峰格式

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:Java中将字符串转为驼峰格式: 使用CaseUtils 对Java字符串进行转换为驼峰格式: CaseUtils.toCamelCas ...

  6. object and namespace

    http://effbot.org/zone/python-objects.htm 几点总结: (1) 类的基本属性 . id, returned by id(obj) . type, returne ...

  7. pip报错ImportError: cannot import name main

    编辑pip sudo gedit /usr/bin/pip 修改pip文件: 源文件 from pip import main if __name__ == '__main__': sys.exit( ...

  8. leyou_06_Nginx的自启

    1.在linux系统的/etc/init.d/目录下创建nginx文件 vim /etc/init.d/nginx 添加以下内容 #!/bin/sh # # nginx - this script s ...

  9. notes 摘自陶哲轩演讲

    摘自陶哲轩演讲http://www.youku.com/playlist_show/id_5267259.htmlA frog in a well 井底之蛙 Aristotle        亚里士多 ...

  10. HDFS常用Java API