Java language MD5 encryption

Java language MD5 encryption


 

Background note

In a real project, for safety considerations , often require account password cipher text is encrypted , saved to the database .

In this way, even if someone acquires a cipher text password database, I do not know what the plaintext password information to prevent malicious access to the system.

Password encryption There are many ways, such as: Base64 , DSA , RSA , MD5 , SHA128 , SHA256 , SHA512 , etc. encryption.

This paper describes the MD5 encryption.


 

MD5 Profile

MD5 message digest algorithm (English: MD5 M essage - D igest Algorithm), a widely used cryptographic hash function , can produce a 128-bit (16 byte ) hash value (hash value), to ensure complete and consistent information transmission.

MD5 by American cryptographers Ron Rivest , in 1992 public (Ronald Linn Rivest) designed to replace the MD4 algorithm.


 

 MD5 application

1, data encryption

Take advantage of its one-way encryption of , that is irreversible , we can apply to encrypted data and passwords .

Since the one-way MD5 algorithm, MD5 code calculated from the original is almost impossible. At present, for ways to break this encryption method is to collect commonly used passwords forms, such as date of birth, ID number, telephone number and so on. The secret of these collected after treatment with MD5 be stored, and then to compare with the need to restore MD5, when the data collection reaches a certain number, the user's password is compromised the possibility will become very large.

For this method, a way of strengthening the security of the method is to add a value in the vicinity of the original password when user password MD5 processing, for example, the original password is "password", the processing time becomes "passworddrowssap", after this can effectively reduce the possibility of your password being compromised after treatment.

 

2, confirm that the file has been tampered with

Each file can calculate a specific of the MD5 value .

For example, we upload a file to the server can be simultaneously calculated MD5 value corresponding to the file;

When the downloaded file, by calculating the value of MD5 of the file again.

If the two MD5 values ​​are the same, indicating that the file has not changed, otherwise, the documentation has been modified.


 

Java language MD5 encryption

Programming through Java, to achieve arbitrary string MD5 encryption.

1, the first version of the code ( original version )

code show as below:

Package com.miracle.luna.md5; 

Import java.io.UnsupportedEncodingException;
 Import the java.security.MessageDigest;
 Import java.security.NoSuchAlgorithmException; 

/ ** 
 * 2019/11/18 the Created by Miracle Luna ON 
 * / 
public  class Md5UtilOriginal { 

    / ** 
     * the MD5 encrypted data, and output format hexadecimal string 
     * @param data 
     * @return 
     * / 
    public  static string MD5 (data string) {
         the try {
             byte [] = MD5 MD5 (data.getBytes ( "UTF-. 8" ));
             returntoHexString (MD5); 
        } the catch (UnsupportedEncodingException E) { 
            e.printStackTrace (); 
        } 
        return "" ; 
    } 

    / ** 
     * MD5 encrypted byte array 
     * @param Data 
     * @return 
     * / 
    public  static  byte [] MD5 ( byte [] Data) {
         the try { 
            the MessageDigest MD = MessageDigest.getInstance ( "MD5" );
             return md.digest (Data); 
        } the catch  (NoSuchAlgorithmException E) {
            e.printStackTrace (); 
        }
         return  new new  byte [] {}; 
    } 

    / ** 
     * encrypted byte array, converted to a hexadecimal string 
     * @param MD5 
     * @return 
     * / 
    Private  static String toHexString ( byte [] MD5) { 
        the StringBuilder SB = new new the StringBuilder (); 
        System.out.println ( "md5.length:" + md5.length);
         for ( byte B: MD5) { 
            sb.append (Integer.toHexString (B & 0xFF )); 
        } 
        return SB .toString (); 
    } 

    public static void main(String[] args) {
        String password = "password";
        String md5HexStr = md5(password);
        System.out.println("==> MD5 加密前: " + password);
        System.out.println("==> MD5 加密后: " + md5HexStr);
    }
}

 

Results are as follows:

md5.length: 16 
==> before the MD5: password
 ==> MD5 encrypted: 5f4dcc3b5aa765d61d8327deb882cf99

 

2, Second Edition codes ( Lite )

code show as below:

Package Penalty for com.miracle.luna.md5; 

Import java.nio.charset.StandardCharsets;
 Import java.security.MessageDigest;
 Import java.security.NoSuchAlgorithmException; 

/ ** 
 * the Created by Miracle Luna ON 2019/11/18 
 * / 
public  class {Md5UtilSimple 

