1. 程式人生 > >訊息摘要演算法(慕課網視訊總結與知識梳理)

訊息摘要演算法(慕課網視訊總結與知識梳理)

  1. 訊息摘要演算法分類:
    1. MD(Message Digetst):訊息摘要
    2. SHA(Secure Hash Algorithm):安全雜湊
    3. MAC(Message Authentication Code):訊息認證碼
    4. 以上演算法主要是用來驗證資料的完整性
  2. .MD系列演算法:
    1. 特點:MD系列生成的演算法都是128位的。MD2,MD4,MD5
    2. 安全性:5>4>2
  3. SHA演算法(SHA-1,SHA-2(224,256,384,512)):
    1. 特點:固定長度摘要資訊
    2. 上述演算法中,JDK缺少演算法SHA-224,BC提供了全部的演算法
  4. MAC演算法:
    1. 定義:很有金鑰的雜湊函式演算法。包含了MD與SHA兩個系列的演算法,只是在他們兩種演算法的基礎上添加了金鑰
    2. 演算法分類:
      1. MD系列:HmacMD2,HmacMD4,HmacMD5
      2. SHA系列:HmacSHA1,HmacSHA224,HmacSHA256,HmacSHA384,HmacSHA512
  5. MD與SHA演算法的使用:
    1. 是與非:演算法名稱不同,但是使用方法相同
      1. MessageDigets.getInstence(Algorithm);獲取了物件然後加密
  6. MAC的使用:
    1. 使用方式:
      1. MAC是含有金鑰的雜湊函式演算法,既然這樣肯定是要新增金鑰的。那麼首先得生成金鑰
      2. // 初始化KeyGenerator
                    KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
                    // 產生金鑰
                    SecretKey secretKey = keyGenerator.generateKey();
                    // 獲取金鑰
                    // byte[] key = secretKey.getEncoded();
                    byte[] key = Hex.decodeHex(new char[] { '1', '2', '3', '4', '5',
                            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e' });
         
                    // 還原金鑰,HmacMD5是演算法的名字
                    SecretKey restoreSecretKey = new SecretKeySpec(key, "HmacMD5");
                    // 例項化MAC
                    Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm());
                    // 初始化MAC
                    mac.init(restoreSecretKey);
                    // 執行訊息摘要
                    byte[] hmacMD5Bytes = mac.doFinal(src.getBytes());
                    System.out.println("jdk hmacMD5:"
                            + Hex.encodeHexString(hmacMD5Bytes));
  7. 以上是對訊息摘要演算法的簡單的描述,並不涉及操作。