In various application systems, if you need to set up an account, it will involve the storage of user account information issues, in order to ensure the security of the stored account information, usually by MD5 encrypted way to store. First, simply introduce what is MD5 encryption.
MD5 's full name is Message-digest algorithm 5 (Information-Digest algorithm), in the early 90 by MIT Laboratory for Computer Science and RSA Data Security Inc Ronald L. Riv EST developed, through the development of MD2, MD3 and MD4. is to allow bulk information to be "compressed" into a confidential format before signing a private key with a digital signature software (that is, converting an arbitrary-length byte string into a long, large integer). Whether it's MD2, MD4, or MD5, they all need to get a random length of information and generate a 128-bit message digest. Although these algorithms are more or less similar in structure, the design of MD2 is completely different from MD4 and MD5 because MD2 is designed for 8-bit machines, while MD4 and MD5 are for 32-bit computers. The description of these three algorithms and the C language source code are described in detail in Internet RFCs 1321, which is the most authoritative document submitted to the IETF by Ronald L. Rivest in August 1992.
(a) Introduction to the Message digest
A message digest is a digital fingerprint of a block of data. That is, a data block of any length is computed, producing a unique fingerprint (for SHA1, a 20-byte binary array is produced). A message digest is a technique that is used in conjunction with message authentication codes to ensure message integrity. Mainly using the one-way hash function algorithm, can be used to verify the integrity of the message, and by hashing the password directly in the form of text preservation, etc., the current widely used algorithms are MD4, MD5, SHA-1.
The message digest has two basic properties:
- Two different messages difficult to generate the same digest
- It is difficult to generate a message for the specified digest, which can be extrapolated from the specified digest
Representative: SHA1 of the American National Institute of Standards and Technology and MIT Ronald Rivest proposed MD5
(b) Encrypt the string
/** using MD5 for encryption
* @param str string to encrypt
* @return The encrypted string
* @throws NoSuchAlgorithmException does not have this algorithm for generating message digests
* @throws unsupportedencodingexception
*/
public string EncoderByMd5 (String str) throws NoSuchAlgorithmException, Unsupportedencodingexception{
Determine the calculation method
MessageDigest md5=messagedigest.getinstance ("MD5");
Base64encoder base64en = new Base64encoder ();
The encrypted string
String Newstr=base64en.encode (Md5.digest (Str.getbytes ("Utf-8"));
return newstr;
}
Call Function:
String str= "0123456789"
SYSTEM.OUT.PRINTLN (ENCODERBYMD5 (str));
Output: eb5ejf1ptwaxm4bijspyxw==
(iii) Verify that the password is correct
Because MD5 is based on the principle of message digest, the basic feature of Message digest is that it is difficult to deduce the message from the digest, so to verify the password is correct, we must recalculate the digest of the input password (message packet) and compare it with the digest stored in the database (that is, the digest of the user's password is stored in the database). If the two summaries are the same, the password is correct and different, then the password is incorrect.
/** determine if the user's password is correct
* @param newpasswd User-entered password
* @param the password stored in the OLDPASSWD database-a summary of the user's password
* @return
* @throws nosuchalgorithmexception
* @throws unsupportedencodingexception
*/
public boolean Checkpassword (String newpasswd,string oldpasswd) throws NoSuchAlgorithmException, Unsupportedencodingexception{
if (EncoderByMd5 (NEWPASSWD). Equals (OLDPASSWD))
return true;
Else
return false;
}
Java implementation of MD5 encryption