MD5加密

Md5加密

public class MessageDigests {

  public static final String KEY_MD5 = "MD5";// 加密算法

  public static final String CHAR_SET = "utf-8";

  public static final String prifix = "cst_";

  public static String encryptMD5(String str) { 
    try {
      byte[] data = str.getBytes(CHAR_SET);
      byte[] md5data = encryptMD5(data);
      BigInteger md5bi = new BigInteger(md5data);
      return md5bi.toString(16);
     } catch (UnsupportedEncodingException e) {
        e.printStackTrace();}
    return null;
    }

  public static String cstMd5(String pwd) { //对pwd 进行 Md5 算法加密,返回加密后的字符串
    String md5 = encryptMD5(prifix + pwd);
    return md5;}
  public static byte[] encryptMD5(byte[] data) { //采用md5加密并返回byte[]
    MessageDigest md5;
    try {
      md5 = MessageDigest.getInstance(KEY_MD5);
      md5.update(data);
      return md5.digest();
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);}}

}
posted @ 2020-12-14 19:46  来一杯coffee  阅读(63)  评论(0)    收藏  举报