资源读取类
package com.crypto.encrypt;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ResourceBundle;
import java.util.Locale;
/**
* 资源读取类
*/
public class Util {
private static ResourceBundle resources=null;
/**
* 初始化资源
*/
static {
new Util();
}
public Util() {
resources = ResourceBundle.getBundle(
"resource.algorithm",
Locale.getDefault());
}
/**
* 读取源文件内容
* @param filename String 文件路径
* @throws IOException
* @return byte[] 文件内容
*/
public static byte[] readFile(String filename) throws IOException {
File file =new File(filename);
if(filename==null || filename.equals(""))
{
throw new NullPointerException("无效的文件路径");
}
long len = file.length();
byte[] bytes = new byte[(int)len];
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
int r = bufferedInputStream.read( bytes );
if (r != len)
throw new IOException("读取文件不正确");
bufferedInputStream.close();
return bytes;
}
/**
* 将加密的数据写入文件
* @param data byte[]
* @throws IOException
*/
public static void writeFile(byte[] data,String filename) throws IOException {
File file =new File(filename);
file.getParentFile().mkdirs();
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(data);
bufferedOutputStream.close();
}
/**
* 从jar文件里读取class
* @param filename String
* @throws IOException
* @return byte[]
*/
public byte[] readFileJar(String filename) throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(getClass().getResource(filename).openStream());
int len=bufferedInputStream.available();
byte[] bytes=new byte[len];
int r=bufferedInputStream.read(bytes);
if(len!=r)
{
bytes=null;
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
}
/**
* 获得密码生成法则
* @return String
*/
public static String getAlgorithm()
{
return resources.getString("algorithm");
}
/**
* 获得值
* @param skey String
* @return String
*/
public static String getValue(String skey)
{
return resources.getString(skey);
}
}
生成密钥
package com.crypto.encrypt;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import java.security.NoSuchAlgorithmException;
import javax.crypto.SecretKey;
import java.io.*;
public class CreateKey {
String filename="";
public CreateKey() {
}
/**
* 获得密匙字节内容
* @throws IOException
* @return byte[]
*/
public byte[] getKeyByte() throws IOException {
byte[] bytes=Util.readFile(filename);
return bytes;
}
public void CreateKeyFile(String filename) throws IOException,
NoSuchAlgorithmException {
this.filename=filename;
if(filename==null || filename.equals(""))
{
throw new NullPointerException("无效的文件路径");
}
createKey();
}
/**
* 生成密匙
* @throws NoSuchAlgorithmException
* @throws IOException
*/
private void createKey() throws NoSuchAlgorithmException, IOException {
SecureRandom secureRandom = new SecureRandom();
// 为我们选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance(Util.getValue("algorithm"));
kg.init(secureRandom);
// 生成密钥
SecretKey key = kg.generateKey();
// 将密钥数据保存为文件供以后使用
Util.writeFile(key.getEncoded(),filename);
}
/**
* 获得密匙文件路径
* @return String
*/
public String getKeyFilePath()
{
return filename;
}
}
加密
package com.crypto.encrypt;
import java.security.SecureRandom;
import java.io.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.Cipher;
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.lang.reflect.Constructor;
import java.security.spec.KeySpec;
import java.lang.reflect.InvocationTargetException;
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 加密后的类文件
*/
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);
// 保存加密后的文件,覆盖原有的类文件。
Util.writeFile(encryptedClassData,encryptfile);
}
/**
* 直接获得加密数据
* @param bytes byte[]
* @return byte[]
*/
public byte[] createEncryptData(byte[] bytes) throws IllegalStateException,
IllegalBlockSizeException, BadPaddingException, InvalidKeyException,
NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException,
InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException,
ClassNotFoundException, IOException {
bytes=getencryptData(bytes);
return bytes;
}
/**
* 加密业务方法
* @param bytes byte[] 待加密数据
* @return byte[] 加密后数据
*/
private byte[] getencryptData(byte[] bytes) 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(keyfile);
// 从原始密钥数据创建DESKeySpec对象
Class classkeyspec=Class.forName(Util.getValue("keyspec"));
Constructor constructor = classkeyspec.getConstructor(new Class[]{byte[].class});
KeySpec dks = (KeySpec)constructor.newInstance(new Object[]{rawKeyData});
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(Util.getAlgorithm());
SecretKey key = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(Util.getAlgorithm());
// 用密钥初始化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;
}
}
解密
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 {
//验证keyfile
if(keyfile==null || keyfile.equals("")){
throw new NullPointerException("无效的key文件路径");
}
unEncryptData(encryptfile,filename);
}
/**
* 解密类文件
* @param encryptfile String 经过加密的文件
* @param filename String 解密后的文件
*/
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);
// 然后将解密后的数据转化成原来的类文件。
Util.writeFile(decryptedData,filename);
}
/**
* 解密字节数组
* @param bytes byte[]
* @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[]
* @return byte[]
*/
private byte[] getunEncryptData(byte[] bytes) 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(keyfile);
// 创建一个DESKeySpec对象
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);
// 创建一个密钥工厂,然后用它把DESKeySpec对象转换成Secret Key对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(Util.getAlgorithm());
SecretKey key = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(Util.getAlgorithm());
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 获得经过加密的数据
//执行解密操作
bytes = cipher.doFinal(bytes);
// 然后将解密后的数据转化成原来的类文件。
return bytes;
}
public void setKeyFile(String keyfile){this.keyfile=keyfile;}
}
algorithm.properties资源文件
algorithm=DES
keyspec=javax.crypto.spec.DESKeySpec
keypath=src/resource/key
测试类
package com.crypto.encrypt;
import java.security.*;
import java.io.*;
import java.lang.reflect.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.*;
import javax.crypto.*;
import sun.security.provider.MD5;
public class TestEncrypt {
public TestEncrypt() {
}
/**
* 创建KEY
* @param keyPath String 生成密钥路径
*/
public void createKey(String keyPath){
CreateKey ck = new CreateKey();
try {
ck.CreateKeyFile(keyPath);
} catch (NoSuchAlgorithmException ex) {ex.printStackTrace();
} catch (IOException ex) {ex.printStackTrace();
}
}
/**
* 加密
* @param sourcePath String 待加密文件路径
* @param distinationPath String 文件加密后保存路径
* @param keyPath String 密匙文件路径
*/
public void encrypt(String sourcePath,String distinationPath,String keyPath){
EncryptData enc = new EncryptData(keyPath);
try {
enc.createEncryptData(sourcePath, distinationPath);
} catch (ClassNotFoundException ex) {ex.printStackTrace();
} catch (SecurityException ex) {ex.printStackTrace();
} catch (NoSuchMethodException ex) {ex.printStackTrace();
} catch (InvocationTargetException ex) {ex.printStackTrace();
} catch (IllegalArgumentException ex) {ex.printStackTrace();
} catch (IllegalAccessException ex) {ex.printStackTrace();
} catch (InstantiationException ex) {ex.printStackTrace();
} catch (IOException ex) {ex.printStackTrace();
} catch (InvalidKeyException ex) {ex.printStackTrace();
} catch (NoSuchAlgorithmException ex) {ex.printStackTrace();
} catch (InvalidKeySpecException ex) {ex.printStackTrace();
} catch (NoSuchPaddingException ex) {ex.printStackTrace();
} catch (BadPaddingException ex) {ex.printStackTrace();
} catch (IllegalBlockSizeException ex) {ex.printStackTrace();
} catch (IllegalStateException ex) {ex.printStackTrace();
}
}
/**
* 解密文件
* @param sourcePath String 待解密文件路径
* @param distinationPath String 解密后文件路径
* @param keyPath String 密钥路径
*/
public void unEncrypt(String sourcePath,String distinationPath,String keyPath){
UnEncryptData unEnc = new UnEncryptData(keyPath);
try {
unEnc.createUnEncryptData(sourcePath,distinationPath);
} catch (ClassNotFoundException ex) {ex.printStackTrace();
} catch (InvocationTargetException ex) {ex.printStackTrace();
} catch (IllegalArgumentException ex) {ex.printStackTrace();
} catch (IllegalAccessException ex) {ex.printStackTrace();
} catch (InstantiationException ex) {ex.printStackTrace();
} catch (SecurityException ex) {ex.printStackTrace();
} catch (NoSuchMethodException ex) {ex.printStackTrace();
} catch (IOException ex) {ex.printStackTrace();
} catch (InvalidKeyException ex) {ex.printStackTrace();
} catch (NoSuchAlgorithmException ex) {ex.printStackTrace();
} catch (InvalidKeySpecException ex) {ex.printStackTrace();
} catch (NoSuchPaddingException ex) {ex.printStackTrace();
} catch (BadPaddingException ex) {ex.printStackTrace();
} catch (IllegalBlockSizeException ex) {ex.printStackTrace();
} catch (IllegalStateException ex) {ex.printStackTrace();
}
}
public static void main(String[] args) {
TestEncrypt e = new TestEncrypt();
e.createKey("classes\\resource\\key1");
e.encrypt("classes\\resource\\a.txt","classes\\resource\\ena.txt","classes\\resource\\key1");
e.unEncrypt("classes\\resource\\ena.txt","classes\\resource\\una.txt","classes\\resource\\key1");
}
}
posted on 2006-05-19 09:23
Derek.Guo 阅读(1403)
评论(1) 编辑 收藏 所属分类:
Java