package com.asiainfo.uap.util.des;
import java.io.IOException;
import javax.crypto.spec.PBEKeySpec;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import com.asiainfo.uap.util.FinalValue;
public class EncryptData {
private String keyfile=null;
public EncryptData() {
}
public EncryptData(String keyfile) {
this.keyfile=keyfile;
}
/**
* �����ļ�
* @param filename String Դ·��
* @param filenamekey String ���ܺ��·��
*/
public void createEncryptData(String filename,String filenamekey) throws
IllegalStateException, IllegalBlockSizeException, BadPaddingException,
NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException,
InvalidKeyException, IOException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException,
ClassNotFoundException, IllegalStateException, IllegalBlockSizeException,
BadPaddingException, NoSuchPaddingException, InvalidKeySpecException,
NoSuchAlgorithmException, InvalidKeyException, IOException {
//��֤keyfile
if(keyfile==null || keyfile.equals(""))
{
throw new NullPointerException("��Ч��key�ļ�·��");
}
encryptData(filename,filenamekey);
}
/**
* �������ļ�
* @param filename String ԭʼ�����ļ�
* @param encryptfile String ���ܺ�����ļ�
* @throws IOException
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws IllegalStateException
*/
private void encryptData(String filename,String encryptfile) throws IOException, InvalidKeyException,
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException,
IllegalStateException, ClassNotFoundException, SecurityException,
NoSuchMethodException, InvocationTargetException,
IllegalArgumentException, IllegalAccessException, InstantiationException {
byte data[]=Util.readFile(filename);
// ִ�м��ܲ���
byte encryptedClassData[] = getencryptData(data,"file");
// ������ܺ���ļ�������ԭ�е����ļ���
Util.writeFile(encryptedClassData,encryptfile);
}
/**
* ֱ�ӻ�ü�������
* @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[] createEncryptData(byte[] bytes, String algorithm) throws IllegalStateException,
IllegalBlockSizeException, BadPaddingException, InvalidKeyException,
NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException,
InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException,
ClassNotFoundException, IOException {
bytes=getencryptData(bytes,algorithm);
return bytes;
}
private byte[] getencryptData(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();
//����Կ�ļ�key Filename�еõ���Կ����
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});
// ����һ����Կ������Ȼ��������DESKeySpecת����SecretKey����
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey key = keyFactory.generateSecret(dks);
// Cipher����ʵ����ɼ��ܲ���
Cipher cipher = Cipher.getInstance(algorithm);
// ����Կ��ʼ��Cipher����
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
// ִ�м��ܲ���
bytes = cipher.doFinal(bytes);
// �����ֽ�����
return bytes;
}
/**
* ����key�ļ�·��
* @param keyfile String
*/
public void setKeyFile(String keyfile)
{
this.keyfile=keyfile;
}
}