频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

在Spring中如何使用加密外部属性文件

来源: 作者:IT168 它山 出处:巧巧读书 2008-02-12 进入讨论组

    加密解密工具类DESEncryptUtil 
    对文件进行对称加密的算法很多,一般使用DES对称加密算法,因为它速度很快,破解困难,DESEncryptUtil不但提供了DES解密功能,还提供了DES加密的功能,因为属性文件在部署前必须经常加密: 
    图 2 加密解密工具类
package com.baobaotao.place; … 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()方法对字节数组进行加密或解密。关于加密,解密更详细的知识,感兴趣的读者可以参阅相关的文章。转载保留:http://www.qqread.com/java/2008/02/e396819.html 更多文章 更多内容请看加密与解密技术常用软件加密宝典Spring开源框架技术专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章