    / ** 
     * the MD5 encrypted data, and output format hexadecimal string 
     * @param data 
     * @return 
     * / 
    public  static string MD5 (data string) { 
        the StringBuilder SB = new new the StringBuilder ();
         the try { 
            the MessageDigest mdMessageDigest.getInstance = ( "MD5" );
             byte [] = MD5 md.digest (data.getBytes ( StandardCharsets.UTF_8 )); 

            // the data is converted to hexadecimal bytes 
            for ( byte B: MD5) { 
                SB .append (Integer.toHexString (B & 0xFF )); 
            } 
        } the catch (NoSuchAlgorithmException E) { 
            e.printStackTrace (); 
        } 
        return sb.toString (); 
    } 

    public  static  void main (String [] args) { 
        String password = "password" ;
        Md5HexStr String = MD5 (password); 
        System.out.println ( "==> before the MD5:" + password); 
        System.out.println ( "==> MD5 encrypted:" + md5HexStr); 
    } 
}

 

Results are as follows:

==> before the MD5 encryption: password
 ==> MD5 encrypted: 5f4dcc3b5aa765d61d8327deb882cf99

 

3, the third edition of the code ( the final optimized version )

code show as below:

Package com.miracle.luna.md5; 

// needed here incorporated 1.13.jar-CODEC-Commons 
Import org.apache.commons.codec.binary.Hex ; 

Import java.nio.charset.StandardCharsets;
 Import the java.security.MessageDigest ;
 Import java.security.NoSuchAlgorithmException; 

/ ** 
 * 2019/11/18 the Created by Miracle Luna ON 
 * / 
public  class Md5Util { 

    / ** 
     * the MD5 encrypted data, and output format hexadecimal string 
     * @param Data 
     * @return 
     * / 
    public  static String MD5 (Data String) {
         the try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return Hex.encodeHexString(md.digest(data.getBytes(StandardCharsets.UTF_8)));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) {
        String password = "password";
        String md5HexStr = md5(password);
        System.out.println("==> MD5 加密前: " + password);
        System.out.println("==> After the MD5:" + md5HexStr); 
    } 
}

 

Results are as follows:

==> before the MD5 encryption: password
 ==> MD5 encrypted: 5f4dcc3b5aa765d61d8327deb882cf99

commons-codec-1.13.jar of pom reference as follows:
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.13</version>
        </dependency> 



Online MD5 encryption & decryption tool

1, MD5 encryption tool online

http://www.bejson.com/enc/md5/  (not only supports MD5 encryption, also supports Base64, SHA, etc.)

Results are as follows (in the string input box "before encryption" requires the encrypted MD5 -> click "MD5 encryption" -> to see the encrypted ciphertext "encrypted" in display box ):

 

2, MD5 decryption online

1) https://www.somd5.com/

Results are as follows (the input box MD5 cipher text to be decrypted, to decrypt the decryption click):

 

2)  https://www.cmd5.com/  (This is a very powerful decryption tool, not only supports MD5, SHA also supports decryption of)

Results are as follows (MD5 ciphertext input by the ciphertext input box and click "decryption", to see the plaintext MD5):

 

Decryption many species, the choice as follows:


 

MD5 Password Security Optimization

Since the password there is a certain security risk (the possibility to crack the violence) is now directly use the MD5 algorithm, we can according to the actual needs of the password encryption process to do some logic.

For example, the following logic:

1) password -> Base64 encryption, password A to obtain encrypted;

2) password -> Flip give drowssap -> MD5 encrypted password B to obtain encrypted;

3) Password Password A + B splice -> MD5 encrypted, the encrypted password obtained C;

4) password C -> reversing operation, generate the password D, into the database as the final ciphertext password.

This enhances password security, reduce the likelihood of being cracked. (Herein specific logic optimization, according to actual needs and personal preferences, the flexibility to define the complexity of its transformation).  


 

[Supplementation] 

When the MD5 password stored in the database, when the user logs in, the front end of the plain text password entered by the user to the rear end of the transmission.

MD5 encrypted program rear end thereof by the same processing logic comparison encrypted password in the database the user's cipher text password.

If they are equal, indicating that the user enters the correct password, allowing access to the system; otherwise, indicating that the password is incorrect, prohibit access.

 

In addition, to security considerations system , preventing malicious violence to try to crack the code , we often restrict the user to enter a password error of times .

For example, consecutive 5 times or three times the input password, will automatically lock the account a period of time (based on the actual scene, setting a long lock).

 

About Base64 encryption & decryption of the Java language, please refer to the blog: https://www.cnblogs.com/miracle-luna/p/11128734.html

 

I hope to help you, thank you!

Guess you like

Origin www.cnblogs.com/miracle-luna/p/11879447.html