java spring中对properties属性文件加密及其解密
来源:互联网 发布:最全家庭网络个人投资 编辑:程序博客网 时间:2024/05/01 03:54
package com.system.utils;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.security.Key;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.Security;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;/** * <ul> * <li>Title:[DESEncryptUtil]</li> * <li>Description: [加密码解密类]</li> * <li>Copyright 2009 RoadWay Co., Ltd.</li> * <li>All right reserved.</li> * <li>Created by [Huyvanpull] [Jul 19, 2010]</li> * <li>Midified by [修改人] [修改时间]</li> * </ul> * * @version 1.0 */ public class DesEncryptUtil {/** * <ul> * <li>Description:[创建一个密钥]</li> * <li>Created by [Huyvanpull] [Jul 19, 2010]</li> * <li>Midified by [修改人] [修改时间]</li> * </ul> * * @return * @throws NoSuchAlgorithmException */ public static Key createKey() throws NoSuchAlgorithmException { Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1); KeyGenerator generator = KeyGenerator.getInstance("DES"); generator.init(new SecureRandom()); Key key = generator.generateKey(); return key; } /** * <ul> * <li>Description:[根据流得到密钥]</li> * <li>Created by [Huyvanpull] [Jul 19, 2010]</li> * <li>Midified by [修改人] [修改时间]</li> * </ul> * * @param is * @return */ public static Key getKey(InputStream is) { try { ObjectInputStream ois = new ObjectInputStream(is); return (Key) ois.readObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * <ul> * <li>Description:[对数据进行加密]</li> * <li>Created by [Huyvanpull] [Jul 19, 2010]</li> * <li>Midified by [修改人] [修改时间]</li> * </ul> * * @param key * @param data * @return */ private static byte[] doEncrypt(Key key, byte[] data) { try { Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] raw = cipher.doFinal(data); return raw; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * <ul> * <li>Description:[对数据进行解密]</li> * <li>Created by [Huyvanpull] [Jul 19, 2010]</li> * <li>Midified by [修改人] [修改时间]</li> * </ul> * * @param key * @param in * @return */ public static InputStream doDecrypt(Key key, InputStream in) { try { Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] tmpbuf = new byte[1024]; int count = 0; while ((count = in.read(tmpbuf)) != -1) { bout.write(tmpbuf, 0, count); tmpbuf = new byte[1024]; } in.close(); byte[] orgData = bout.toByteArray(); byte[] raw = cipher.doFinal(orgData); ByteArrayInputStream bin = new ByteArrayInputStream(raw); return bin; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } private static void test(String keyFilePath, String sourceFilePath, String operatorType) throws Exception { // 提供了Java命令使用该工具的功能 if (operatorType.equalsIgnoreCase("key")) { // 生成密钥文件 Key key = DesEncryptUtil.createKey(); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keyFilePath)); oos.writeObject(key); oos.close(); System.out.println("成功生成密钥文件" + keyFilePath); } else if (operatorType.equalsIgnoreCase("encrypt")) { // 对文件进行加密 File file = new File(sourceFilePath); FileInputStream in = new FileInputStream(file); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] tmpbuf = new byte[1024]; int count = 0; while ((count = in.read(tmpbuf)) != -1) { bout.write(tmpbuf, 0, count); tmpbuf = new byte[1024]; } in.close(); byte[] orgData = bout.toByteArray(); Key key = getKey(new FileInputStream(keyFilePath)); byte[] raw = DesEncryptUtil.doEncrypt(key, orgData); file = new File(file.getParent() + "\\en_" + file.getName()); FileOutputStream out = new FileOutputStream(file); out.write(raw); out.close(); System.out.println("成功加密,加密文件位于:" + file.getAbsolutePath()); } else if (operatorType.equalsIgnoreCase("decrypt")) { // 对文件进行解密 File file = new File(sourceFilePath); FileInputStream fis = new FileInputStream(file); Key key = getKey(new FileInputStream(keyFilePath)); InputStream raw = DesEncryptUtil.doDecrypt(key, fis); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] tmpbuf = new byte[1024]; int count = 0; while ((count = raw.read(tmpbuf)) != -1) { bout.write(tmpbuf, 0, count); tmpbuf = new byte[1024]; } raw.close(); byte[] orgData = bout.toByteArray(); file = new File(file.getParent() + "\\rs_" + file.getName()); FileOutputStream fos = new FileOutputStream(file); fos.write(orgData); System.out.println("成功解密,解密文件位于:" + file.getAbsolutePath()); } } /** * 测试..... * @param args * @throws Exception */ public static void main(String[] args) throws Exception { /** 生成KEY */ String operatorType = "key"; String keyFilePath = "/home/zer0Server/key.k"; DesEncryptUtil.test(keyFilePath, null, operatorType); /** 加密 */ operatorType = "encrypt"; String sourceFilePath = "/home/zer0Server/css.properties"; DesEncryptUtil.test(keyFilePath, sourceFilePath, operatorType); /** 解密 */ operatorType = "decrypt"; sourceFilePath = "/home/zer0Server/en_css.properties"; DesEncryptUtil.test(keyFilePath, sourceFilePath, operatorType); }}
DecryptPropertyPlaceholderConfigurer.Java
package com.system.utils;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.security.Key;import java.util.Properties;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;import org.springframework.core.io.Resource;import org.springframework.util.DefaultPropertiesPersister;import org.springframework.util.PropertiesPersister;public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {private Resource[] locations;private Resource keyLocation;private String fileEncoding;public void setKeyLocation(Resource keyLocation) {this.keyLocation = keyLocation;}public void setLocations(Resource[] locations) {this.locations = locations;}public void loadProperties(Properties props) throws IOException {if (this.locations != null) {PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();for (int i = 0; i < this.locations.length; i++) {Resource location = this.locations[i];if (logger.isInfoEnabled()) {logger.info("Loading properties file from " + location);}InputStream is = null;try {is = location.getInputStream();Key key = DesEncryptUtil.getKey(keyLocation.getInputStream());is = DesEncryptUtil.doDecrypt(key, is);if (fileEncoding != null) {propertiesPersister.load(props, new InputStreamReader(is, fileEncoding));} else {propertiesPersister.load(props, is);}} finally {if (is != null) {is.close();}}}}}}
配置文件:
<!-- 加密码属性文件 --> <bean id="myPropertyConfigurer" class="com.system.utils.DecryptPropertyPlaceholderConfigurer"> <property name="locations"> <list><value>classpath*:spring_config/jdbc_official.databaseinfo</value></list> </property> <property name="fileEncoding" value="UTF-8"/> <property name="keyLocation" value="classpath:spring_config/key.key" /> </bean>
代码下载地址:http://www.zuidaima.com/share/1781588957400064.htm
原文:http://www.blogjava.net/hwpok/archive/2010/07/19/326537.html
0 0
- java spring中对properties属性文件加密及其解密
- java spring中对properties属性文件加密及其解密
- java spring中对properties属性文件加密及其解密
- java spring中对properties属性文件加密及其解密
- java spring中对properties属性文件加密及其解密
- java spring中对properties属性文件加密及其解密
- java spring中对properties属性文件加密及其解密
- Spring对属性文件加密解密应用
- Spring对属性文件加密解密应用详解
- Spring读取properties文件加密解密
- Spring 管理下的web项目,对Properties文件重要参数的加密解密处理
- Spring 管理下的web项目,对Properties文件重要参数的加密解密处理(续)
- Spring对外部属性文件指定的某个属性进行加密、解密
- Spring对外部属性文件指定的某个属性进行加密、解密
- java文件加密|spring属性文件加密
- java对文件进行加密解密操作
- spring加密properties文件
- spring加密properties文件
- mysql二进制日志
- TCP的流量控制和拥塞控制机制
- [Android]当Activity启动模式为singleTask时的生命周期
- android studio添加butterknif插件
- [1] Window PowerShell DSC 学习系列----DSC的定义和2种架构模式
- java spring中对properties属性文件加密及其解密
- HTML转义字符汇总
- android----fragment学习中的listFragment
- 找数
- Rails配置项force_ssl与hsts
- 基于WWW的get请求Demo
- IE css3常用属性兼容性
- HTML5页面开发的基础性模板
- 328. Odd Even Linked List