- 浏览: 355939 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
呆呆DE萌萌:
不可以吗?Timer里不是有一个指定首次运行时间firstDa ...
在Spring中使用 Java Timer 调度任务 -
accpchf:
不太明白,Error jvm都已经停止线程了,怎么还能转译?
深入探索 高效的Java异常处理框架。 -
bo_hai:
讲的详细。谢谢!
详解spring2.0的scope -
hpjianhua:
学习下...
线程池的实现 -
eltonto:
以后你可以来这里看看favicon在线转换
Tomcat中使用Favicon
编写支持加密属性文件的实现类
通过以上分析,我们设计一个支持加密属性文件的增强型PropertyPlaceholderConfigurer,其代码如所示:
代码清单 2
对locations指定的属性文件流数据进行额外的解密工作,解密后再装载到props中。比起PropertyPlaceholderConfigurer,我们只做了额外的一件事:装载前对属性资源进行解密。
在代码清单 2的③和④处,我们使用了一个DES解密的工具类对加密的属性文件流进行解密。
对文件进行对称加密的算法很多,一般使用DES对称加密算法,因为它速度很快,破解困难,DESEncryptUtil不但提供了DES解密功能,还提供了DES加密的功能,因为属性文件在部署前必须经常加密:
代码清单 3 加密解密工具类
解密工作主要涉及到两个类Cipher和Key,前者是加密器,可以通过init()方法设置工作模式和密钥,在这里,我们设置为解密工作模式:Cipher.DECRYPT_MODE。Cipher通过doFinal()方法对字节数组进行加密或解密。
通过以上分析,我们设计一个支持加密属性文件的增强型PropertyPlaceholderConfigurer,其代码如所示:
代码清单 2
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; //② 用于指定密钥文件 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); //⑤ 将解密后的属性流装载到props中 if(fileEncoding != null)...{ propertiesPersister.load(props, new InputStreamReader(is,fileEncoding)); }else...{ propertiesPersister.load(props ,is); } } finally ...{ if (is != null) is.close(); } } } } } }
对locations指定的属性文件流数据进行额外的解密工作,解密后再装载到props中。比起PropertyPlaceholderConfigurer,我们只做了额外的一件事:装载前对属性资源进行解密。
在代码清单 2的③和④处,我们使用了一个DES解密的工具类对加密的属性文件流进行解密。
对文件进行对称加密的算法很多,一般使用DES对称加密算法,因为它速度很快,破解困难,DESEncryptUtil不但提供了DES解密功能,还提供了DES加密的功能,因为属性文件在部署前必须经常加密:
代码清单 3 加密解密工具类
public class DESEncryptUtil ...{ 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; } 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); } } 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); } } 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); } } public static void main(String[] args) throws Exception {//提供了Java命令使用该工具的功能 if (args.length == 2 && args[0].equals("key")) {// 生成密钥文件 Key key = DESEncryptUtil.createKey(); ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(args[1])); oos.writeObject(key); oos.close(); System.out.println("成功生成密钥文件。"); } else if (args.length == 3 && args[0].equals("encrypt")) {//对文件进行加密 File file = new File(args[1]); 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(args[2])); 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 (args.length == 3 && args[0].equals("decrypt")) {//对文件进行解密 File file = new File(args[1]); FileInputStream fis = new FileInputStream(file); Key key = getKey(new FileInputStream(args[2])); 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()); } } }
解密工作主要涉及到两个类Cipher和Key,前者是加密器,可以通过init()方法设置工作模式和密钥,在这里,我们设置为解密工作模式:Cipher.DECRYPT_MODE。Cipher通过doFinal()方法对字节数组进行加密或解密。
发表评论
-
基于约定的Spring MVC扩展
2009-11-05 12:12 3415闲来无事翻了下以前写的项目,发现一个还算不错的东西 ... -
关于OpenSessionInViewFilter
2008-03-04 16:05 1474用了OpenSessionInViewFilter之后,程序开 ... -
Spring 事务简化配置
2008-03-02 19:44 1182在 spring 中, 事务管理一般是通过声明一个 txPro ... -
详解spring2.0的scope
2008-02-25 19:14 9497如何使用spring的作用域: <bean id=& ... -
扩展Spring——外部属性文件安全(三)
2008-02-17 16:13 1945要完成属性文件的加密工作,首先,必须获取一个密钥文件, ... -
扩展Spring——外部属性文件安全(一)
2008-02-17 16:02 4128前言 在Spring的开发中, ... -
Spring 中与 Aware相关的接口
2008-02-17 15:49 3853Spring中提供一些Aware相关接口,像是BeanFact ... -
用 Spring 发送邮件的注意事项
2008-01-23 19:32 2782使用Spring 的封装的MailSender,确实省事多了! ... -
利用Spring简单使用quartz实现定时作业
2008-01-18 00:47 1512定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间进行 ... -
利用Spring按调度计划调用方法
2008-01-18 00:39 1932为了调度报表邮件,你不得不编写EmailReportJob B ... -
在Spring中使用Quartz调度器
2008-01-18 00:32 9152Quartz调度器为调度工作提供了更丰富的支持。和Java定时 ... -
在Spring中使用 Java Timer 调度任务
2008-01-18 00:10 5692从Java 1.3开始,Java SDK就通过java.uti ... -
Send mail with spring mail support and velocity
2008-01-16 23:42 2063One of the requirements on my c ... -
Spring 中关于文件上传与 MultipartResolver
2008-01-16 14:16 12835在 Spring 中, MultipartResolver 主 ... -
spring mvc的异常处理
2008-01-16 14:06 5149在 Spring 中,框架自动集成了异常处理,其主要核心是由 ... -
Spring2.0 声明式事务配置
2008-01-11 16:14 2747<!-- 声明一个事务管理器 --> <b ...
相关推荐
5.3.1 使用外部属性文件 5.3.2 使用加密的属性文件 5.3.3 属性文件自身的引用 5.4 引用Bean的属性值 5.5 国际化信息 5.5.1 基础知识 5.5.2 MessageSource 5.5.3 容器级的国际化信息资源 5.6 容器事件 5.6.1 Spring...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...
5.3.1 使用外部属性文件 5.3.2 使用加密的属性文件 5.3.3 属性文件自身的引用 5.4 引用Bean的属性值 5.5 国际化信息 5.5.1 基础知识 5.5.2 MessageSource 5.5.3 容器级的国际化信息资源 5.6 容器事件 5.6.1 Spring...
• 扩展性 —— Webx 3.0对Spring做了扩展,使Spring Bean不再是“bean”,而是升级成“组件”。一个组件可以扩展另一个组件,也可以被其它组件扩展。这种机制造就了Webx的非常好的扩展性,且比未经扩展的Spring更易...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
——Jim Springfield,ATL发明人 虽然ATL比较精深,但是这本书的讲解非常通俗易懂,语言比较简练,条理非常清楚。即使在读完这本书之后,它仍然可以作为参考书指导我们的开发和学习工作。我想,这就是好书的价值...
8.5.8 通过重写扩展父类方法的功能 143 8.5.9 替代性原理 144 8.6 方法的重载 145 8.6.1 方法重载的规则 145 8.6.2 重载方法的匹配 145 8.6.3 重写与重载的区别 149 8.7 final与继承 149 8.7.1 最终...
5.1.4 重建持久化的实体:如何从外部设置值 5.1.5 获取订单列表 5.1.6 该到讨论实体的时候了 5.1.7 再次回到流程上来 5.1.8 总览图 5.1.9 建立OrderRepository的伪实现 5.1.10 简单讨论一下保存 5.1.11 每个订单的...
这些都预示着我们进入了一个新的互联网阶段web 2.0,它是相对web 1.0的新的一类互联网应用的总称,是一次从核心内容到外部应用的革命[10]。这个阶段发展迅速,互联网应用趋于多样化,其中变化最大的是由web 1.0网站...
这些都预示着我们进入了一个新的互联网阶段web 2.0,它是相对web 1.0的新的一类互联网应用的总称,是一次从核心内容到外部应用的革命[10]。这个阶段发展迅速,互联网应用趋于多样化,其中变化最大的是由web 1.0网站...
第二章 多窗口类浏览器设计 11 2.1 多窗口类浏览器需求分析 11 2.1.1 Activity简介 11 2.1.2 Fragment简介 11 2.1.3 多窗口类浏览器需求 12 2.2 多窗口浏览器模式的实现机制 12 2.2.1安卓移动端多窗口浏览器框架 12 ...