开门见山直接贴上代码

.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. Spring注解驱动(上)

    记录常用的spring注解 1.@Configuration 和 @Bean spring中可以使用xml 的方式进行配置, 也可以使用 @ Configuration 来指定一个类为配置类, 并使用 ...

  2. SDOI2019 R2退役记

    还是退役了呀 Day -1 早上loli发了套题结果啥都不会 之后胡爷爷就秒了道数据结构 不过也没什么人做,于是全机房都在愉快的划水 下午来机房打了场luogu的\(rated\)赛,还是啥都不会 之 ...

  3. vue生成条形码/二维码/带logo二维码

    条形码:https://blog.csdn.net/dakache11/article/details/83749410 //安装 cnpm install @xkeshi/vue-barcode / ...

  4. CAS添加验证码功能

    1.  cas.war 下面的web-inf/web.xml  lib添加  kaptcha.jar kaptcha.jar通过maven获取 <dependency> <group ...

  5. hiveUDF的使用

    在此自己总结下UDF的用法 1.首先最简单的UDF(普通用java扩充函数的方式,大多数简便函数可以用这个函数来实现,返回单个字段),其加强版UDGF据说对map一类数据类型有更好兼容,实现上略复杂 ...

  6. 测试是否是移动端,是否是iphone,是否是安卓

    function isMobile(){ return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(na ...

  7. 性能压测中的SLA,你知道吗?

    本文是<Performance Test Together>(简称PTT)系列专题分享的第6期,该专题将从性能压测的设计.实现.执行.监控.问题定位和分析.应用场景等多个纬度对性能压测的全 ...

  8. DBMS的四大特性

  9. Spring注解驱动开发(一)-----组件注册

    注册bean xml方式 1.beans.xml-----很简单,里面注册了一个person bean <?xml version="1.0" encoding=" ...

  10. apt-get正在等待报头(waiting for headers)

    可能的解决方法 1. 删除/var/cache/apt/archives/下的所有文件.可能是上次没有成功导致遗留了部分文件. 2. 如果使用的是代理,需要检查DNS.如果机器不能连接DNS服务器,要 ...