Java加密AES算法及spring中应用
开门见山直接贴上代码
.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中应用的更多相关文章
- java加密解密算法位运算
一.实例说明 本实例通过位运算的异或运算符 “ ^ ” 把字符串与一个指定的值进行异或运算,从而改变每个字符串中字符的值,这样就可以得到一个加密后的字符串.当把加密后的字符串作为程序输入内容,异或运算 ...
- C#与Java互通AES算法加密解密
/// <summary>AES加密</summary> /// <param name="text">明文</param> /// ...
- .NET与Java互通AES算法加密解密
/// <summary>AES加密</summary> /// <param name="text">明文</param> /// ...
- Java 加密 AES 对称加密算法
版权声明:本文为博主原创文章,未经博主允许不得转载. [AES] 一种对称加密算法,DES的取代者. 加密相关文章见:Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES R ...
- Java使用AES算法进行加密解密
一.加密 /** * 加密 * @param src 源数据字节数组 * @param key 密钥字节数组 * @return 加密后的字节数组 */ public static byte[] En ...
- java普通类如何得到spring中的bean类
在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方法.例如: ApplicationContext ...
- 【经验总结】Java在ACM算法竞赛编程中易错点
一.Java之ACM易错点 1. 类名称必须采用public class Main方式命名 2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非 ...
- 使用java实现AES算法的加解密(亲测可用)
话不多说,直接上代码 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.cryp ...
- AES算法在Python中的使用
Python有很多开源库,使用AES等加密算法时可以找对应的开源库.我记录一下安装方法: (1)下载开源库pycrypto 下载地址:https://pypi.python.org/pypi/pycr ...
随机推荐
- python学习笔记3.1_数据读取常用函数参数
一.read_table/read_csv常用函数参数 1.path:表明文件系统位置的字符串.url或文件型对象 2.sep或delimiter:用于分隔每行字段的字符序列或正则表达式 3.head ...
- python 将值相同的key分组的方法
方法一: 使用 itertools.groupby() rows = [ {'address': '5412 N CLARK ', 'date ': '07/12/2012 ’ }, {'addres ...
- es6 Promise 异步函数调用
开发很多的时候需要异步操作,常用的做法就是用回调函数,假如需要一连串的调用,并且后面一个调用依赖前一个返回的结果的时候,就得多层嵌套回调函数,比如下面这种情况: $('.animateEle').an ...
- java linkedlist和arraylist添加元素时性能比较
- Konig定理及证明
Konig定理 由匈牙利数学家柯尼希(D.Konig)于1913年首先陈述的定理. 定理的内容:在0-1矩阵中,1的最大独立集合最小覆盖包含的元素个数相同,等价地,二分图中的最大匹配数等于这个图中的最 ...
- 比特镇旅游(Tourist Attractions)【暴力+Bitset 附Bitset用法】
Online Judge:NOIP2016十连测第一场 T2 Label:暴力,Bitset 题目描述 在美丽的比特镇一共有n个景区,编号依次为1到n,它们之间通过若干条双向道路连接. Byteasa ...
- odoo:Actions
actions定义了系统对于用户的操作的响应:登录.按钮.选择项目等. 一:窗口action(ir.actions.act_window ) 最常用的action类型,用于将model的数据展示出来. ...
- 数位DP入门题——[hdu2089]不要62
数位DP是我的噩梦. 现在初三了,却没AC过数位DP的题目. 感觉数位DP都是毒瘤-- 题目 hdu不用登录也可以进去,所以就不把题目copy到这里来了. 题目大意 求区间[n,m][n,m][n,m ...
- csp-s模拟47 Emotional Flutter,Endless Fantasy题解
题面:https://www.cnblogs.com/Juve/articles/11558523.html A:Emotional Flutter 如果起点确定,那么我们后面走的点都是固定的,及mo ...
- SQL有意思的面试题
1.中软国际 SQL行转列 变成 --数据准备create table t_test( year int, month int, sale int, primary key (year, mon ...