Java Implementation MD5 algorithm

Source: Internet
Author: User

Package and other;

Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
/*
* MD5 algorithm
*/
public class MD5 {

Global array
Private final static string[] strdigits = {"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "B", "C", "D", "E", "F"};

Public MD5 () {
}

Returns the form of a number followed by a string
private static String bytetoarraystring (byte bbyte) {
int iRet = Bbyte;
System.out.println ("iret=" +iret);
if (IRet < 0) {
IRet + = 256;
}
int iD1 = IRET/16;
int iD2 = iRet% 16;
return STRDIGITS[ID1] + STRDIGITS[ID2];
}

Return form is only numeric
private static String Bytetonum (byte bbyte) {
int iRet = Bbyte;
System.out.println ("iret1=" + iRet);
if (IRet < 0) {
IRet + = 256;
}
Return string.valueof (IRet);
}

Convert byte array to 16 binary string
private static String bytetostring (byte[] bbyte) {
StringBuffer sbuffer = new StringBuffer ();
for (int i = 0; i < bbyte.length; i++) {
Sbuffer.append (bytetoarraystring (bbyte[i));
}
return sbuffer.tostring ();
}

public static string Getmd5code (String strobj) {
String resultstring = null;
try {
resultstring = new String (strobj);
MessageDigest MD = messagedigest.getinstance ("MD5");
Md.digest () The function returns a byte array that holds the result of the hash value
resultstring = bytetostring (Md.digest (Strobj.getbytes ()));
} catch (NoSuchAlgorithmException ex) {
Ex.printstacktrace ();
}
return resultstring;
}

public static void Main (string[] args) {
MD5 getMD5 = new MD5 ();
System.out.println (Getmd5.getmd5code ("000000"));
}
}

Java Implementation MD5 algorithm

Related Article

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.