MD5加密

来源:互联网 发布:莱特币开发源码 编辑:程序博客网 时间:2024/05/22 14:50

public final static String encrypt(String s){
    char hexDigits[] = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c','d',
        'e', 'f'};
    try {
      byte[] strTemp = s.getBytes();
      MessageDigest mdTemp = MessageDigest.getInstance("MD5");
      mdTemp.update(strTemp);
      byte[] md = mdTemp.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) throwsNoSuchAlgorithmException{
  System.out.println(MD5.encrypt("1"));

  }

 

 

 

 public static String byte2hex(byte[] bytes){
   String result= "";
   String stmp ="";
   for (int n =0; n < bytes.length; n++) {
    stmp= (java.lang.Integer.toHexString(bytes[n] & 0xFF));
    if(stmp.length() == 1) result = result + "0" + stmp;
    else
     result= result + stmp;
   }
   returnresult;
  }

public static void main(String[] args) throwsNoSuchAlgorithmException{
   String value="1";
   String result ="";
   MessageDigest messageDigest =MessageDigest.getInstance("MD5");
  messageDigest.update(value.getBytes());
   result =byte2hex(messageDigest.digest());

  System.out.println(result);

  }

0 0
原创粉丝点击