javaWeb项目中往往我们会把jdbc/redis等的属性配置放在properties文件中,但是又不想让非开发人员看到某些重要配置信息,所以需要对这样类似与用户名、密码等的字段进行加密,但是Spring管理项目启动时候只去读取,所以必须继承PropertyPlaceholderConfigurer 类来实现读取前的解密过程
加密类
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DesEncryptUtil
{
public static void main(String[] args) throws Exception
{
/** 生成KEY */
// createKey("../key");
Key key = getKey("../key");
/** 加密 */
byte[] result = doEncrypt(key, "");
// System.out.println(result);
// System.out.println(result.length);
String buf = "";
for (int i = 0; i < result.length; i++)
{
buf += result[i] + ",";
}
System.out.println(buf);
/** 解密 */
// System.out.println(doDecrypt(key, buf));
}
@SuppressWarnings("restriction")
public static void createKey(String keyFilePath) throws Exception
{
Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1);
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new