📄 des.java
字号:
/*
* Last Modify: 2005年1月13日 2:18:50
*/
package com.bluestar.core.lc;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import java.io.*;
import java.security.Security;
import java.util.Date;
/**
* Filename: DES.java <br/>
* Ttitle: DES文件加密解密类 <br/>
* Description: 目前只有文件的加密,以后把字符串的加密加上 <br/>
* Copyright: Copyright (c) 2001-2004 BlueStar,Inc.All Rights Reserved. <br/>
* Company: bluestar <br/>
* Author: <a href="mailto:lanxingsc@163.com">wuyang</a> <br/>
* Telephone: 86-28-00000000 <br/>
* Date: 2004-5-20 <br/>
* Time: 9:06:45 <br/>
* Version: 2.0.0 <br/>
*/
public class DES {
/**
* 加密方式
*/
private static String Algorithm = "DES";//定义 加密算法,可用 DES,DESede,Blowfish
/**
* 取得实例
*
* @return
*/
public static DES getInstance() {
return new DES();
}
/**
* 取得实例
*
* @return
*/
public static DES getInstance(String algorithm) {
return new DES(algorithm);
}
/**
* 构造函数
*/
private DES() {
}
/**
* 构造函数
*
* @param algorithm
*/
private DES(String algorithm) {
if (algorithm.equals("DES") || algorithm.equals("DESede") || algorithm.equals("Blowfish")) {
this.Algorithm = algorithm;
}
}
/**
* 创建密匙
*
* @param saveFile
*/
public void CreateKey(File saveFile) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
System.out.println("format:" + deskey.getFormat());
System.out.println("format:" + deskey.getAlgorithm());
System.out.println("format:" + String.valueOf(deskey.getEncoded()));
java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(new java.io.FileOutputStream(saveFile));
out.writeObject(deskey);
out.close();
System.out.println("生成密钥成功");
} catch (Exception e) {
System.out.println("生成密钥失败");
e.printStackTrace();
}
}
/**
* 加密文件
*
* @param sourcefile
* @param outfile
* @param key
*/
public String DesFile(File sourcefile, File outfile, File key) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String reStr = "加密成功";
try {
//读入文件
System.out.println("读入文件");
byte[] bt = getBytesFromFile(sourcefile);
//密匙
System.out.println("取得密匙");
java.io.ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(key));
SecretKey deskey = (SecretKey) in.readObject();
in.close();
//加密
System.out.println("加密文件");
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(bt);
bt = null;
//写入文件
System.out.println("写入文件");
writeBytesToFile(outfile, cipherByte);
} catch (java.security.NoSuchAlgorithmException e1) {
reStr = "加密失败,没有该算法";
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
reStr = "加密失败";
e2.printStackTrace();
} catch (java.lang.OutOfMemoryError e3) {
reStr = "加密失败,文件过大,内存溢出";
e3.printStackTrace();
} catch (java.io.IOException e4) {
reStr = "文件读取或写入失败";
e4.printStackTrace();
} catch (java.lang.Exception e5) {
e5.printStackTrace();
}
return reStr;
}
/**
* 解密文件
*
* @param sourcefile
* @param outfile
* @param key
*/
public void OnDesFile(File sourcefile, File outfile, File key) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
//读入文件
byte[] bt = getBytesFromFile(sourcefile);
//密匙
java.io.ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(key));
SecretKey deskey = (SecretKey) in.readObject();
in.close();
System.out.println(deskey.getEncoded().length);
System.out.println(new String(deskey.getEncoded()));
//解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(bt);
byte[] b = deskey.getEncoded();
for (int k = 0; k < b.length; k++) {
b[k] = 25;
System.out.println(b[k]);
}
//写入文件
writeBytesToFile(outfile, cipherByte);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
}
/**
* 读取字节
*
* @param file
* @return
* @throws IOException
*/
private byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
//Read in bytes
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
//Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("不能读取文件: " + file.getAbsolutePath() + file.getName());
}
is.close();
return bytes;
}
/**
* 写入文件
*
* @param file
* @param bytes
* @return
* @throws IOException
*/
private boolean writeBytesToFile(File file, byte[] bytes) throws IOException {
try {
if (!file.exists()) file.createNewFile();
FileOutputStream fs = new FileOutputStream(file);
fs.write(bytes);
fs.close();
return true;
} catch (IOException e) {
throw e;
}
}
/**
* 测试
*
* @param arg
*/
public static void main(String[] arg) {
DES des = DES.getInstance();
File file = new File("c:\\1.rar");
File saveFile = new File("c:\\bbb.doc");
File outfile = new File("c:\\ccc.rar");
File key = new File("c:\\key");
long first = new Date().getTime();
System.out.println("生成key");
if (!key.exists()) des.CreateKey(key);
System.out.println("加密");
des.DesFile(file, saveFile, key);
System.out.println("解密");
des.OnDesFile(saveFile, outfile, key);
System.out.println("用时:" + (new Date().getTime() - first) + "毫秒");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -