消息摘要算法(慕课网视频总结与知识梳理)

  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. 以上是对消息摘要算法的简单的描述,并不涉及操作。

猜你喜欢

转载自blog.csdn.net/m0_37626203/article/details/83088476