?? unencryptdata.java
字號(hào):
package com.crypto.encrypt;
import java.security.SecureRandom;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.security.spec.KeySpec;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.io.ByteArrayInputStream;
public class UnEncryptData {
private String keyfile="";
public UnEncryptData() {
}
public UnEncryptData(String keyfile) {
this.keyfile=keyfile;
}
public void createUnEncryptData(String encryptfile,String filename) throws
IllegalStateException, IllegalBlockSizeException, BadPaddingException,
NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException,
InvalidKeyException, IOException, NoSuchMethodException,
SecurityException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
ClassNotFoundException, IllegalStateException, IllegalBlockSizeException,
BadPaddingException, NoSuchPaddingException, InvalidKeySpecException,
NoSuchAlgorithmException, InvalidKeyException, IOException {
//驗(yàn)證keyfile
if(keyfile==null || keyfile.equals(""))
{
throw new NullPointerException("無(wú)效的key文件路徑");
}
unEncryptData(encryptfile,filename);
}
/**
* 解密類文件
* @param encryptfile String 經(jīng)過(guò)加密的文件
* @param filename String 解密后的文件
* @throws IOException
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws IllegalStateException
*/
private void unEncryptData(String encryptfile,String filename) throws
IOException, IllegalStateException, IllegalBlockSizeException,
BadPaddingException, InvalidKeyException, NoSuchPaddingException,
InvalidKeySpecException, NoSuchAlgorithmException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException,
ClassNotFoundException, IOException {
// 獲得經(jīng)過(guò)加密的數(shù)據(jù)
byte[] data = Util.readFile(encryptfile);
//執(zhí)行解密操作
byte decryptedData[] = getunEncryptData(data);
// 然后將解密后的數(shù)據(jù)轉(zhuǎn)化成原來(lái)的類文件。
Util.writeFile(decryptedData,filename);
}
/**
* 解密字節(jié)數(shù)組
* @param bytes byte[]
* @throws IllegalStateException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws InvalidKeyException
* @throws NoSuchPaddingException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws ClassNotFoundException
* @throws IOException
* @return byte[]
*/
public byte[] createUnEncryptData(byte[] bytes) throws IllegalStateException,
IllegalBlockSizeException, BadPaddingException, InvalidKeyException,
NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException,
InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException,
ClassNotFoundException, IOException {
bytes = getunEncryptData(bytes);
return bytes;
}
/**
*
* @param bytes byte[]
* @throws IOException
* @throws ClassNotFoundException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws IllegalStateException
* @return byte[]
*/
private byte[] getunEncryptData(byte[] bytes) throws IOException,
ClassNotFoundException, SecurityException, NoSuchMethodException,
InvocationTargetException, IllegalArgumentException,
IllegalAccessException, InstantiationException, NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException, NoSuchAlgorithmException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException,
IllegalStateException {
// 生成一個(gè)可信任的隨機(jī)數(shù)源
SecureRandom sr = new SecureRandom();
// 從密鑰文件中獲取原始密鑰數(shù)據(jù)
byte[] rawKeyData = Util.readFile(keyfile);
// 創(chuàng)建一個(gè)DESKeySpec對(duì)象
Class classkeyspec=Class.forName(Util.getValue("keyspec"));
Constructor constructor = classkeyspec.getConstructor(new Class[]{byte[].class});
KeySpec dks = (KeySpec) constructor.newInstance(new Object[]{rawKeyData}); //new DESKeySpec(rawKeyData);
// 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec對(duì)象轉(zhuǎn)換成Secret Key對(duì)象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(Util.getAlgorithm());
SecretKey key = keyFactory.generateSecret(dks);
// Cipher對(duì)象實(shí)際完成解密操作
Cipher cipher = Cipher.getInstance(Util.getAlgorithm());
// 用密鑰初始化Cipher對(duì)象
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 獲得經(jīng)過(guò)加密的數(shù)據(jù)
//執(zhí)行解密操作
bytes = cipher.doFinal(bytes);
// 然后將解密后的數(shù)據(jù)轉(zhuǎn)化成原來(lái)的類文件。
return bytes;
}
public void setKeyFile(String keyfile)
{
this.keyfile=keyfile;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -