java消息摘要算法加密

1.几种消息摘要算法简介

  • md(Message Digest) (md2,md4,md5) 128位
  • sha(Secure Hash Algorithm)(sha1,sha2)
  • mac(Message Authentication Code) 融合md,mac,含有秘钥
    验证数据的完整性
    数字签名核心算法

2.应用场景
md可以用于用户登录密码的加密
sha应用

  • 加入约定key
  • 增加时间戳
  • 排序
    http://**?msg=12Hasd74mh&timestamp=1309488734
    对msg,key,timestamp排序拼接,然后sha加密
    例如,微信公众账号开发开始时的验证

3.实现代码
md算法

    public static void jdkmd()
    {
        try {
            MessageDigest md = MessageDigest.getInstance("md2");
            byte[] encodeBytes = md.digest(str.getBytes());
            System.out.println(Hex.encodeHexString(encodeBytes));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void ccmd()
    {
        byte[] encodeBytes = DigestUtils.md5(str.getBytes());
        System.out.println(Hex.encodeHexString(encodeBytes));
    }
    public static void bcmd()
    {
        Digest dt = new MD5Digest();
        dt.update(str.getBytes(), 0, str.getBytes().length);
        byte[] md5Bytes = new byte[dt.getDigestSize()];
        dt.doFinal(md5Bytes, 0);
        System.out.println("bcmd5:"+org.bouncycastle.util.encoders.Hex.toHexString(md5Bytes));

//      try {
//          MessageDigest md = org.bouncycastle.jcajce.provider.digest.MD5.Digest.getInstance("md5");
//          byte[] encodeBytes = md.digest(str.getBytes());
//          System.out.println(org.bouncycastle.util.encoders.Hex.toHexString(encodeBytes));
//          
//      } catch (NoSuchAlgorithmException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
    }
    public static void bcmd4()
    {
        Security.addProvider(new BouncyCastleProvider());
        try {
            MessageDigest md = MessageDigest.getInstance("MD4");
            byte[] encodeByte = md.digest(str.getBytes());
            System.out.println("bcmd4:"+org.bouncycastle.util.encoders.Hex.toHexString(encodeByte));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

sha算法代码实现

    public static void jdksha()
    {
        try {
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            byte[] encodeBytes = sha.digest(str.getBytes());
            System.out.println("jdksha1:"+Hex.encodeHexString(encodeBytes));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void bcsha()
    {
        Digest dt = new SHA224Digest();
        dt.update(str.getBytes(),0,str.getBytes().length);
        byte[] sha224Bytes = new byte[dt.getDigestSize()];
        dt.doFinal(sha224Bytes, 0);
        System.out.println("bcsha224:"+org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes));
        System.out.println();
    }

md和sha的代码实现几乎差不多,二者可以互相借鉴。
mac算法代码实现

    public static void jdkHmacMd5() 
    {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");//初始化KeyGenerator
            //随机产生秘钥
            SecretKey secertKey = keyGenerator.generateKey(); //产生秘钥
            byte[] key = secertKey.getEncoded();
            //产生指定秘钥
            key = new byte[]{'a','a','a','a','a','a','a','a','a','a'};//Hex.decodeHex(new char[]{'a','a','a','a','a','a','a','a','a','a'});
            secertKey = new SecretKeySpec(key,"HmacMD5");
            Mac mac = Mac.getInstance(secertKey.getAlgorithm());//实例化Mac
            mac.init(secertKey);//初始化Mac
            byte[] encodeByte = mac.doFinal(str.getBytes()); //执行摘要
            System.out.println("jdkHmacMd5:"+Hex.encodeHexString(encodeByte));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void bcHmacMd5()
    {
        HMac hmac = new HMac(new MD5Digest());
        hmac.init(new KeyParameter(new byte[]{'a','a','a','a','a','a','a','a','a','a'}));
        hmac.update(str.getBytes(), 0, str.getBytes().length);
        byte[] hmacMd5Bytes = new byte[hmac.getMacSize()];
        hmac.doFinal(hmacMd5Bytes, 0);
        System.out.println("bcHmacMd5:"+org.bouncycastle.util.encoders.Hex.toHexString(hmacMd5Bytes));
    }

你可能感兴趣的:(加密,消息摘要算法)