Using MD5 in Java

Source: Internet
Author: User
Use MD5 for encryption in Java

In the development of various application systems, user information is often stored. In many places, user passwords must be stored, and direct storage of user passwords on servers is obviously insecure, this article briefly introduces the commonly used MD5 encryption algorithms in the work, and hopes to introduce them to others.
(1) Brief Introduction to messages
A message digest is a digital fingerprint of a data block. Calculate a data block of any length and generate a unique fingerprint (for sha1, a 20-byte binary array ). Message Digest is a technology used in combination with message authentication codes to ensure message integrity. One-way hash function algorithms are mainly used to verify the integrity of messages and store messages in text format using hashed passwords. Currently, md4, MD5, and SHA-1 are widely used algorithms.

The message digest has two basic attributes:

  1. It is difficult for two different packets to generate the same abstract.
  2. It is difficult to generate a message for a specified digest, but the specified Digest can be reversed by the message.

Representatives: sha1 of the National Institute of Standards and Technology and MD5 proposed by Ronald Rivest of the Massachusetts Institute of Technology

(2) encrypt strings/*** // use MD5 for encryption
* @ Param STR the string to be encrypted
* @ Return encrypted string
* @ Throws nosuchalgorithmexception does not have this algorithm for generating message summarization.
* @ Throws unsupportedencodingexception
*/
Public String encoderbymd5 (string Str) throws nosuchalgorithmexception, unsupportedencodingexception ...{
// Determine the calculation method
Messagedigest MD5 = messagedigest. getinstance ("MD5 ");
Base64encoder base64en = new base64encoder ();
// Encrypted string
String newstr = base64en. encode (md5.digest (Str. getbytes ("UTF-8 ")));
Return newstr;
}

Call the function:
String STR = "0123456789"
System. Out. println (encoderbymd5 (STR ));
Output: eb5ejf1ptwaxm4bijspyxw =
(3) Verify that the password is correct
Because MD5 is based on the message digest principle, it is difficult to calculate the message digest Based on the Digest. to verify that the password is correct, you must enter the password (Message Digest) recompute the digest and compare it with the digest stored in the database (that is, the Digest of the user password is actually stored in the database). If the two digests are the same, the password is correct and different, the password is incorrect.

/***** // Determines whether the user password is correct.
* @ Param newpasswd the password entered by the user
* @ Param oldpasswd password stored in the database-User Password Summary
* @ 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;
}

References
Java-based Encryption Algorithm Implementation example
Java encryption and digital signature programming Quick Start

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.