package com.asiainfo.uap.util.des;
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 com.asiainfo.uap.util.FinalValue;
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 {
//��֤keyfile
if(keyfile==null || keyfile.equals(""))
{
throw new NullPointerException("��Ч��key�ļ�·��");
}
unEncryptData(encryptfile,filename);
}
/**
* �������ļ�
* @param encryptfile String �������ܵ��ļ�
* @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 {
// ��þ������ܵ�����
byte[] data = Util.readFile(encryptfile);
//ִ�н��ܲ���
byte decryptedData[] = getunEncryptData(data,"file");
// Ȼ���ܺ������ת����ԭ�������ļ���
Util.writeFile(decryptedData,filename);
}
/**
* �����ֽ�����
* @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, String algorithm) throws IllegalStateException,
IllegalBlockSizeException, BadPaddingException, InvalidKeyException,
NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException,
InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException,
ClassNotFoundException, IOException {
bytes = getunEncryptData(bytes,algorithm);
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, String algorithm) throws IOException,
ClassNotFoundException, SecurityException, NoSuchMethodException,
InvocationTargetException, IllegalArgumentException,
IllegalAccessException, InstantiationException, NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException, NoSuchAlgorithmException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException,
IllegalStateException {
// ����һ�������ε������Դ
SecureRandom sr = new SecureRandom();
// ����Կ�ļ��л�ȡԭʼ��Կ����
byte[] rawKeyData = Util.readFile(FinalValue.KEY_FILE_NAME);
//byte[] rawKeyData = CreateKey.createKey1(algorithm);
// ����һ��DESKeySpec����
//Class classkeyspec=Class.forName(Util.getValue("keyspec"));
Class classkeyspec;
if(algorithm.equals(FinalValue.ALGORITHM_DES)){
classkeyspec=Class.forName(FinalValue.ALGORITHM_DES_CLASS);
}else if(algorithm.equals(FinalValue.ALGORITHM_PBE)){
classkeyspec=Class.forName(FinalValue.ALGORITHM_PBE_CLASS);
}else{
classkeyspec=Class.forName(FinalValue.ALGORITHM_DES_CLASS);
}
Constructor constructor = classkeyspec.getConstructor(new Class[]{byte[].class});
KeySpec dks = (KeySpec) constructor.newInstance(new Object[]{rawKeyData}); //new DESKeySpec(rawKeyData);
// ����һ����Կ������Ȼ��������DESKeySpec����ת����Secret Key����
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey key = keyFactory.generateSecret(dks);
// Cipher����ʵ����ɽ��ܲ���
Cipher cipher = Cipher.getInstance(algorithm);
// ����Կ��ʼ��Cipher����
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// ��þ������ܵ�����
//ִ�н��ܲ���
bytes = cipher.doFinal(bytes);
// Ȼ���ܺ������ת����ԭ�������ļ���
return bytes;
}
public void setKeyFile(String keyfile)
{
this.keyfile=keyfile;
}
}