BASE64、MD5、SHA、HMAC幾種加密演算法(轉)
本篇內容簡要介紹BASE64、MD5、SHA、HMAC幾種加密演算法。
BASE64編碼演算法不算是真正的加密演算法。
MD5、SHA、HMAC這三種加密演算法,可謂是非可逆加密,就是不可解密的加密方法,我們稱之為單向加密演算法。我們通常只把他們作為加密的基礎。單純的以上三種的加密並不可靠。
BASE64
按照RFC2045的定義,Base64被定義為:Base64內容傳送編碼被設計用來把任意序列的8位位元組描述為一種不易被人類直接識別的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常見於郵件、http加密,擷取http資訊,你就會發現登入操作的使用者名稱、密碼欄位通過BASE64加密的。
通過java程式碼實現如下:
- /**
- * BASE64解密
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] decryptBASE64(String key) throws Exception {
- return (new BASE64Decoder()).decodeBuffer(key);
- }
- /**
- * BASE64加密
- * @param key
- * @return
- * @throws Exception
- */
- public static String encryptBASE64(byte[] key) throws Exception {
- return (new BASE64Encoder()).encodeBuffer(key);
- }
主要就是BASE64Encoder、BASE64Decoder兩個類,我們只需要知道使用對應的方法即可。另,BASE加密後產生的位元組位數是8的倍數,如果不夠位數以=符號填充。
MD5
MD5 -- message-digest algorithm 5 (資訊-摘要演算法)縮寫,廣泛用於加密和解密技術,常用於檔案校驗。校驗?不管檔案多大,經過MD5後都能生成唯一的MD5值。好比現在的ISO校驗,都是MD5校驗。怎麼用?當然是把ISO經過MD5後產生MD5的值。一般下載linux-ISO的朋友都見過下載連結旁邊放著MD5的串。就是用來驗證檔案是否一致的。
通過java程式碼實現如下:
- /**
- * MD5加密
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptMD5(byte[] data) throws Exception {
- MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
- md5.update(data);
- return md5.digest();
- }
通常我們不直接使用上述MD5加密。通常將MD5產生的位元組陣列交給BASE64再加密一把,得到相應的字串。
SHA
SHA(Secure Hash Algorithm,安全雜湊演算法),數字簽名等密碼學應用中重要的工具,被廣泛地應用於電子商務等資訊保安領域。雖然,SHA與MD5通過碰撞法都被破解了, 但是SHA仍然是公認的安全加密演算法,較之MD5更為安全。
通過java程式碼實現如下:
- /**
- * SHA加密
- *
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptSHA(byte[] data) throws Exception {
- MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
- sha.update(data);
- return sha.digest();
- }
- }
HMAC
HMAC(Hash Message Authentication Code,雜湊訊息鑑別碼,基於金鑰的Hash演算法的認證協議。訊息鑑別碼實現鑑別的原理是,用公開函式和金鑰產生一個固定長度的值作為認證標識,用這個標識鑑別訊息的完整性。使用一個金鑰生成一個固定大小的小資料塊,即MAC,並將其加入到訊息中,然後傳輸。接收方利用與傳送方共享的金鑰進行鑑別認證等。
通過java程式碼實現如下:
- /**
- * 初始化HMAC金鑰
- *
- * @return
- * @throws Exception
- */
- public static String initMacKey() throws Exception {
- KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
- SecretKey secretKey = keyGenerator.generateKey();
- return encryptBASE64(secretKey.getEncoded());
- }
- /**
- * HMAC加密
- *
- * @param data
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- mac.init(secretKey);
- return mac.doFinal(data);
- }
給出一個完整類,如下:
- import java.security.MessageDigest;
- import javax.crypto.KeyGenerator;
- import javax.crypto.Mac;
- import javax.crypto.SecretKey;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- /**
- * 基礎加密元件
- * @author
- * @version 1.0
- * @since 1.0
- */
- public abstract class Coder {
- public static final String KEY_SHA = "SHA";
- public static final String KEY_MD5 = "MD5";
- /**
- * MAC演算法可選以下多種演算法
- * <pre>
- * HmacMD5
- * HmacSHA1
- * HmacSHA256
- * HmacSHA384
- * HmacSHA512
- * </pre>
- */
- public static final String KEY_MAC = "HmacMD5";
- /**
- * BASE64解密
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] decryptBASE64(String key) throws Exception {
- return (new BASE64Decoder()).decodeBuffer(key);
- }
- /**
- * BASE64加密
- * @param key
- * @return
- * @throws Exception
- */
- public static String encryptBASE64(byte[] key) throws Exception {
- return (new BASE64Encoder()).encodeBuffer(key);
- }
- /**
- * MD5加密
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptMD5(byte[] data) throws Exception {
- MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
- md5.update(data);
- return md5.digest();
- }
- /**
- * SHA加密
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] encryptSHA(byte[] data) throws Exception {
- MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
- sha.update(data);
- return sha.digest();
- }
- /**
- * 初始化HMAC金鑰
- * @return
- * @throws Exception
- */
- public static String initMacKey() throws Exception {
- KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
- SecretKey secretKey = keyGenerator.generateKey();
- return encryptBASE64(secretKey.getEncoded());
- }
- /**
- * HMAC加密
- * @param data
- * @param key
- * @return
- * @throws Exception
- */
- public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- mac.init(secretKey);
- return mac.doFinal(data);
- }
- }
再給出一個測試類:
- import static org.junit.Assert.*;
- import org.junit.Test;
- /**
- *
- * @author
- * @version 1.0
- * @since 1.0
- */
- public class CoderTest {
- @Test
- public void test() throws Exception {
- String inputStr = "簡單加密";
- System.err.println("原文:/n" + inputStr);
- byte[] inputData = inputStr.getBytes();
- String code = Coder.encryptBASE64(inputData);
- System.err.println("BASE64加密後:/n" + code);
- byte[] output = Coder.decryptBASE64(code);
- String outputStr = new String(output);
- System.err.println("BASE64解密後:/n" + outputStr);
- // 驗證BASE64加密解密一致性
- assertEquals(inputStr, outputStr);
- // 驗證MD5對於同一內容加密是否一致
- assertArrayEquals(Coder.encryptMD5(inputData), Coder
- .encryptMD5(inputData));
- // 驗證SHA對於同一內容加密是否一致
- assertArrayEquals(Coder.encryptSHA(inputData), Coder
- .encryptSHA(inputData));
- String key = Coder.initMacKey();
- System.err.println("Mac金鑰:/n" + key);
- // 驗證HMAC對於同一內容,同一金鑰加密是否一致
- assertArrayEquals(Coder.encryptHMAC(inputData, key), Coder.encryptHMAC(
- inputData, key));
- BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData));
- System.err.println("MD5:/n" + md5.toString(16));
- BigInteger sha = new BigInteger(Coder.encryptSHA(inputData));
- System.err.println("SHA:/n" + sha.toString(32));
- BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr));
- System.err.println("HMAC:/n" + mac.toString(16));
- }
- }
控制檯輸出:
- 原文:
- 簡單加密
- BASE64加密後:
- 566A5Y2V5Yqg5a+G
- BASE64解密後:
- 簡單加密
- Mac金鑰:
- uGxdHC+6ylRDaik++leFtGwiMbuYUJ6mqHWyhSgF4trVkVBBSQvY/a22xU8XT1RUemdCWW155Bke
- pBIpkd7QHg==
- MD5:
- -550b4d90349ad4629462113e7934de56
- SHA:
- 91k9vo7p400cjkgfhjh0ia9qthsjagfn
- HMAC:
- 2287d192387e95694bdbba2fa941009a
BASE64的加密解密是雙向的,可以求反解。
MD5、SHA以及HMAC是單向加密,任何資料加密後只會產生唯一的一個加密串,通常用來校驗資料在傳輸過程中是否被修改。其中HMAC演算法有一個金鑰,增強了資料傳輸過程中的安全性,強化了演算法外的不可控因素。單向加密的用途主要是為了校驗資料在傳輸過程中是否被修改。
--------------------- 本文來自 winter13292 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/winter13292/article/details/6528554?utm_source=copy