Java generates the MD5 value of the string, java string md5
The following code generates an MD5 value:
Public class MD5Test2 {public static void main (String [] args) {System. out. println (MD5Test2. MD5Operation ("hello"); System. out. println (MD5Test2. getMD5 ("hello");} // use java. the BigInteger class of the math package implements hexadecimal conversion public final static String MD5Operation (String s) {try {byte strTemp [] = s. getBytes (); MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (strTemp); byte B [] = md. digest (); BigInteger bigInt = new BigInteger (1, B); return bigInt. toString (16);} catch (NoSuchAlgorithmException e) {return null ;}// use the java underlying code to implement hexadecimal conversion public final static String getMD5 (String s) {char hexDigits [] = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F '}; try {byte strTemp [] = s. getBytes (); MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (strTemp); byte B [] = md. digest (); int len = B. length; char str [] = new char [len * 2]; int k = 0; for (int I = 0; I <len; I ++) {str [k ++] = hexDigits [B [I] >>> 4 & 0xf]; str [k ++] = hexDigits [B [I] & 0xf];} return new String (str);} catch (NoSuchAlgorithmException e) {return null ;}}}
Urgent. For example, how to use java to program string HelloWold with MD5 Encryption
Import java. security. MessageDigest;
Public class SpiderMD5 {
Public final static String MD5 (String s ){
String result = "";
Char hexDigits [] =
{'0', '1', '2', '3 ',
'4', '5', '6', '7 ',
'8', '9', 'A', 'B ',
'C', 'D', 'E', 'F '};
Try {
Byte [] strTemp = s. getBytes ();
MessageDigest mdTemp = MessageDigest. getInstance ("MD5 ");
MdTemp. update (strTemp );
Byte [] md = mdTemp. digest ();
Int j = md. length;
Char str [] = new char [j * 2];
Int k = 0;
For (int I = 0; I <j; I ++ ){
Byte B = md [I];
Str [k ++] = hexDigits [B> 4 & 0xf];
Str [k ++] = hexDigits [B & 0xf];
}
Result = new String (str );
} Catch (Exception e)
{E. printStackTrace ();}
Return result;
}
}
The MD5 values in different operating systems using java are different.
Different character set encoding