MD5 encryption is a simple application of the messagedigest class in the Java. Security. * package.

Source: Internet
Author: User

Package com. henii. test;
Import java. Security. messagedigest;
/**
* After MD5 encryption, a reversible encryption is performed again.
* The order can be adjusted. You can choose to use Reversible Encryption and then MD5 encryption.
*/
Public class md5andkl {
// MD5 encryption. 32-bit
Public static string MD5 (string instr ){
Messagedigest MD5 = NULL;
Try {
MD5 = messagedigest. getinstance ("MD5 ");
} Catch (exception e ){
System. Out. println (E. tostring ());
E. printstacktrace ();
Return "";
}
Char [] chararray = instr. tochararray ();
Byte [] bytearray = new byte [chararray. Length];
 
For (INT I = 0; I <chararray. length; I ++)
Bytearray [I] = (byte) chararray [I];
 
Byte [] md5bytes = md5.digest (bytearray );
 
Stringbuffer hexvalue = new stringbuffer ();

For (INT I = 0; I <md5bytes. length; I ++ ){
Int val = (INT) md5bytes [I]) & 0xff;
If (Val <16)
Hexvalue. append ("0 ");
Hexvalue. append (integer. tohexstring (VAL ));
}

Return hexvalue. tostring ();
}

// Reversible Encryption Algorithm
Public static string kl (string instr ){
// String S = new string (instr );
Char [] A = instr. tochararray ();
For (INT I = 0; I <A. length; I ++ ){
A [I] = (char) (a [I] ^ 'T ');
}
String S = new string ();
Return S;
}
// Encrypted and decrypted
Public static string JM (string instr ){
Char [] A = instr. tochararray ();
For (INT I = 0; I <A. length; I ++ ){
A [I] = (char) (a [I] ^ 'T ');
}
String K = new string ();
Return K;
}
// Test the main function
Public static void main (string ARGs []) {
String S = new string ("admin ");
System. Out. println ("Original:" + S );
System. Out. println ("after MD5:" + MD5 (s ));
System. Out. println ("MD5 encryption:" + kl (MD5 (s )));
System. Out. println ("decrypted to MD5:" + JM (kl (MD5 (s ))));
}
 
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.