BASE6四、MD五、SHA、HMAC幾種加密算法

本篇內容簡要介紹BASE6四、MD五、SHA、HMAC幾種加密算法。
    BASE64編碼算法不算是真正的加密算法。
    MD五、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

如基本的單向加密算法: linux

  • BASE64 嚴格地說,屬於編碼格式,而非加密算法git

  • MD5(Message Digest algorithm 5,信息摘要算法)算法

  • SHA(Secure Hash Algorithm,安全散列算法)數組

  • HMAC(Hash Message Authentication Code,散列消息鑑別碼)安全


    複雜的對稱加密(DES、PBE)、非對稱加密算法: 函數

  • DES(Data Encryption Standard,數據加密算法)工具

  • PBE(Password-based encryption,基於密碼驗證)測試

  • RSA(算法的名字以發明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)編碼

  • DH(Diffie-Hellman算法,密鑰一致協議)

  • DSA(Digital Signature Algorithm,數字簽名)

  • ECC(Elliptic Curves Cryptography,橢圓曲線密碼編碼學)



    本篇內容簡要介紹BASE6四、MD五、SHA、HMAC、DES、PBE、RSA幾種方法。 

    MD五、SHA、HMAC這三種加密算法,可謂是非可逆加密,就是不可解密的加密方法。咱們一般只把他們做爲加密的基礎。單純的以上三種的加密並不可靠。


經過java代碼實現以下:

  1. /** 
  2.  * BASE64解密 
  3.  *  
  4.  * @param key 
  5.  * @return 
  6.  * @throws Exception 
  7.  */  
  8. public static byte[] decryptBASE64(String key) throws Exception {  
  9.     return (new BASE64Decoder()).decodeBuffer(key);  
  10. }  
  11.   
  12. /** 
  13.  * BASE64加密 
  14.  *  
  15.  * @param key 
  16.  * @return 
  17.  * @throws Exception 
  18.  */  
  19. public static String encryptBASE64(byte[] key) throws Exception {  
  20.     return (new BASE64Encoder()).encodeBuffer(key);  
  21. }  


主要就是BASE64Encoder、BASE64Decoder兩個類,咱們只須要知道使用對應的方法便可。另,BASE加密後產生的字節位數是8的倍數,若是不夠位數以=符號填充。

MD5
MD5 -- message-digest algorithm 5 (信息-摘要算法)縮寫,普遍用於加密和解密技術,經常使用於文件校驗。校驗?無論文件多大,通過MD5後都能生成惟一的MD5值。比如如今的ISO校驗,都是MD5校驗。怎麼用?固然是把ISO通過MD5後產生MD5的值。通常下載linux-ISO的朋友都見過下載連接旁邊放着MD5的串。就是用來驗證文件是否一致的。



經過java代碼實現以下:

  1. /** 
  2.  * MD5加密 
  3.  *  
  4.  * @param data 
  5.  * @return 
  6.  * @throws Exception 
  7.  */  
  8. public static byte[] encryptMD5(byte[] data) throws Exception {  
  9.   
  10.     MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);  
  11.     md5.update(data);  
  12.   
  13.     return md5.digest();  
  14.   
  15. }  



一般咱們不直接使用上述MD5加密。一般將MD5產生的字節數組交給BASE64再加密一把,獲得相應的字符串。

SHA
SHA(Secure Hash Algorithm,安全散列算法),數字簽名等密碼學應用中重要的工具,被普遍地應用於電子商務等信息安全領域。雖然,SHA與MD5經過碰撞法都被破解了, 可是SHA仍然是公認的安全加密算法,較之MD5更爲安全。



經過java代碼實現以下:

  1.     /** 
  2.      * SHA加密 
  3.      *  
  4.      * @param data 
  5.      * @return 
  6.      * @throws Exception 
  7.      */  
  8.     public static byte[] encryptSHA(byte[] data) throws Exception {  
  9.   
  10.         MessageDigest sha = MessageDigest.getInstance(KEY_SHA);  
  11.         sha.update(data);  
  12.   
  13.         return sha.digest();  
  14.   
  15.     }  
  16. }  



