|
Posted on 2009-06-02 09:34 Gavin.lee 閱讀(403) 評論(0) 編輯 收藏 所屬分類: java SE & EE
續加密工具類(一),這個我沒用過,但是看上去沒什么問題,而且詳細到受不了。
package com.Gavin.tools.des;

import java.util.ResourceBundle;
 /** *//**
* @descripte load resource
* @author Gavin.lee
* @date 2009-5-19上午09:49:32
* @version 1.0
*
*/
 public class ResourceLoader {
private ResourceBundle resBundle;
 public ResourceLoader(String resourceName) {
resBundle = ResourceBundle.getBundle(resourceName);
}
 public String getString(String key) {
return resBundle.getString(key);
}
 public static void main(String args[]) {
new ResourceLoader("config").getString("username");
}
}

package com.Gavin.tools.des;

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("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);
// System.out.println(file.getParentFile());
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.Gavin.tools.des;

import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.crypto.SecretKey;

import com.Gavin.tools.util.WriteLog;

import java.io.*;

 public class CreateKey {
String keyfilepath = "";

 public CreateKey() {
this.keyfilepath = new ResourceLoader("algorithm").getString("keypath");
}

 /** *//**
* 獲得密匙字節內容
* @throws IOException
* @return byte[]
*/
 public byte[] getKeyByte() throws IOException {
byte[] bytes = Util.readFile(keyfilepath);
return bytes;
}

public void CreateKeyFile(String filename) throws IOException,
 NoSuchAlgorithmException {
this.keyfilepath = 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"));
// keyfilepath = Util.getValue("keypath");
kg.init(secureRandom);
// 生成密鑰
SecretKey key = kg.generateKey();
// 將密鑰數據保存為文件供以后使用
System.out.println(Util.getValue("algorithm"));
System.out.println(keyfilepath);
Util.writeFile(key.getEncoded(), keyfilepath);
}

 /** *//**
* 獲得密匙文件路徑
* @return String
*/
 public String getKeyFilePath() {
return keyfilepath;
}
 public static void main(String args[]) {
CreateKey cKey = new CreateKey();
 try {
cKey.createKey();
 } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
}
}
}

package com.Gavin.tools.des;

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 keyfilepath = null;

// public EncryptData() {
// }

 public EncryptData() {
this.keyfilepath = new ResourceLoader("algorithm").getString("keypath");
}

 /** *//**
* 加密文件
* @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 (keyfilepath == null || keyfilepath.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);
// 保存加密后的文件,覆蓋原有的類文件。
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) throws IllegalStateException,
IllegalBlockSizeException, BadPaddingException,
InvalidKeyException, NoSuchPaddingException,
InvalidKeySpecException, NoSuchAlgorithmException,
InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException, ClassNotFoundException,
 IOException {
bytes = getencryptData(bytes);
return bytes;
}

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(keyfilepath);
// 從原始密鑰數據創建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.keyfilepath = keyfile;
}

}

package com.Gavin.tools.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 java.security.spec.KeySpec;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.io.ByteArrayInputStream;

 public class UnEncryptData {

private String keyfilepath = "";

 public UnEncryptData() {
}

 public UnEncryptData(String keyfile) {
this.keyfilepath = 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 (keyfilepath == null || keyfilepath.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);
// 然后將解密后的數據轉化成原來的類文件。
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)
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 {
// 生成一個可信任的隨機數源
SecureRandom sr = new SecureRandom();
// 從密鑰文件中獲取原始密鑰數據
byte[] rawKeyData = Util.readFile(keyfilepath);
// 創建一個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.keyfilepath = keyfile;
}
}

|