MD5 encryption and decryption help class, MD5 encryption and decryption
Using System; using System. Security. Cryptography; using System. Text; namespace Maticsoft. DBUtility {// <summary> // DES encryption/Decryption class. /// </Summary> public class DESEncrypt {public DESEncrypt () {}# region ========= encryption ============/// <summary> // encryption /// </summary> // /<param name = "Text"> </param> // <returns> </returns> public static string Encrypt (string Text) {return Encrypt (Text, "litianping ");} /// <summary> /// encrypt data /// </summary> /// <param name = "Text"> </param> /// <param name = "sKey"> </param> // <returns> </returns> public static string Encrypt (string Text, string sKey) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte [] inputByteArray; inputByteArray = Encoding. default. getBytes (Text); des. key = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); des. IV = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); System. IO. memoryStream MS = new System. IO. memoryStream (); CryptoStream cs = new CryptoStream (MS, des. createEncryptor (), CryptoStreamMode. write); cs. write (inputByteArray, 0, inputByteArray. length); cs. flushFinalBlock (); StringBuilder ret = new StringBuilder (); foreach (byte B in ms. toArray () {ret. appendFormat ("{0: X2}", B);} return ret. toString ();} # endregion # region ========= decryption ==============/// <summary> /// decrypt /// </summary> // /<param name = "Text"> </param> // <returns> </returns> public static string Decrypt (string Text) {return Decrypt (Text, "litianping ");} /// <summary> // decrypt the data /// </summary> /// <param name = "Text"> </param> /// <param name = "sKey"> </param> // <returns> </returns> public static string Decrypt (string Text, string sKey) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); int len; len = Text. length/2; byte [] inputByteArray = new byte [len]; int x, I; for (x = 0; x <len; x ++) {I = Convert. toInt32 (Text. substring (x * 2, 2), 16); inputByteArray [x] = (byte) I;} des. key = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); des. IV = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); System. IO. memoryStream MS = new System. IO. memoryStream (); CryptoStream cs = new CryptoStream (MS, des. createDecryptor (), CryptoStreamMode. write); cs. write (inputByteArray, 0, inputByteArray. length); cs. flushFinalBlock (); return Encoding. default. getString (ms. toArray () ;}# endregion }}
Java's MD5 encryption and decryption implementation class needs to encrypt the user's password and then verify the user's password
Import java. security .*;
Import java. util. logging. Level;
Import java. util. logging. Logger;
Public class md5 {
Public String md5 (String str ){
String s = str;
If (s = null ){
Return "";
} Else {
String value = null;
MessageDigest md5 = null;
Try {
Md5 = MessageDigest. getInstance ("MD5 ");
} Catch (NoSuchAlgorithmException ex ){
Logger. getLogger (md5.class. getName (). log (Level. SEVERE, null, ex );
}
Sun. misc. BASE64Encoder baseEncoder = new sun. misc. BASE64Encoder ();
Try {
Value = baseEncoder. encode (md5.digest (s. getBytes ("UTF-8 ")));
} Catch (Exception ex ){
}
Return value;
}
}
}
Decrypts an MD5 encrypted string
MD5 encryption is irreversible, in order to ensure the correctness of the file and prevent some people from stealing the program. In addition, different string encryption results may be the same, but the probability is very small.
The full name of MD5 is Message-Digest Algorithm 5, which was invented by MIT's computer science lab and RSA Data Security Inc in Early 1990s and developed by MD2, MD3, and MD4.
Message-Digest refers to the Hash transformation of a Message, which is to convert a byte string of any length into a long big integer. Note that I use the word "Byte string" instead of "string" because this conversion is only related to the value of the byte and has nothing to do with the character set or encoding method.
MD5 converts a "Byte string" of any length into a large integer of BITs, and it is an irreversible String Conversion Algorithm. In other words, even if you see the source program and algorithm description, it is also impossible to convert an MD5 value back to the original string. In terms of mathematical principle, it is because there are infinite numbers of original strings, which is a bit like a mathematical function without an inverse function.
A typical application of MD5 is to generate fingerprint (fingerprint) for a Message (byte string) to prevent "tampering ". For example, you write a paragraph in a readme.txt file and generate an MD5 value for this readme.txt file and record it. Then you can spread the file to others. If someone else modifies any content in the file, when you re-calculate the MD5 value of this file, you will find that (two MD5 values are different ). If there is another third-party certification authority, MD5 can also prevent the "credit" of the file author. This is the so-called digital signature application.
MD5 is also widely used in encryption and decryption technology. In many operating systems, users' passwords are stored in MD5 values (or similar algorithms, the system calculates the password entered by the user into an MD5 value, and then compares it with the MD5 value saved in the system. The system does not "know" what the user password is.