Using MD5 for encryption in Java

Source: Internet
Author: User
Tags md5 encryption sha1

Java in the use of MD5 encryption in the development of various application systems, often need to store user information, many places to store user passwords, and the user password directly stored on the server is obviously not secure, this article briefly introduces the common MD5 encryption algorithm in the work, hope to be able to initiate.
(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). If the news A technique 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:
    1. Two different messages difficult to generate the same digest
    2. 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;
}Transfer from https://www.cnblogs.com/weiwangnuanyang/articles/4326336.html

Using MD5 for encryption in Java

Related Article

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.