有時候系統數據庫需要保存特定的密碼,例如郵箱密碼發送郵件時要用到,有些會保存到配置文件,但是如果密碼一個月改一次,每次重啓程序太麻煩了,那保存到數據庫,用明文的話(就算第三方已經用加密的密碼key)那不太安全吧?如果這樣我們要使用加密保存,常見的保存是base64或者md5
1.base64
但是base64太簡單,只是轉成64位字符串保存還是太容易了,數據庫被黑客入侵看到了,基本秒破
2.md5
那用md5吧,不好意思,這個是單向的,保存的md5密碼就沒法解密,自己系統還好,但是如果是要解密用於發送郵件時或者API接口對接使用,難道要對方也加密md5匹對?
3.AES128+Base64
所以我這次分享的這個是使用加鹽加解密密碼,這樣保存到數據庫黑客入侵破解沒有我的解密程序,也比較難搞,這裏我只是舉例,大家可以再搞複雜點哈
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class EncrypAES128 {
/**
* 加密
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
System.out.print("Key爲空null");
return null;
}
// 判斷Key是否爲16位
if (sKey.length() != 16) {
System.out.print("Key長度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/補碼方式"
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new Base64().encodeToString(encrypted);//此處使用BASE64做轉碼功能,同時能起到2次加密的作用。
}
/**
* 解密
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String Decrypt(String sSrc, String sKey) throws Exception {
try {
// 判斷Key是否正確
if (sKey == null) {
System.out.print("Key爲空null");
return null;
}
// 判斷Key是否爲16位
if (sKey.length() != 16) {
System.out.print("Key長度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original,"utf-8");
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
/**
* 生成密鑰(鹽)
* @param n 密鑰長度
* @return
*/
public static String getCKey(int n) {
String val = "";
Random random = new Random();
for (int i = 0; i < n; i++) {
String str = random.nextInt(2) % 2 == 0 ? "num" : "char";
if ("char".equalsIgnoreCase(str)) { // 產生字母
int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
// System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
val += (char) (nextInt + random.nextInt(26));
} else if ("num".equalsIgnoreCase(str)) { // 產生數字
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
public static void main(String[] args) throws Exception {
/*
* 此處使用AES-128-ECB加密模式,key需要爲16位。
*/
//String cKey = "1234567890123h56";
String cKey = getCKey(16);
System.out.println("密鑰:"+cKey);
// 需要加密的字串
String cSrc = "www.gowhere.so";
System.out.println(cSrc);
// 加密
String enString = EncrypAES128.Encrypt(cSrc, cKey);
System.out.println("加密後的字串是:" + enString);
// 解密
String DeString = EncrypAES128.Decrypt(enString, cKey);
System.out.println("解密後的字串是:" + DeString);
}
關於使用org.apache.commons.codec.binary.Base64
這個類比較通用可以用於jdk1.6,jdk1.7,如果項目是使用jdk1.8以上,果斷用回原生java.util.Base64的吧
具體可以看看【Java】實現Base64加解密方法彙總以及性能比較
有更好歡迎留言分享交流