zoukankan      html  css  js  c++  java
  • JDK自带方法实现消息摘要运算

    啊,有点小注释,懒得介绍了,就贴个代码吧,大意理解就可以了。

      1 package jdbc.pro.lin;
      2 
      3 import java.security.InvalidKeyException;
      4 import java.security.MessageDigest;
      5 import java.security.NoSuchAlgorithmException;
      6 
      7 import javax.crypto.KeyGenerator;
      8 import javax.crypto.Mac;
      9 import javax.crypto.SecretKey;
     10 import javax.crypto.spec.SecretKeySpec;
     11 
     12 import org.apache.commons.codec.binary.Base64;
     13 
     14 public class MyMessageDigest {
     15     public static final String PLAIN_TEXT = "i m a sample";
     16     public static final String MD_ALGORITHM = "MD5";
     17     public static final String SHA_ALGORITHM = "SHA-512";
     18     public static final String MAC_ALGORITHM = "HmacSHA512";
     19 
     20     public static void main(String[] args) {
     21         System.out.println("MD5: " + MD5(PLAIN_TEXT.getBytes()));
     22         System.out.println("SHA-512: " + SHA(PLAIN_TEXT.getBytes()));
     23         System.out.println("HmacSHA512:" + MAC(PLAIN_TEXT.getBytes()));
     24     }
     25 
     26     /**
     27      * 1.消息摘要算法,MD家族,有MD2 MD4 MD5,其中MD4 JDK不支持
     28      * 
     29      * @param plainText
     30      * @return
     31      */
     32     public static String MD5(byte[] plainText) {
     33         MessageDigest messageDigest;
     34         try {
     35             messageDigest = MessageDigest.getInstance(MD_ALGORITHM);
     36             return Base64.encodeBase64String(messageDigest.digest(plainText));
     37         } catch (NoSuchAlgorithmException e) {
     38             // TODO Auto-generated catch block
     39             e.printStackTrace();
     40         }
     41         return null;
     42 
     43     }
     44 
     45     /**
     46      * 2.SHA Security Hash Algorithm 安全散列算法,固定长度摘要信息 SHA-1 SHA-2( SHA-224
     47      * SHA-256 SHA-384 SHA-512) 使用的依然是MessageDigest类,JDK不支持224
     48      * 
     49      * @param plainText
     50      * @return
     51      */
     52     public static String SHA(byte[] plainText) {
     53         MessageDigest messageDigest;
     54         try {
     55             messageDigest = MessageDigest.getInstance(SHA_ALGORITHM);
     56             return Base64.encodeBase64String(messageDigest.digest(plainText));
     57         } catch (Exception e) {
     58             e.printStackTrace();
     59         }
     60         return null;
     61     }
     62 
     63     /**
     64      * 3.MAC(Message Authentication Code) 消息认证码算法,是含有密钥散列函数算法。
     65      * 兼容了MD和SHA的特性。
     66      * 加密过程三步走,与后面要介绍的对称加密和非对称加密是相似的
     67      * 1) 传入算法,实例化一个加密器
     68      * 2) 传入密钥,初始化加密器
     69      * 3) 调用doFinal方法进行加密
     70      * @param plainText
     71      * @return
     72      */
     73     public static String MAC(byte[] plainText) {
     74 
     75         try {
     76             byte[] secretBytes = generatorMACSecretKey();
     77             SecretKey key = restoreMACSecretKey(secretBytes);
     78             Mac mac = Mac.getInstance(MAC_ALGORITHM);
     79             mac.init(key);
     80             return Base64.encodeBase64String(mac.doFinal(plainText));
     81         } catch (NoSuchAlgorithmException | InvalidKeyException e) {
     82             // TODO Auto-generated catch block
     83             e.printStackTrace();
     84         }
     85         return null;
     86 
     87     }
     88 
     89     /**
     90      * MAC生成随机密钥 两步走 1.创建一个KeyGenerator 2.调用KeyGenerator.generateKey方法
     91      * 
     92      * @return
     93      */
     94     public static byte[] generatorMACSecretKey() {
     95         KeyGenerator keyGenerator;
     96         try {
     97             keyGenerator = KeyGenerator.getInstance(MAC_ALGORITHM);
     98             SecretKey key = keyGenerator.generateKey();
     99             return key.getEncoded();
    100         } catch (NoSuchAlgorithmException e) {
    101             // TODO Auto-generated catch block
    102             e.printStackTrace();
    103         }
    104         return null;
    105     }
    106 
    107     /**
    108      * 还原密钥
    109      * 
    110      * @param secretBytes
    111      * @return
    112      */
    113     public static SecretKey restoreMACSecretKey(byte[] secretBytes) {
    114         SecretKey key = new SecretKeySpec(secretBytes, MAC_ALGORITHM);
    115         return key;
    116     }
    117 }
  • 相关阅读:
    Linux开发环境必备十大开发工具
    mysql executemany与 insert ... ON DUPLICATE KEY UPDATE 一起使用
    python LD_LIBRARY_PATH 靠谱解决办法
    搭建简单ftp,满足windows和ubuntu共享文件
    用类方法作为装饰器装饰同属于本类的另一个方法
    通过类方法名调用类方法
    java将jpg文件转化为base64字节(互转)
    CSS元素居中的方式
    T-SQL语法学习一(持续更新)
    SVN的使用教程(一)
  • 原文地址:https://www.cnblogs.com/kingsleylam/p/4986835.html
Copyright © 2011-2022 走看看