zoukankan      html  css  js  c++  java
  • java实现的各种hash加密

    public class jiami {
    public static void main(String args[]){
        String signString="Thank you!";
        String type="SHA-1";
        String result=sign(signString,type);
        System.out.println("采用"+type+"加密之后的串为:"+result);
        type="MD5";
        result=sign(signString,type);
        System.out.println("采用"+type+"加密之后的串为:"+result);
        type="SHA-256";
        result=sign(signString,type);
        System.out.println("采用"+type+"加密之后的串为:"+result);
        type="SHA-384";
        result=sign(signString,type);
        System.out.println("采用"+type+"加密之后的串为:"+result);
    }
        //签名     
            public static String sign(String str, String type){
               String s=Encrypt(str,type);
                return s;
            }
            public static String Encrypt(String strSrc, String encName) {
                MessageDigest md = null;
                String strDes = null;
                byte[] bt = strSrc.getBytes();
                try {
                    md = MessageDigest.getInstance(encName);
                    md.update(bt);
                    strDes = bytes2Hex(md.digest()); // to HexString
                } catch (NoSuchAlgorithmException e) {
                    System.out.println("签名失败!");
                    return null;
                }
                return strDes;
            }
            public static String bytes2Hex(byte[] bts) {
                String des = "";
                String tmp = null;
                for (int i = 0; i < bts.length; i++) {
                    tmp = (Integer.toHexString(bts[i] & 0xFF));
                    if (tmp.length() == 1) {
                        des += "0";
                    }
                    des += tmp;
                }
                return des;
            }
    }

    还有  type="SHA-512";

  • 相关阅读:
    NTT算法小结
    FFT算法小结
    [USACO18DEC]Balance Beam
    洛谷4014分配问题
    洛谷4015运输问题
    洛谷3356火星探险问题
    python中函数详解
    python函数详解
    Python中对文件处理
    Python中的字符编码
  • 原文地址:https://www.cnblogs.com/lwmp/p/6424831.html
Copyright © 2011-2022 走看看