Java MD5 String md5(String plainText)

Here you can find the source of md5(String plainText)

Description

md

License

Open Source License

Declaration

public static String md5(String plainText) 

Method Source Code


//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String md5(String plainText) {
        String encryptText = null;
        try {//ww  w  .  jav  a2  s  . co m
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(plainText.getBytes("UTF8"));
            byte s[] = m.digest();
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < s.length; i++) {
                result.append(Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6));
            }
            encryptText = result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptText;
    }

    public static String MD5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. md5(String password)
  2. md5(String path_of_file_)
  3. MD5(String pData)
  4. md5(String plain)
  5. Md5(String plainText)
  6. Md5(String plainText)
  7. md5(String plainText)
  8. md5(String plainText)
  9. MD5(String pwd)