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