本文將介紹使用Java里的常見的加解密的一些方法。
前提摘要:加解密對于數據傳輸和數據安全都是非常重要的,學習點加解密知識也是必不可少的.
Base64編碼
Base64[^1]是一種基于64個可打印字符來表示二進制數據的表示方法。由于2的6次方等于64,所以每6個比特為一個單元,對應某個可打印字符。三個字節有24個比特,對應于4個Base64單元,即3個字節需要用4個可打印字符來表示。它可用來作為電子郵件的傳輸編碼。在Base64中的可打印字符包括字母A-Z、a-z、數字0-9,這樣共有62個字符.
Tip: 嚴格意義上來說,base64算不上加密算法,而應該是一種編碼格式。
示例代碼
base64編碼
/**
* base 64 編碼
*
* @param input 字符串輸入
* @return base64 字符串輸出
*/
public static String base64Encode(String input) {
return Base64.encodeBase64String(input.getBytes());
}
base64解碼
/**
* base64 解碼
*
* @param input
* @return
*/
public static String base64Decode(String input) {
return new String(Base64.decodeBase64(input));
}
執行測試
mvn test -Dtest=EncryptUtilTest#testBase64
信息摘要算法:MD5,SHA
MD5[^2] (Message-Digest Algorithm)
MD5消息摘要算法(英語:MD5 Message-Digest Algorithm),一種被廣泛使用的密碼散列函數,可以產生出一個128位(16字節)的散列值(hash value),用于確保信息傳輸完整一致。MD5由羅納德·李維斯特設計,于1992年公開,用以替換MD4算法。這套算法的程序在 RFC 1321 中被加以規范。
廣泛用于加密和解密技術,常用于文件校驗。一般下載linux-ISO的朋友都見過下載鏈接的網頁放著MD5的串,就是用來驗證文件一致性的。
示例代碼
/**
* md5 加密
*
* @param str 字符串
* @return
*/
public static String md5(String str) {
try {
return md5(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
執行測試
mvn test -Dtest=EncryptUtilTest#testMd5
SHA[^3] 安全散列算法(Secure Hash Algorithm)
安全散列算法(英語:Secure Hash Algorithm,縮寫為SHA)是一個密碼散列函數家族,是FIPS所認證的五種安全散列算法。能計算出一個數字消息所對應到的,長度固定的字符串(又稱消息摘要)的算法。且若輸入的消息不同,它們對應到不同字符串的概率很高。這些算法之所以稱作“安全”是基于以下兩點(根據官方標準的描述):
由消息摘要反推原輸入消息,從計算理論上來說是很困難的。想要找到兩組不同的消息對應到相同的消息摘要,從計算理論上來說也是很困難的。任何對輸入消息的變動,都有很高的概率導致其產生的消息摘要迥異。
示例代碼
public static String sha512(byte[] arr) {
try {
MessageDigest sha = MessageDigest.getInstance("SHA-512");
sha.reset();
// must specify "UTF-8" encoding
sha.update(arr);
byte[] array = sha.digest();
// Use Base64 encoding here
String hashed = Base64.encodeBase64URLSafeString(array);
StringBuilder sb = new StringBuilder(32);
if (hashed.length() > 32) {
sb.append(hashed.substring(0, 32));
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
執行測試
mvn test -Dtest=EncryptUtilTest#testSha
對稱加密算法
Des算法
加密
public static byte[] desEncrypt(SecretKey deskey, String input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
// Create the cipher
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE, deskey);
//sensitive information
byte[] text = input.getBytes();
System.out.println("Text [Byte Format] : " + text);
System.out.println("Text : " + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
return textEncrypted;
}
解碼
public static String desDecrypt(SecretKey deskey, byte[] input) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException {
// Create the cipher
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, deskey);
// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(input);
System.out.println("Text Decryted : " + new String(textDecrypted));
return new String(textDecrypted);
}
執行測試
mvn test -Dtest=EncryptUtilTest#testEncrypt
非對稱加密算法(后續)
參考列表
- [^1]base64 wiki
- [^2]MD5
- [^3]SHA
- [^4]DES
本文源代碼地址:https://github.com/daimaniu/cainiaobiji ,歡迎star 和 fork。
下期預告
下期預告(9月22號):網頁調試的相關技巧。
歡迎大家關注我,周更兩篇技術文章。菜鳥筆記 周四不見不散