DES即数据加密标准,是一种使用密钥加密的块算法。
目的:数据库账户密码我们一般会写在项目jdbc.properties配置文件中,而明文就有了很大的风险。本文记录在JAVA项目中编写DES工具类,以及对加密后的jdbc.properties解密。在SSM程序运行时读取spring-dao.xml配置文件时,自动替换jdbc.properties里的jdbc.username,jdbc.password DES密文进行数据库连接。
1.DESUtil 加密解密
package cn.richwit.o2o.util;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
/**
* @author 王小东
* @date 2020年7月9日 上午8:51:04
* @version 1.0
*/
public class DESUtil {
private static Key key;
//设置密钥key
private static String KEY_STR = "mykey";
private static String CHARSETNAME = "UTF-8";
private static String ALGORITHM = "DES";
static {
try {
//生成DES算法对象
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
//运用SHA1安全策略
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
//设置上密钥种子
secureRandom.