?? desedetools.java
字號:
package com.gmc.algorithms;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import com.gmc.algorithms.Base64;
import com.gmc.crypto.SymmetryEncrypt;
/**
* Desede加密算法,實(shí)現(xiàn)了對字符串,文件,數(shù)據(jù)流的加解密功能,加密時密如為null,可自動生成Des密鑰,供加密使用,解密操作必須傳入密鑰
* 否則不能進(jìn)行解密操作
* @author wanna
*
*/
public class DesedeTools implements SymmetryEncrypt
{
private final static String algorithm = "DESede"; // 對稱加密算法
private SecretKey desKey; // 密鑰
private FileInputStream fis;
private FileOutputStream fos;
public DesedeTools()
{
}
/**
*加密
*
* @param plainText
* 明文 字節(jié)數(shù)組形式
* @param key
* 加密的密鑰,如果為null,會自動生成一個密鑰供加密使用,可用getKey()方法獲得此密鑰
* @return 密文
* @throws Exception
*/
public String encrypt(byte[] plainText, Key key) throws Exception
{
if (plainText == null || plainText.length == 0)
{
return null;
}
this.setDesKey(key); //根據(jù)傳入密鑰情況設(shè)置加密密鑰
return Base64.encode(this.basicEncrypt(plainText)); // Base64進(jìn)行編碼轉(zhuǎn)換
}
/**
* 加密
*
* @param plainText
* 明文
* @param key
* 加密的密鑰,如果為null,會自動生成一個密鑰供加密使用,可用getKey()方法獲得此密鑰
* @return 密文
* @throws Exception
*/
public String encrypt(String plainText, Key key) throws Exception
{
if (plainText == null)
{
return null;
}
this.setDesKey(key);
byte[] input = plainText.getBytes();
return Base64.encode(this.basicEncrypt(input));
}
/**
* 加密 ,對文件進(jìn)行加密,加密后,密文信息存放在護(hù)展名為.djm,與file相同目錄下的文件中。
* 例: file: F:\1.txt
* 密文 : F:\1.txt.djm
*
* @param file
* 明文
* @param key
* 加密所需的密鑰,如果key為null,則自動生成一個密鑰用于加密,加密后可通過getKey()方法獲得此密鑰
* @return 存放有密文信息的文件
* @throws Exception
*/
public File encrypt(File file, Key key) throws Exception
{
if (file == null)
{
return null;
}
if (!file.exists() || file.isDirectory())
{
return null;
}
try
{
this.setDesKey(key);
fis = new FileInputStream(file);
byte[] input = this.getByteFromStream(fis); //獲取輸入流中的數(shù)據(jù)
byte[] output = this.basicEncrypt(input);
String cipherFilePath = file.getPath() + ".djm";
File fileOut = new File(cipherFilePath);
fos = new FileOutputStream(fileOut);
for (int i = 0; i < output.length; i++)
{
fos.write((int) output[i]); //將密文信息寫入文件
}
System.out.println("Des加密文件成功");
return fileOut; //密文
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
this.closeStream(); //關(guān)閉流
}
}
/**
* 加密,對輸入流中的數(shù)據(jù)進(jìn)行加密處理,加密后,將密文信息存儲在指定文件(cipherPath)中
* @param plainText 輸入流
* @param key 加密密鑰,如果為null,則自動生成一個密鑰
* @param cipherPath 加密后,存放密文信息的文件路徑
* @return 存放密文信息的文件
* @throws Exception
*/
public File encrypt(InputStream plainText, Key key, String cipherPath) throws Exception
{
if (plainText == null)
{
return null;
}
if (cipherPath == null || cipherPath.equals(""))
{
return null;
}
try
{
this.setDesKey(key);
byte[] input = this.getByteFromStream(plainText); //獲取輸入流中的數(shù)據(jù)信息
byte[] output = this.basicEncrypt(input); //加密
File fileOut = new File(cipherPath);
fos = new FileOutputStream(fileOut);
for (int i = 0; i < output.length; i++)
{
fos.write((int) output[i]);
}
System.out.println("流加密成功!");
return fileOut;
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
this.closeStream();
}
}
/**
*
* 解密
*
* @param cipherText 密文
* @param key 解密密鑰
* @return 明文信息
* @throws Exception
*/
public String decrypt(byte[] cipherText, Key key) throws Exception
{
if (cipherText == null || cipherText.length == 0)
{
return null;
}
if (key == null)
{
return null;
}
else
{
this.desKey = (SecretKey) key;
}
byte[] input = Base64.decode(cipherText); // 用Base64對加密后的字節(jié)數(shù)組進(jìn)行解碼
return new String(this.basicDecrypt(input));
}
/**
* 解密 如果解密密鑰key為null,則不能進(jìn)行解密操作
*
* @param cipherText 密文
* @param key 解密所需的密鑰Key
* @return 明文信息(字符串形式)
* @throws Exception
*/
public String decrypt(String cipherText, Key key) throws Exception
{
if (cipherText == null)
{
return null;
}
if (key == null)
{
return null;
}
else
{
this.desKey = (SecretKey) key;
}
byte[] input = Base64.decode(cipherText);
return new String(this.basicDecrypt(input));
}
/**
* 解密,對擴(kuò)展名為.djm,存有密文信息的文件進(jìn)行解密操作,解密存放明文件信息的文件與cipherFile在同一目錄下
* 例: cipherFile F;\1.txt.djm
* 解密后存放明文信息的文件為 F:\1.txt
* @param cipherFile 存有密文信息的文件 擴(kuò)展名為.djm
* @param key 解密密鑰
* @return 存有明文件信息的文件
* @throws Exception
*/
public File decrypt(File cipherFile, Key key) throws Exception
{
if (cipherFile == null)
{
return null;
}
if (!cipherFile.exists() || cipherFile.isDirectory())
{
return null;
}
if (key == null)
{
return null;
}
this.desKey = (SecretKey) key; //設(shè)置解密密鑰
try
{
String strPath = cipherFile.getPath();
if (!strPath.substring(strPath.length() - 4).toLowerCase().equals(".djm"))
{
// 只對擴(kuò)展名為.djm,存儲密文信息的文件進(jìn)行解密處理
return null;
}
fis = new FileInputStream(cipherFile);
byte[] input = this.getByteFromStream(fis);
byte[] output = this.basicDecrypt(input);
String outFilePath = strPath.substring(strPath.length() - 4);
File fileOut = new File(outFilePath); // 解密后 存儲明文信息的文件
fos = new FileOutputStream(fileOut);
for (int i = 0; i < output.length; i++)
{
fos.write((int) output[i]);
}
System.out.println("文件解密成功");
return fileOut;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
this.closeStream();
}
return null;
}
/**
* 解密 對輸入流中的密文數(shù)據(jù)解密,解密后的明文存儲在指定文件中(plainPath)
* @param cipherText 存有密文信息的輸入流
* @param key 解密所需的密鑰
* @param plainPath 解密后,明文所存放的文件路徑
* @return 解密后,存有明文信息的文件
* @throws Exception
*/
public File decrypt(InputStream cipherText, Key key, String plainPath) throws Exception
{
if (cipherText == null || key == null)
{
return null;
}
if (plainPath == null || plainPath.equals(""))
{
return null;
}
try
{
this.desKey = (SecretKey) key;
byte[] input = this.getByteFromStream(cipherText); //獲取輸入流中的數(shù)據(jù)
byte[] output = this.basicDecrypt(input); //解密
File outputFile = new File(plainPath);
fos = new FileOutputStream(outputFile);
for (int i = 0; i < output.length; i++)
{
fos.write((int) output[i]);
}
System.out.println("流解密成功");
return outputFile;
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
this.closeStream();
}
}
/**
* 獲得加密/解密操作的密鑰
* @return 加密/解密操作所用的密鑰Key
* @throws Exception
*/
public Key getKey() throws Exception
{
return desKey;
}
/**
* 解密處理后的密文數(shù)據(jù)(字節(jié)數(shù)組形式)
* @param input 要進(jìn)行加密的字節(jié)數(shù)據(jù)
* @return 加密處理后的密文數(shù)據(jù)(字節(jié)數(shù)組形式)
* @throws Exception
*/
private byte[] basicEncrypt(byte[] input) throws Exception
{
Cipher cipher = null;
try
{
cipher = Cipher.getInstance(algorithm); //鍒涘緩鍔犲瘑鎵?闇?鐨凜ipher綾?
cipher.init(Cipher.ENCRYPT_MODE, this.desKey); // 鐢ㄥ瘑閽ュ垵濮嬪寲姝? cipher銆?
return cipher.doFinal(input); //瀹屾垚鍔犲瘑榪愮畻
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
}
/**
*實(shí)現(xiàn)基本的解密功能,供其它方法調(diào)用
*
* @param input 要進(jìn)行解密的字節(jié)數(shù)據(jù)
* @return 解密處理后的密文數(shù)據(jù)(字節(jié)數(shù)組形式)
* @throws Exception
*/
private byte[] basicDecrypt(byte[] input) throws Exception
{
Cipher cipher = null;
try
{
cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, this.desKey); //鐢ㄥ瘑閽ュ垵濮嬪寲姝? cipher銆?
return cipher.doFinal(input); //
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
}
/**
* 設(shè)置加密密鑰,如果key為null,則調(diào)用generateDesKey()方法生成Des密鑰Key供加密使用
* 如果非null,則使用傳入的密鑰進(jìn)行加密操作
*
* @param key
* @throws Exception
*/
private void setDesKey(Key key) throws Exception
{
if (key == null)
{
desKey = (SecretKey) this.generateDesKey();
}
else
{
desKey = (SecretKey) key;
}
}
/**
* 生成Desede算法的密鑰
*
* @return
*/
private Key generateDesKey() throws Exception
{
// KeyGenerator錕?
KeyGenerator keyGen = null;
SecretKey sekey = null;
try
{
keyGen = KeyGenerator.getInstance(algorithm); //根據(jù)加密算法獲得KeyGenerator對象,密鑰生成器
keyGen.init(168); //初始化密鑰長度
sekey = keyGen.generateKey(); //生成 密鑰
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
throw e;
}
return sekey;
}
/**
* 獲取輸入流中的數(shù)據(jù),以字節(jié)數(shù)組形式返回
*
* @param is 輸入流
* @return 以字節(jié)數(shù)組形式返回輸入流中的數(shù)據(jù)
* @throws Exception
*/
private byte[] getByteFromStream(InputStream is) throws Exception
{
int length = -1;
ArrayList temp = new ArrayList();
while ((length = is.read()) != -1)
{
temp.add((byte) length);
}
byte[] out = new byte[temp.size()];
for (int i = 0; i < temp.size(); i++)
{
Byte byt = (Byte) temp.get(i);
out[i] = byt.byteValue();
}
return out;
}
/**
* 關(guān)閉流
* @throws Exception
*/
private void closeStream() throws Exception
{
if (fis != null)
{
fis.close();
}
if (fos != null)
{
fos.close();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -