項目中藥使用加密的東西,我在網上找了點資料(就是剽竊人家的代碼) 然后自己整理哈,現在把代碼貼在這里以后好用。
/**
* @Title Encryption.java ,By yangtao at 2011-3-4上午10:37:55
* @version 1.0
*
* @Description this is you mark
* @Company Copyright (c) 2010 AOSA TECH, Ltd. All right reserved
* @Project SafeMedia
*
*/
package aosa.safemedia.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @Title Encryption
* @author yangtao at 2011-3-4上午10:38:00
* @Description 用于對字符串加密使用。
*/
public class Encryption {
/**
* @Description 加密的時候要使用的密鑰,這個方法就是生成一個密鑰并保存在文件中
* 這個每次生成的密鑰都不同,所以這個生成一次就行了,在寫入文件的時候應該判斷這個文件是否存在,這樣是否更合理。
*/
private void createKey() {
try {
// 得到密鑰的實例 以什么方式加密。加密的方式比較多。
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56);
SecretKey key = kg.generateKey();
// 將生成的密鑰對象寫入文件。
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
new FileOutputStream(new File("e:\\key.obj")));
objectOutputStream.writeObject(key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param KeyFilePath
* 密鑰Key對象的路徑。注意使用該方法的時候,確保你已經生成了密鑰。
* @return
* @Description 從文件中讀出Key,用于加密使用。
*/
private static Key getKey(String KeyFilePath) {
Key key = null;
try {
// 將生成的密鑰對象從文件中讀取出來,然后再強制轉換成一個密鑰對象。
ObjectInputStream objectInputStream = new ObjectInputStream(
new FileInputStream(new File(KeyFilePath)));
key = (Key) objectInputStream.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return key;
}
/**
* @param source
* @return 放回一個byte數組,為什么不放回字符串,是因為解密的時候要傳入這個byte數組才能進行解密,如果解密的時候傳入的是字符串
* 那么就會出錯,愿意是編碼的問題。
* @Description 將傳入的字符串進行加密 下面寫了將這種byte數組轉換成字符串的方法。直接在調用就行了。
*/
public byte[] encrypt(String source) {
byte[] target = null;
try {
byte[] center = source.getBytes("UTF-8");
Key key = getKey("e:\\key.obj");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
target = cipher.doFinal(center);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return target;
}
/**
* @param source
* 加密后的byte數組??捎眉用芊椒╡ncrypt(“String”)生成即可
* @return 解密后的字符串。
* @Description 解密算法。
*/
public byte[] decrypt(byte[] source) {
byte[] dissect = null;
try {
Key key = getKey("e:\\key.obj");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);// 使用私鑰解密
dissect = cipher.doFinal(source);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return dissect;
}
/**
* @param bytes
* @Description 用于把加密后的byte[]數組采用特定的方式寫入到文件。
*/
public void encodeByteToFile(byte[] bytes){
BASE64Encoder base64encoder = new BASE64Encoder();
try {
base64encoder.encode(bytes,new FileOutputStream(new File("D:\\t.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @return
* @Description 由于加密之前采用了編碼的格式 所以現在采用特點的方式讀出來 ,然后得到用一個byte[]用于解碼。
*/
public byte[] getByteFromFile(){
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encodeByte = null;
try {
encodeByte = base64decoder.decodeBuffer(new FileInputStream(new File("D:\\t.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return encodeByte;
}
/**
* @param b
* @param filePath
* @Description 將指定的字節寫入到文件中。
*/
public void writeByteToFile(byte[] b, String filePath) {
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(b);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//測試使用。
// public static void main(String[] args) {
// Encryption encryption = new Encryption();
// //encryption.dateEncrypt(encryption.encrypt("yangtao"));
// System.out.println(new String(encryption.decrypt(encryption.getByteFromFile())));
// }
}
代碼寫的也不怎么好,只是吧功能實現了。以后功底后了明白了再來修改。