Here we only write out the main method, and call the main method. Don't forget to package it.
- /**
- * Java. Security. messagedigest class implements encryption algorithms such as MD5 Sha.
- */
- Public static void md5test (){
- Messagedigest MD = NULL;
- Try {
- MD = messagedigest. getinstance ("MD5 ");
- } Catch (nosuchalgorithmexception e ){
- E. printstacktrace ();
- }
- String S = "df9f"; // string to be encrypted
- System. Out. println ("encrypted string:" + S );
- Md. Update (S. getbytes (); // the MD5 encryption algorithm only encrypts the character array rather than the string to obtain the object to be encrypted.
- Byte [] BS = md. Digest (); // performs encryption and returns a String Array
- For (byte B: BS ){
- System. Out. Print (B + "|"); // print out the encrypted character array
- }
- System. Out. println ();
- Stringbuffer sb = new stringbuffer ();
- For (INT I = 0; I <BS. length; I ++) {// The byte array is converted into a hexadecimal string to form the final ciphertext.
- Int v = BS [I] & 0xff;
- If (v <16 ){
- SB. append (0 );
- }
- SB. append (integer. tohexstring (V ));
- }
- System. Out. println ("encrypted string:" + Sb );
- }
This is: