DES加密文件和字符串
/*
* @(#)DESUtil.java Jan 7, 2013
*
* @Company < Technology Development Company LTD..>
*/
package com.des;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
/**
* @Project <CL-Allocation tool>
* @version <1.0>
* @Author <Administrator>
* @Date <Jan 7, 2013>
* @description
*/
public class DESUtil {
private Key key = null;
public DESUtil(){}
public DESUtil(String strKey){
setKey(strKey);
}
/**根据参数生成一个密室
* @param strKey
*/
public void setKey(String strKey){
OutputStream os = null;
ObjectOutput objectOut = null;
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(new SecureRandom(strKey.getBytes()));
key = keyGenerator.generateKey();
//密室写入文件保存
os = new FileOutputStream("E:\\key.key");
objectOut = new ObjectOutputStream(os);
objectOut.writeObject(key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(objectOut!=null){
objectOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取密室
*/
public void getKey(){
InputStream is = null;
ObjectInput objectIn = null;
try {
is = new FileInputStream("E:\\key.key");
objectIn = new ObjectInputStream(is);
key = (Key) objectIn.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
if(objectIn!=null){
objectIn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**加密,明文输入,密文输出
* @param strMing
* @return
*/
public String encryptStr(String strMing){
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
byteMing = strMing.getBytes("UTF-8");
byteMi = this.encryptByte(byteMing);
strMi = Base64.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
}
return strMi;
}
/**解密,密文输入,明文输出
* @param strMi
* @return
*/
public String decryptStr(String strMi){
String strMing = "";
byte[] byteMi = null;
byte[] byteMing = null;
try {
byteMi = Base64.decode(strMi);
byteMing = this.decryptByte(byteMi);
strMing = new String(byteMing,"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return strMing;
}
/**
* 解密以 byte[] 密文输入 , 以 byte[] 明文输出
*
* @param byteD
* @return
*/
private byte[] decryptByte(byte[] bytes){
Cipher cipher = null;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}finally{
cipher = null ;
}
return byteFina;
}
/**
* 加密以 byte[] 明文输入 ,byte[] 密文输出
*
* @param byteS
* @return
*/
private byte[] encryptByte(byte[] bytes){
byte[] byteFina = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}finally{
cipher = null ;
}
return byteFina;
}
/**
* 文件 file 进行加密并保存目标文件 destFile 中
*
* @param file
* 要加密的文件 如 c:/test/srcFile.txt
* @param destFile
* 加密后存放的文件名 如 c:/ 加密后文件 .txt
*/
public void encryptFile(String filePath,String destPath){
Cipher cipher = null;
InputStream is = null;
OutputStream os = null;
InputStream cis = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
is = new FileInputStream(filePath);
os = new FileOutputStream(destPath);
cis = new CipherInputStream(is,cipher);
byte[] buffer = new byte[1024];
int len = 0;
while((len=cis.read(buffer))>=0){
os.write(buffer, 0,len);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
cipher = null;
try {
if(cis!=null){
cis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件采用 DES 算法解密文件
*
* @param file
* 已加密的文件 如 c:/ 加密后文件 .txt *
* @param destFile
* 解密后存放的文件名 如 c:/ test/ 解密后文件 .txt
*/
public void decryptFile(String filePath,String destPath){
Cipher cipher = null;
InputStream is = null;
OutputStream os = null;
CipherOutputStream cos = null;
try {
is = new FileInputStream(filePath);
os = new FileOutputStream(destPath);
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
cos = new CipherOutputStream(os,cipher);
int len = 0;
byte[] buffer = new byte[1024];
while((len=is.read(buffer))>=0){
cos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
cipher = null;
try {
if(cos!=null){
cos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试方法
public static void main(String[] args) {
DESUtil des = new DESUtil("1234567");
des.getKey();
String strMi = des.encryptStr("des加密的文件sss德萨发家史凯夫拉激发放假");
System.out.println(strMi);
strMi = des.decryptStr(strMi);
}