此文章是vip文章,如何查看?  

1,点击链接获取密钥 http://nicethemes.cn/product/view29882.html

2,在下方输入文章查看密钥即可立即查看当前vip文章


java实现MD5和sha1加密

  • 时间:
  • 浏览:
  • 来源:互联网

import java.security.MessageDigest;
import java.util.Scanner;


public class MD5Util {
    public final static String MD5(String s,String name) {//s是要加密的字符串,name是加密算法(sha1或者md5)
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        try {
            byte[] btInput = s.getBytes();


            MessageDigest mdInst = MessageDigest.getInstance(name);


            mdInst.update(btInput);


            byte[] md = mdInst.digest();


            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String in = input.next();
        String mi = input.next();
        System.out.println(MD5Util.MD5(in,mi));


    }
}

本文链接http://element-ui.cn/news/show-577148.aspx