MD5 encryption method in Java

Source: Internet
Author: User

Package com.func;

Import java.io.UnsupportedEncodingException;
Import Java.math.BigInteger;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;

public class Md5 {
public static string GetMd5 (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));
}
32-bit encryption
return buf.tostring ();
16-bit encryption
Return buf.tostring (). SUBSTRING (8, 24);
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
return null;
}

}


public static string getMD5 (String str) {
try {
Generate a MD5 cryptographic calculation summary
MessageDigest MD = messagedigest.getinstance ("MD5");
Calculate the MD5 function
Md.update (Str.getbytes ());
Digest () finally determines the return MD5 hash value with a return value of 8 as a string. Because the MD5 hash value is a 16-bit hex value, it's actually a 8-bit character.
The BigInteger function converts a 8-bit string to a 16-bit hex value, represented by a string, and a hash value in the form of a string.
return new BigInteger (1, Md.digest ()). toString (16);
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("MD5 encryption error occurred");
Return "";
}

}

}

MD5 encryption method in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.