HMAC
HMAC(Hash Message Authentication Code,散列消息鑑別碼,基於密鑰的Hash算法的認證協議。消息鑑別碼實現鑑別的原理是,用公開函數和密鑰產生一個固定長度的值做爲認證標識,用這個標識鑑別消息的完整性。使用一個密鑰生成一個固定大小的小數據塊,即MAC,並將其加入到消息中,而後傳輸。接收方利用與發送方共享的密鑰進行鑑別認證等。



經過java代碼實現以下:

  1. /** 
  2.  * 初始化HMAC密鑰 
  3.  *  
  4.  * @return 
  5.  * @throws Exception 
  6.  */  
  7. public static String initMacKey() throws Exception {  
  8.     KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);  
  9.   
  10.     SecretKey secretKey = keyGenerator.generateKey();  
  11.     return encryptBASE64(secretKey.getEncoded());  
  12. }  
  13.   
  14. /** 
  15.  * HMAC加密 
  16.  *  
  17.  * @param data 
  18.  * @param key 
  19.  * @return 
  20.  * @throws Exception 
  21.  */  
  22. public static byte[] encryptHMAC(byte[] data, String key) throws Exception {  
  23.   
  24.     SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);  
  25.     Mac mac = Mac.getInstance(secretKey.getAlgorithm());  
  26.     mac.init(secretKey);  
  27.   
  28.     return mac.doFinal(data);  
  29.   
  30. }  



給出一個完整類,以下:

  1. import java.security.MessageDigest;  
  2.   
  3. import javax.crypto.KeyGenerator;  
  4. import javax.crypto.Mac;  
  5. import javax.crypto.SecretKey;  
  6.   
  7. import sun.misc.BASE64Decoder;  
  8. import sun.misc.BASE64Encoder;  
  9.   
  10. /** 
  11.  * 基礎加密組件 
  12.  *  
  13.  * @author 樑棟 
  14.  * @version 1.0 
  15.  * @since 1.0 
  16.  */  
  17. public abstract class Coder {  
  18.     public static final String KEY_SHA = "SHA";  
  19.     public static final String KEY_MD5 = "MD5";  
  20.   
  21.     /** 
  22.      * MAC算法可選如下多種算法 
  23.      *  
  24.      * <pre> 
  25.      * HmacMD5  
  26.      * HmacSHA1  
  27.      * HmacSHA256  
  28.      * HmacSHA384  
  29.      * HmacSHA512 
  30.      * </pre> 
  31.      */  
  32.     public static final String KEY_MAC = "HmacMD5";  
  33.   
  34.     /** 
  35.      * BASE64解密 
  36.      *  
  37.      * @param key 
  38.      * @return 
  39.      * @throws Exception 
  40.      */  
  41.     public static byte[] decryptBASE64(String key) throws Exception {  
  42.         return (new BASE64Decoder()).decodeBuffer(key);  
  43.     }  
  44.   
  45.     /** 
  46.      * BASE64加密 
  47.      *  
  48.      * @param key 
  49.      * @return 
  50.      * @throws Exception 
  51.      */  
  52.     public static String encryptBASE64(byte[] key) throws Exception {  
  53.         return (new BASE64Encoder()).encodeBuffer(key);  
  54.     }  
  55.   
  56.     /** 
  57.      * MD5加密 
  58.      *  
  59.      * @param data 
  60.      * @return 
  61.      * @throws Exception 
  62.      */  
  63.     public static byte[] encryptMD5(byte[] data) throws Exception {  
  64.   
  65.         MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);  
  66.         md5.update(data);  
  67.   
  68.         return md5.digest();  
  69.   
  70.     }  
  71.   
  72.     /** 
  73.      * SHA加密 
  74.      *  
  75.      * @param data 
  76.      * @return 
  77.      * @throws Exception 
  78.      */  
  79.     public static byte[] encryptSHA(byte[] data) throws Exception {  
  80.   
  81.         MessageDigest sha = MessageDigest.getInstance(KEY_SHA);  
  82.         sha.update(data);  
  83.   
  84.         return sha.digest();  
  85.   
  86.     }  
  87.   
  88.     /** 
  89.      * 初始化HMAC密鑰 
  90.      *  
  91.      * @return 
  92.      * @throws Exception 
  93.      */  
  94.     public static String initMacKey() throws Exception {  
  95.         KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);  
  96.   
  97.         SecretKey secretKey = keyGenerator.generateKey();  
  98.         return encryptBASE64(secretKey.getEncoded());  
  99.     }  
  100.   
  101.     /** 
  102.      * HMAC加密 
  103.      *  
  104.      * @param data 
  105.      * @param key 
  106.      * @return 
  107.      * @throws Exception 
  108.      */  
  109.     public static byte[] encryptHMAC(byte[] data, String key) throws Exception {  
  110.   
  111.         SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);  
  112.         Mac mac = Mac.getInstance(secretKey.getAlgorithm());  
  113.         mac.init(secretKey);  
  114.   
  115.         return mac.doFinal(data);  
  116.   
  117.     }  
  118. }  



