1 PackageCOM.COMPANY.FJF;2 3 Importjava.security.MessageDigest;4 Importjava.security.NoSuchAlgorithmException;5 6 Public classMd5test {7 Private Final StaticString[] strdigits = {"0", "1", "2", "3", "4", "5",8"6", "7", "8", "9", "a", "B", "C", "D", "E", "F" };9 Ten Publicmd5test () { One } A - //return hexadecimal - Private StaticString bytetoarraystring (bytebbyte) { the intIRet =Bbyte; - if(IRet < 0) { -IRet + = 256; - } + intID1 = IRET/16; - intiD2 = iRet% 16; + returnSTRDIGITS[ID1] +Strdigits[id2]; A } at - //convert byte array to 16 binary string - Private StaticString bytetostring (byte[] bbyte) { -StringBuffer Sbuffer =NewStringBuffer (); - for(inti = 0; i < bbyte.length; i++) { - sbuffer.append (bytetoarraystring (bbyte[i)); in } - returnsbuffer.tostring (). toUpperCase (); to } + - Public Staticstring Getmd5code (String strobj) { theString resultstring =NULL; * Try { $Resultstring =NewString (strobj);Panax NotoginsengMessageDigest MD = messagedigest.getinstance ("MD5"); - //md.digest () The function returns a byte array that holds the result of the hash value theResultstring =bytetostring (Md.digest (Strobj.getbytes ())); +}Catch(NoSuchAlgorithmException ex) { A ex.printstacktrace (); the } + returnresultstring; - } $ $ Public Static voidMain (string[] args) { -System.out.println (Md5test.getmd5code ("AAAA")); - } the}
MD5 Encryption Java Code implementation