Current location - Quotes Website - Signature design - How to realize java md5 encryption? Introduce the source video.
How to realize java md5 encryption? Introduce the source video.
Import java.security.messagedigest;

Import java.security.nosuchalgarithmexception;

Public class MD5 {

Private MessageDigest md

Private static MD5 md5

Private MD5 ()

{

Try {

MD = message digest . getinstance(" MD5 ");

} catch(nosuch algorithm exception e){

System.out.println ("no such algorithm");

}

}

//Generate an MD5 instance.

Public static MD5 getInstance ()

{

if(null! =md5)

Return to md5

other

{

make instance();

Return to md5

}

}

//Make sure that only one thread uses MD5 encryption at the same time.

Private static synchronization void makeInstance ()

{

if(null==md5)

MD5 = new MD5();

}

Common string createMD5 (string passing)

{

MD . update(pass . getbytes());

byte[]b = MD . digest();

Returns bytetohexstring (b);

}

Private string byteToHexString(byte[] b)

{

string buffer sb = new string buffer();

String temp =

for(int I = 0; I<b. Length; i++)

{

temp = integer . tohexstring(b[I]& amp; 0x ff);

if(temp.length()== 1)

temp = " 0 "+temp;

Sb.append (temporary);

}

return sb . tostring();

}

Public static void main(String[] args) {

system . out . println(MD5 . getinstance()create MD5(" 123 ")); }}