import com.mchange.v2.io.DirectoryDescentUtils;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.Key;
import java.security.SecureRandom; /**
* Created by John on 2015/3/24.
*/
public class DESUtil { public static String keyPath;
public static void saveDesKey() {
try {
SecureRandom sr = new SecureRandom();
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
FileOutputStream fos = new FileOutputStream(keyPath);
ObjectOutputStream oos = new ObjectOutputStream(fos); //generate key
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} public static Key getKey() {
Key kp = null; try {
// String fileName = "D:/deskey.xml";
/** InputStream is = DESTest.class.getClassLoader()
.getResourceAsStream(fileName);
**/
Path path = Paths.get(keyPath);
InputStream is = Files.newInputStream(path);
ObjectInputStream oos = new ObjectInputStream(is); kp = (Key) oos.readObject();
oos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return kp;
} /**
* 文件file进行加密并保存目标文件destFile中 * @param file
* 要加密的文件 如c:/test/srcFile.txt * @param destFile
* 加密后存放的文件名 如c:/加密后文件.txt
*/
public static void encrypt(String file, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
} /**
* 文件file进行加密并保存目标文件destFile中 * @param file
* 已加密的文件 如c:/加密后文件.txt
*
* @param dest 解密后存放的文件名 如c:/ test/解密后文件.txt
*/
public static void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
} public static void main(String[] args) throws Exception { //密钥保存在c:\\gpi.key文件中
keyPath = "c:\\gpi.key";
saveDesKey(); //加密c:\src.xml文件
encrypt("C:\\src.xml", "c:\\encrypted.xml"); //解密c:\encrypted.xml文件
decrypt("c:\\encrypted.xml", "c:\\decrypted.xml");
}
}

Java DES 加解密文件的更多相关文章

  1. Java Des加解密方法(c#加密Java解密)

    最近我们用Java把一个用.net编写的老系统重新做了翻版,但是登录还是用.net的登录.这样就会遇到一个比较棘手的问题,我们登录用的cookie信息都是.net用des加密的,但我们不得不用Java ...

  2. java DES加解密及Wrong key size错误

    如下的DES加密方法会报错:Wrong key size public static String encryptDES(String source) throws Exception{ Secret ...

  3. Java DES 加解密("DES/CBC/PKCS5Padding")

    /** * DES加密 * * @param data 加密数据 * @param key 密钥 * @return 返回加密后的数据 */ public static byte[] desEncry ...

  4. Java DES 加解密("DES/ECB/PKCS1Padding")

    private static final Cipher DES_CIPHER; static { try { DES_CIPHER = Cipher.getInstance("DES/ECB ...

  5. Java DES 加解密("DES/EBC/NoPadding")

    private static final Cipher DES_CIPHER; static { try { DES_CIPHER = Cipher.getInstance("DES/ECB ...

  6. Java拓展教程:文件DES加解密

    Java拓展教程:文件加解密 Java中的加密解密技术 加密技术根据一般可以分为对称加密技术和非对称加密技术.对称加密技术属于传统的加密技术,它的加密和解密的密钥是相同的,它的优点是:运算速度快,加密 ...

  7. 一个java的DES加解密类转换成C#

    原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //i ...

  8. C# Java DES加密解密

    转自http://www.cnblogs.com/zhuiyi/archive/2013/04/01/2993201.html 最近被DES加解密弄得超级郁闷,我用C#的方法加密得到的密文老是跟客户给 ...

  9. DES加解密算法Qt实现

      算法解密qt加密table64bit [声明] (1) 本文源码 大部分源码来自:DES算法代码.在此基础上,利用Qt编程进行了改写,实现了DES加解密算法,并添加了文件加解密功能.在此对署名为b ...

随机推荐

  1. phpcms标签大全V9

    转自:http://blog.csdn.net/cloudday/article/details/7343448调用头部 尾部 {template "content"," ...

  2. 工程源码github地址

    APP APP历史版本地址:https://github.com/Myskety/aps APP最终版地址:https://github.com/dycaly/YHAPP 服务器 历史服务器版本地址: ...

  3. iOS - UIGestureRecognizer

    前言 NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject @available(iOS 3.2, *) publ ...

  4. poj1039Pipe(直线交点、叉积)

    链接 之前刷poj计划时刷过,不过也没什么印象了.打铁还是趁热,还没热起来就放弃了,前面算是做了无用功,有如胡乱的看解题报告一样. 题目应该是比较经典的集合入门题,黑书上有一部分核心讲解. 题目中的最 ...

  5. JavaWeb学习总结(二)—http协议

    http协议概念: * 即超文本传输协议.它规定了浏览器与服务器之间的通讯规则. * http是基于请求/响应模式的,所以分为请求协议和响应协议 http的类型: HTTP协议的版本:HTTP/1.0 ...

  6. Handler知识点详解

    Handler是在多线程之间使用的,用于线程之间进行通信. 要想知道为什么需要Handler就首先说明android的主线程和工作线程. 主线程又称为UI线程.正是因为在android中,所有与UI有 ...

  7. Bootstrap文本对齐风格

    在排版中离不开文本的对齐方式.在CSS中常常使用text-align来实现文本的对齐风格的设置.其中主要有四种风格: ☑  左对齐,取值left ☑  居中对齐,取值center ☑  右对齐,取值r ...

  8. eclipse里maven install时,报错提示jdk为无效的目标版本:1.7

    http://blog.csdn.net/wabiaozia/article/details/51733372 ************************************ 报错提示: [ ...

  9. Python 学习笔记 - 10.类(Class) 1

    定义 Python 的 Class 比较特别,和我们习惯的静态语言类型定义有很大区别. 1. 使用一个名为 __init__ 的方法来完成初始化.2. 使用一个名为 __del__ 的方法来完成类似析 ...

  10. HTTP Status 500 - An exception occurred processing at line 35

    HTTP Status 500 - An exception occurred processing JSP page /manage/addCategory.jsp at line 35 type ...