再給出一個測試類:

  1. import static org.junit.Assert.*;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. /** 
  6.  *  
  7.  * @author 樑棟 
  8.  * @version 1.0 
  9.  * @since 1.0 
  10.  */  
  11. public class CoderTest {  
  12.   
  13.     @Test  
  14.     public void test() throws Exception {  
  15.         String inputStr = "簡單加密";  
  16.         System.err.println("原文:/n" + inputStr);  
  17.   
  18.         byte[] inputData = inputStr.getBytes();  
  19.         String code = Coder.encryptBASE64(inputData);  
  20.   
  21.         System.err.println("BASE64加密後:/n" + code);  
  22.   
  23.         byte[] output = Coder.decryptBASE64(code);  
  24.   
  25.         String outputStr = new String(output);  
  26.   
  27.         System.err.println("BASE64解密後:/n" + outputStr);  
  28.   
  29.         // 驗證BASE64加密解密一致性  
  30.         assertEquals(inputStr, outputStr);  
  31.   
  32.         // 驗證MD5對於同一內容加密是否一致  
  33.         assertArrayEquals(Coder.encryptMD5(inputData), Coder  
  34.                 .encryptMD5(inputData));  
  35.   
  36.         // 驗證SHA對於同一內容加密是否一致  
  37.         assertArrayEquals(Coder.encryptSHA(inputData), Coder  
  38.                 .encryptSHA(inputData));  
  39.   
  40.         String key = Coder.initMacKey();  
  41.         System.err.println("Mac密鑰:/n" + key);  
  42.   
  43.         // 驗證HMAC對於同一內容,同一密鑰加密是否一致  
  44.         assertArrayEquals(Coder.encryptHMAC(inputData, key), Coder.encryptHMAC(  
  45.                 inputData, key));  
  46.   
  47.         BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData));  
  48.         System.err.println("MD5:/n" + md5.toString(16));  
  49.   
  50.         BigInteger sha = new BigInteger(Coder.encryptSHA(inputData));  
  51.         System.err.println("SHA:/n" + sha.toString(32));  
  52.   
  53.         BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr));  
  54.         System.err.println("HMAC:/n" + mac.toString(16));  
  55.     }  
  56. }  



控制檯輸出:

  1. 原文:  
  2. 簡單加密  
  3. BASE64加密後:  
  4. 566A5Y2V5Yqg5a+G  
  5.   
  6. BASE64解密後:  
  7. 簡單加密  
  8. Mac密鑰:  
  9. uGxdHC+6ylRDaik++leFtGwiMbuYUJ6mqHWyhSgF4trVkVBBSQvY/a22xU8XT1RUemdCWW155Bke  
  10. pBIpkd7QHg==  
  11.   
  12. MD5:  
  13. -550b4d90349ad4629462113e7934de56  
  14. SHA:  
  15. 91k9vo7p400cjkgfhjh0ia9qthsjagfn  
  16. HMAC:  
  17. 2287d192387e95694bdbba2fa941009a  



    BASE64的加密解密是雙向的,能夠求反解。
    MD五、SHA以及HMAC是單向加密,任何數據加密後只會產生惟一的一個加密串,一般用來校驗數據在傳輸過程當中是否被修改。其中HMAC算法有一個密鑰,加強了數據傳輸過程當中的安全性,強化了算法外的不可控因素。    單向加密的用途主要是爲了校驗數據在傳輸過程當中是否被修改。

相關文章
相關標籤/搜索