开门见山直接贴上代码

.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. java基础之BigInteger

    BigInteger类概述可以让超过Integer范围内的数据进行运算 构造方法 public BigInteger(String val) 成员方法: public BigInteger add(B ...

  2. 2019-1-10-WPF-使用-RenderTargetBitmap-快速截图出现-COMException-提示

    title author date CreateTime categories WPF 使用 RenderTargetBitmap 快速截图出现 COMException 提示 lindexi 201 ...

  3. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  4. DataLakeAnalytics: 解析IP地址对应的国家城市地址的能力

    Data Lake Analytics 作为云上数据处理的枢纽,最近加入了通过IP地址查找对应的国家.省份.城市.ISP的函数, 今天带大家体验一下. 函数详细介绍 本次一共添加了下面这些函数: ip ...

  5. 技术 | TypeScript

    技术 | TypeScript   第一次接触TypeScript还是和一帮兄弟在居民楼里撸每日优鲜App的时候,没有想过那么多,只想可以快速实现和快速落地,于是我们选择ionic这个Hybrid框架 ...

  6. PAT甲级——A1063 Set Similarity

    Given two sets of integers, the similarity of the sets is defined to be /, where N​c​​ is the number ...

  7. 014-unittest扩展

    unittest扩展 1. unittest框架里面---verbosity设置这里的verbosity是一个选项,表示测试结果的信息复杂度,有三个值: 0 (静默模式): 你只能获得总的测试用例数和 ...

  8. 轻松搞定 JS 的this、call和apply

    年前最后一篇文章,提前祝大家春节快乐.对了,还有369就要2018年了,提前祝大家2018年春节快乐. .. .vr qBMBBBMBMY 8BBBBBOBMBMv iMBMM5vOY:BMBBv . ...

  9. yum与rpm常用选项

    rpm常用的命令组合: rpm 1.对系统中已安装软件的查询-q:查询系统已安装的软件-qa:查询系统所有已安装包-qf:查询一个已经安装的文件属于哪个软件包-ql:查询已安装软件包都安装到何处-qi ...

  10. numpy库数组属性查看:类型、尺寸、形状、维度

    import numpy as np   q = np.array([1,2,3,4],dtype=np.complex128)    print("数据类型",type(q))  ...