我改的代码:
package security;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.crypto.*;
public class SecretData {
/**
* 加密函数
* */
public String encrypt(SecretKey deskey, String myinfo) {
try {
//测试用
System.out.println("加密前的信息:"+myinfo);
//加密 测试用
System.out.println("加密前的二进串:"+byte2hex(myinfo.getBytes()));
Cipher c1 = Cipher.getInstance("DES");
//用密钥来加密,方式:加密模式
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte = c1.doFinal(myinfo.getBytes());
String strAfterEncrypted = byte2hex(cipherByte);
//测试用
System.out.println("加密后的二进串:" + strAfterEncrypted);
return strAfterEncrypted;
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
return "加密出错";
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
return "加密出错";
} catch (java.lang.Exception e3) {
e3.printStackTrace();
return "加密出错";
}
}
/**
* 解密函数
* */
public String decrypt(SecretKey deskey, String secretInfo) {
try {
//解密
Cipher c1 = Cipher.getInstance("DES");
//用密钥来解密,方式:解密模式
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal(hexToByte(secretInfo));
String strAfterDecrypt = byte2hex(clearByte);
//测试用
System.out.println("解密后的二进串:" + strAfterDecrypt);
String strAfterInfo = new String(clearByte);
//测试用
//System.out.println("解密后的信息:" + strAfterInfo);
return strAfterInfo;
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
return "解密出错";
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
return "解密出错";
} catch (java.lang.Exception e3) {
e3.printStackTrace();
return "解密出错";
}
}
/**
*二进制转字符串
*把二进制数组转换成十六进制串,用:号隔开
*一个字节(8位)对应于一个两位的十六进制数
*/
public String byte2hex(byte[] b) {
String hs="";
String stmp="";
for (int n=0;n<b.length;n++) {
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
/**
* 将十六进制字符串转换成二进制数组
* */
public byte[] hexToByte(String c) {
String strTemp[] = c.split(":");
byte[] results = new byte[strTemp.length];
for (int i = 0; i < strTemp.length; i++) {
results[i] = (byte)(Integer.parseInt(strTemp[i].toLowerCase(), 16));
}
return results;
}
/**
* 生成密钥并存入文件OR从文件取出密钥
* */
public SecretKey getDeskey() {
try {
if ((new File("mykey.dat")).exists() == true) {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("mykey.dat"));
SecretKey deskey = (SecretKey)in.readObject();
in.close();
return deskey;
}
//生成密钥
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecretKey deskey = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("mykey.dat"));
out.writeObject(deskey);
out.close();
System.out.println("写入对象 deskey ok");
return deskey;
} catch (java.lang.Exception e) {
e.printStackTrace();
System.out.println("生成密钥失败");
return null;
}
}
/**
* 该方法测试用
* */
public static void main(String[] args) {
try {
SecretData my = new SecretData();
//生成OR取出密钥
SecretKey deskey = my.getDeskey();
//加密
String strForDecrypt = my.encrypt(deskey, "这是加密的信息!");
System.out.println("deskey is: " + deskey.toString());
//将strForDecrypt 存到数据库
System.out.println("从数据库取出来的信息是: " + strForDecrypt);
//将取出来的deskey 及 strForDecrypt作为参数 来解密
String strAfterDecryptInfo = my.decrypt(deskey, strForDecrypt);
System.out.println("解密后的信息是: " + strAfterDecryptInfo);
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
}
}
运行结果如下:
加密前的信息:这是加密的信息!
加密前的二进串5:E2:CA:C7:BC3:C3C:B5:C40:C5:CF:A2:A3:A1
加密后的二进串:0B:8B:2B:ED:3B:78:8B:0C:82:77:F7:65E:E6:C3:75A:EB:0E:B8:AA:6B:27:09
deskey is: com.sun.crypto.provider.DESKey@fffe7ba6
从数据库取出来的信息是: 0B:8B:2B:ED:3B:78:8B:0C:82:77:F7:65E:E6:C3:75A:EB:0E:B8:AA:6B:27:09
解密后的二进串5:E2:CA:C7:BC3:C3C:B5:C40:C5:CF:A2:A3:A1
解密后的信息是: 这是加密的信息!
有个问题:
我现在是把密钥存到文件中去了,但不知道怎么把密钥存到ORACLE,
Oracle中对应字段要设成什么类型?插入SQL语句应该怎么写?又如何取?