public class DESEncrypt {

    /** 加密工具 */
private Cipher encryptCipher = null; /** 解密工具 */
private Cipher decryptCipher = null;
private static String keyVal = "asdf1234"; public DESEncrypt(){
try {
this.initialize_encryptKey(keyVal);
this.initalize_dencryptkey(keyVal);
} catch (Exception e) {
e.printStackTrace();
}
} private void initialize_encryptKey(String keyValue) throws Exception{
Key key = getKey(keyValue.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
} public void initalize_dencryptkey(String keyValue) throws Exception {
Key key = getKey(keyValue.getBytes());
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} /**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[]; // 将原始字节数组转换为8位
for (int i = ; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
} /**
* 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
} /**
* 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
} /**
* 文件file进行加密并保存目标文件destFile中
*
* @param file
* 要加密的文件 如c:/test/srcFile.txt
* @param destFile
* 加密后存放的文件名 如c:/加密后文件.txt
*/
public void encrypt(String sourceFileName, String diminationFileName) throws Exception {
InputStream is = new FileInputStream(sourceFileName);
OutputStream out = new FileOutputStream(diminationFileName);
CipherInputStream cis = new CipherInputStream(is, encryptCipher);
byte[] buffer = new byte[];
int r;
while ((r = cis.read(buffer)) > -) {
out.write(buffer, , r);
}
cis.close();
is.close();
out.close();
}
public void encrypt(File sourceFile, File diminationFile) throws Exception {
InputStream is = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(diminationFile);
CipherInputStream cis = new CipherInputStream(is, encryptCipher);
byte[] buffer = new byte[];
int r;
while ((r = cis.read(buffer)) > -) {
out.write(buffer, , r);
}
cis.close();
is.close();
out.close();
}
/**
* 文件采用DES算法解密文件
*
* @param file
* 已加密的文件 如c:/加密后文件.txt * @param destFile 解密后存放的文件名 如c:/
* test/解密后文件.txt
*/
public void decrypt(String sourceFileName, String diminationFileName) throws Exception {
InputStream is = new FileInputStream(sourceFileName);
OutputStream out = new FileOutputStream(diminationFileName);
CipherOutputStream cos = new CipherOutputStream(out, decryptCipher);
byte[] buffer = new byte[];
int r;
while ((r = is.read(buffer)) >= ) {
cos.write(buffer, , r);
}
cos.close();
out.close();
is.close();
}
public void decrypt(File sourceFile, File fileout ) throws Exception {
InputStream is = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(fileout);
CipherOutputStream cos = new CipherOutputStream(out, decryptCipher);
byte[] buffer = new byte[];
int r;
while ((r = is.read(buffer)) >= ) {
cos.write(buffer, , r);
}
cos.close();
out.close();
is.close();
}
public static void main(String[] args) throws Exception {
DESEncrypt t = new DESEncrypt();
//t.initialize_encryptKey(keyVal);
//t.initalize_dencryptkey(keyVal);
Long l = System.currentTimeMillis();
System.out.println("开始"+l);
t.encrypt("e:/ceshi.sql", "e:/ceshi2.sql");
System.out.println("结束"+(System.currentTimeMillis()-l));
t.decrypt("e:/ceshi2.sql", "e:/ceshi3.sql");
System.out.println("结束2"+(System.currentTimeMillis()-l));
}
}

凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字凑150字

DES 加密解密 文件工具类的更多相关文章

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

    一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //import java.util.regex.P ...

  2. DES加密解密与AES加密解密

    随着开发时间的变长,当初认为比较难的东西,现在渐渐也就变的不那么难了!特别对于一些经常很少使用的类,时间长了之后渐渐就陌生了.所以在这里写一些日后可能会用到的加密与解密. 一.AES加密算法和DES加 ...

  3. DES加密解密 MD5加密解密

    #region MD5 加密 /// <summary> /// MD5加密静态方法 /// </summary> /// <param name="Encry ...

  4. DES加密解密

    加密后生成Base64字符串,并去除'='字符. 加密后替换掉'+',这样加密后的字符串可以作为url参数传递. using System; using System.IO; using System ...

  5. 各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)

    原文:各种加密解密函数(URL加密解密.sha1加密解密.des加密解密) 普通hash函数如md5.sha1.base64等都是不可逆函数.虽然我们利用php可以利用这些函数写出可逆函数来.但是跨语 ...

  6. 使用 gzexe 快速加密解密文件内容

    使用 gzexe 快速加密解密文件内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用sshpass工具编写远程管理脚本 1>.安装依赖包 [root@node101 ...

  7. DES加密/解密

    /// <summary> /// DES加密/解密类. /// </summary> public class DESEncrypt { #region ========加密 ...

  8. 【DES加密解密】 C#&JAVA通用

    DES加密解密 C# Code /// <summary> /// DES加密解密帮助类 /// </summary> public static class DESHelpe ...

  9. php使用openssl进行Rsa长数据加密(117)解密(128) 和 DES 加密解密

    PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文:解密的时候需要128个字符解密一下,然后拼接成数据. 加 ...

随机推荐

  1. SpringMVC入门总结

    一.SpringMVC的好处? 1,基于注解,stuts2虽然也有注解但是比较慢,没人用更多的时候是用xml的形式 2,能与spring其它技术整合比如说webflow等, 3,获取request及s ...

  2. dsPIC单片机的CAN引脚设置

    用单片机的引脚复用 查询芯片数据手册C1RX的寄存器为RPINR26.C1RXR=(设置为需要用到的引脚) 引脚设置为输入(C1RX),TRIS=1: C1TX需要用的引脚为RPn41,查询数据手册R ...

  3. python pexpect总结

    基本使用流程 pexpect 的使用说来说去,就是围绕3个关键命令做操作: 首先用 spawn 来执行一个程序 然后用 expect 来等待指定的关键字,这个关键字是被执行的程序打印到标准输出上面的 ...

  4. 07 . Python3函数

    Python3函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print().我们可以直接调用 ...

  5. keras常见问题

    问题:AttributeError: 'CRF' object has no attribute '_outbound_nodes' 解答:这个一般情况下是keras版本的问题,将其改为keras== ...

  6. Rocket - debug - TLDebugModuleInner - ABSTRACTCS

    https://mp.weixin.qq.com/s/dF_0sE5ZakyY569wlppVHA 简单介绍TLDebugModuleInner中ABSTRACTCS寄存器的实现. 1. ABSTRA ...

  7. jchdl - RTL实例 - And2(结构体的使用)

    https://mp.weixin.qq.com/s/qTgeBF9N0mx5UK3xWDb3jg   jchdl对Verilog做了增强,增加了用户自定义结构体类型.使用自定义结构体,可以对输入和输 ...

  8. Java实现 蓝桥杯 算法提高 GPA(暴力)

    试题 算法提高 GPA 问题描述 输入A,B两人的学分获取情况,输出两人GPA之差. 输入格式 输入的第一行包含一个整数n表示A的课程数,以下n行每行Si,Ci分别表示第i个课程的学分与A的表现. G ...

  9. ASP.NET防止自己网站的资源被盗(通过IHttpHandler 带样例说明)

    我这里用的图片被盗举例子 一个正常的网页 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind ...

  10. Java实现 LeetCode 71 简化路径

    71. 简化路径 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (-) 表示将 ...