java aes应用场景_Java加密AES算法及spring中应用

1.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 classAESUtils {//实例化密钥

private staticKey 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(128, random);

key=kgen.generateKey();

kgen= null;

}catch(Exception e) {throw newRuntimeException(e);

}

}/**

* @description: AES对称加密字符串,并通过Jdk自带Base64转换为ASCII

* @author: Administrator

* @date: 2017年11月7日 上午9:37:48

* @param str

* @return*/

public staticString 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);returnBase64.getEncoder().encodeToString(doFinal);

}catch(Exception e) {throw newRuntimeException(e);

}

}/**

* @description: 对AES加密字符串进行解密

* @author: maojialong

* @date: 2017年11月7日 上午10:14:00

* @param str

* @return*/

public staticString 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 newString(doFinal, CHARSETNAME);

}catch(Exception e) {throw newRuntimeException(e);

}

}

}2.自定义配置文件解析类

import java.util.List;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;/**

* @description: 自定义AES解密

* @author: maojialong

* @date: 2017年11月7日 上午10:26:50*/

public classEncodeAESPlaceholderConfigurer extends PropertyPlaceholderConfigurer {private ListencodeProperties;/**

* @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*/@OverrideprotectedString convertProperty(String propertyName, String propertyValue) {if(encodeProperties != null &&encodeProperties.contains(propertyName)) {

propertyValue=AESUtils.getDecryptString(propertyValue);

}returnsuper.convertProperty(propertyName, propertyValue);

}public ListgetEncodeProperties() {returnencodeProperties;

}public void setEncodeProperties(ListencodeProperties) {this.encodeProperties =encodeProperties;

}

}3.配置文件中加载配置文件

classpath:conf/jdbc.properties

classpath:redis.properties

jdbc.url

jdbc.username

jdbc.password

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值