1 Importjava.security.MessageDigest;2 Importjava.security.NoSuchAlgorithmException;3 /*4 * MD5 Algorithm5 * 6 */7 Public classMD5 {8 9 //Global ArrayTen Private Final StaticString[] strdigits = {"0", "1", "2", "3", "4", "5", One"6", "7", "8", "9", "a", "B", "C", "D", "E", "F" }; A - PublicMD5 () { - } the - //returns the form of a number followed by a string - Private StaticString bytetoarraystring (bytebbyte) { - intIRet =Bbyte; + //System.out.println ("iret=" +iret); - if(IRet < 0) { +IRet + = 256; A } at intID1 = IRET/16; - intiD2 = iRet% 16; - returnSTRDIGITS[ID1] +Strdigits[id2]; - } - - //return form is only numeric in Private StaticString Bytetonum (bytebbyte) { - intIRet =Bbyte; toSystem.out.println ("iret1=" +iRet); + if(IRet < 0) { -IRet + = 256; the } * returnstring.valueof (iRet); $ }Panax Notoginseng - //convert byte array to 16 binary string the Private StaticString bytetostring (byte[] bbyte) { +StringBuffer Sbuffer =NewStringBuffer (); A for(inti = 0; i < bbyte.length; i++) { the sbuffer.append (bytetoarraystring (bbyte[i)); + } - returnsbuffer.tostring (); $ } $ - Public Staticstring Getmd5code (String strobj) { -String resultstring =NULL; the Try { -Resultstring =NewString (strobj);WuyiMessageDigest MD = messagedigest.getinstance ("MD5"); the //md.digest () The function returns a byte array that holds the result of the hash value -Resultstring =bytetostring (Md.digest (Strobj.getbytes ())); Wu}Catch(NoSuchAlgorithmException ex) { - ex.printstacktrace (); About } $ returnresultstring; - } -}
View Code
Java is encrypted by the MD5 method