- 浏览: 361240 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
呆呆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 3517闲来无事翻了下以前写的项目,发现一个还算不错的东西 ... -
关于OpenSessionInViewFilter
2008-03-04 16:05 1528用了OpenSessionInViewFilter之后,程序开 ... -
Spring 事务简化配置
2008-03-02 19:44 1236在 spring 中, 事务管理一般是通过声明一个 txPro ... -
详解spring2.0的scope
2008-02-25 19:14 9576如何使用spring的作用域: <bean id=& ... -
扩展Spring——外部属性文件安全(三)
2008-02-17 16:13 2007要完成属性文件的加密工作,首先,必须获取一个密钥文件, ... -
扩展Spring——外部属性文件安全(一)
2008-02-17 16:02 4206前言 在Spring的开发中, ... -
Spring 中与 Aware相关的接口
2008-02-17 15:49 3915Spring中提供一些Aware相关接口,像是BeanFact ... -
用 Spring 发送邮件的注意事项
2008-01-23 19:32 2832使用Spring 的封装的MailSender,确实省事多了! ... -
利用Spring简单使用quartz实现定时作业
2008-01-18 00:47 1557定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间进行 ... -
利用Spring按调度计划调用方法
2008-01-18 00:39 1989为了调度报表邮件,你不得不编写EmailReportJob B ... -
在Spring中使用Quartz调度器
2008-01-18 00:32 9251Quartz调度器为调度工作提供了更丰富的支持。和Java定时 ... -
在Spring中使用 Java Timer 调度任务
2008-01-18 00:10 5791从Java 1.3开始,Java SDK就通过java.uti ... -
Send mail with spring mail support and velocity
2008-01-16 23:42 2160One of the requirements on my c ... -
Spring 中关于文件上传与 MultipartResolver
2008-01-16 14:16 12891在 Spring 中, MultipartResolver 主 ... -
spring mvc的异常处理
2008-01-16 14:06 5209在 Spring 中,框架自动集成了异常处理,其主要核心是由 ... -
Spring2.0 声明式事务配置
2008-01-11 16:14 2801<!-- 声明一个事务管理器 --> <b ...
相关推荐
UI框架对canvas进行分层 1.base为基础层,放基础界面,主游戏菜单、操作杆、小地图等; 2.main为主业务层,放全部业务界面,背包、榜单等 3.toast层,放吐司 4.loading层,放loading界面 使用方式: 1.在场景中挂上UIManager脚本,并在游戏启动时进行初始化,传入资源加载器 2.显示界面直接UIManager.ShowPanel<T>(),不需要其他操作 3.隐藏界面UIManager.ClosePanel()
IMG_1399.PNG
【毕业设计】java-springboot-vue教师工作量管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
python教程学习
【深圳来觅数据信息科技-2025研报】减产提价!多重因素影响,国内存储芯片逐步崛起.pdf
python学习资源
本协力服装厂服装生产管理系统设计目标是实现协力服装厂服装生产的信息化管理,提高管理效率,使得协力服装厂服装生产管理作规范化、科学化、高效化。 本文重点阐述了协力服装厂服装生产管理系统的开发过程,以实际运用为开发背景,基于Springboot框架,运用了Java编程语言和MYSQL数据库进行开发,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了服装生产相关信息管理的重要功能。 本协力服装厂服装生产管理系统运行效果稳定,操作方便、快捷,界面友好,是一个功能全面、实用性好、安全性高,并具有良好的可扩展性、可维护性的服装生产管理平台。 关键词:服装生产管理,Java编程语言,Springboot框架,MYSQL数据库
网络编程,资源和大家上学的时候的差不多,tcp
vmware虚拟机安装教程
【毕业设计】java-springboot-vue教师人事档案管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计-java】springboot-vue会员制医疗预约服务管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
比赛前开源题目
UniApp开发一个简单的记事本应用文字教程
内容概要:本文档详细介绍了C#编程语言的基础知识和高级特性的相关内容。首先阐述了C#语言的特点和应用场景,涵盖其简单、安全及面向对象三大优点。接着讲述了准备工作的关键步骤,重点提及了开发工具Visual Studio的安装以及推荐的经典书籍和学习资源。文档深入解析了C#基础语法的各个要素,包括数据类型、变量与常量、运算符、控制结构,并结合代码实例强化理解。面向对象编程部分,文档讲解了类与对象的概念,以及继承、多态和封装的原则,并附带具体示例说明,以便读者更容易理解面向对象的理念。此外,文档对比了.NET Framework和.NET Core两大框架的区别和优势,并介绍了一些常用的类库,使开发者在不同平台环境下都可以顺利开发高质量的软件。最后一章提供了一些建议和资源推荐,如参与开源项目、加入技术社区等,为学习者的成长之路指明方向。 适合人群:希望初次接触C#编程的人士、希望通过系统学习达到掌握C#的初级开发者,亦适用于有一定C#经验,想巩固和提升面向对象思想及相关技术的专业技术人员。 使用场景及目标:该文章可以帮助初学者建立完整的C#编程基础框架,快速上手机编程,并引导他们深入了解面向
仅供资料参考,YeeCOM移讯通DTU连接OneNet平台MQTT说明使用手册。
python学习一些项目和资源
python学习资源
【毕业设计-java】springboot-vue家具销售电商平台实现源码(完整前后端+mysql+说明文档+LunW).zip
python学习教程
【毕业设计-java】springboot-vue健身房私人健身与教练预约管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip