java learning -MD5 message digest algorithm

Original link: http://www.cnblogs.com/gne-hwz/p/9343199.html

md5 hash algorithm belongs to a class, the message digest is irreversible algorithm. Unlike the symmetric encryption and asymmetric encryption algorithm, the encryption key is not required.

note:

  md5 encryption algorithm not only for the algorithm to generate a unique data value after the hash calculation, without the encryption key nor the decryption key.

  Below that is the process of md5 encrypted password to encrypt 32-bit length of the string

md5 encrypted password can be used, such as 123456, encrypted string under conditions of large computer can not be forced to break out, cracks can be compared only by way of the same matching dictionary string after md5 encryption.

 

MessageDigest message digest is secure one-way hash function, the character string data of any size will be converted into a hash value of a fixed length.

Encrypted string generally has eight, 16-bit, 32-bit, (if there is no 64-bit) string of length three.

  After the default standard is directly encrypted 32-bit hexadecimal string length

 

After the 32-bit data and returns the encrypted data length

MessageDigest md = MessageDigest.getInstance(String algorithm)

This method can obtain three encryption object instance

MD5, SHA-1, SHA-256

1, to give examples MessageDigest MD5 algorithm,

2, md.update (readEncryptStr.getBytes ()) to be encrypted update data into an array of bytes into byte array object instance in md5 saved.

3, by performing the hash calculation is done like the final filling operation. Return the result of the encryption, i.e., 128 bytes of data

4, i.e. 16 converts the 128-byte data length string of 32 hexadecimal total length of the output data

Intrger.toHexString(int i)

This method is to convert an integer to a hexadecimal string, int is because 32-bit, and 8-bit parameters are Byte, and hexadecimal numbers need OxFF operation with the front 24, is set to 0 after treatment do as a method parameter

Get out of the result is two hexadecimal digits, but if the number is less than the number 10, the method simply returns a hexadecimal character, fill a need in front of 0, then an additional return results.

Last output is 32 hexadecimal string length

/** 
     * MD5 32bit Encrypt Methods. 
     * @param readyEncryptStr ready encrypt string 
     * @return String encrypt result string 
     * @throws NoSuchAlgorithmException  
     * */  
    public static final String MD5_32bit(String readyEncryptStr) throws NoSuchAlgorithmException{  
        if(readyEncryptStr != null){  
            //Get MD5 digest algorithm's MessageDigest's instance.  
            MessageDigest md = MessageDigest.getInstance("MD5");  
            //Use specified byte update digest.  
            md.update(readyEncryptStr.getBytes());  
            //Get cipher text  
            byte [] b = md.digest();  
            //The cipher text converted to hexadecimal string  
            StringBuilder su = new StringBuilder();  
            //byte array switch hexadecimal number.  
            for(int offset = 0,bLen = b.length; offset < bLen; offset++){  
                String haxHex = Integer.toHexString(b[offset] & 0xFF);  
                if(haxHex.length() < 2){  
                    su.append("0");  
                }  
                su.append(haxHex);  
            }  
            return su.toString();  
        }else{  
            return null;  
        }  
    }

 

It returns the encrypted string length three eight, 16-bit, 32 bit

Only know 16 is the result of 32-bit length of the string returned by the encrypted processing 16-bit length is taken as the return value from the middle of the 32-bit encrypted string.

8-bit encryption temporarily unclear.

 

From Baidu Encyclopedia

java version of the original encryption algorithm

public class MD5{
    /*
    * Four chaining variable
    */
    private final int A=0x67452301;
    private final int B=0xefcdab89;
    private final int C=0x98badcfe;
    private final int D=0x10325476;
    /*
    * ABCD temporary variables
    */
    private int Atemp,Btemp,Ctemp,Dtemp;
     
    /*
    * Constants ti
    * Equation: floor (abs (sin (i + 1)) × (2pow32)
    */
    private final int K[]={
        0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
        0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,0x698098d8,
        0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,
        0xa679438e,0x49b40821,0xf61e2562,0xc040b340,0x265e5a51,
        0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
        0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,
        0xfcefa3f8,0x676f02d9,0x8d2a4c8a,0xfffa3942,0x8771f681,
        0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,
        0xbebfbc70,0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
        0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,0xf4292244,
        0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,
        0xffeff47d,0x85845dd1,0x6fa87e4f,0xfe2ce6e0,0xa3014314,
        0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391};
    /*
    * Number leftward displacement, calculated unknown
    */
    private final int s[]={7,12,17,22,7,12,17,22,7,12,17,22,7,
        12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
        4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,
        15,21,6,10,15,21,6,10,15,21,6,10,15,21};
     
     
    /*
    * Initialization function
    */
    private void init(){
        Atemp=A;
        Btemp=B;
        Ctemp=C;
        Dtemp=D;
    }
    /*
    * Certain mobile-digit
    * / 
    Private     int     Shift ( int A, int S) {
         return (A << S) | (A >>> (S-32)); // the right time, we must make zero the high, rather than supplement symbol bit 
    }
     / *
    * The main loop
    */
    private void MainLoop(int M[]){
        int F,g;
        int a=Atemp;
        int b=Btemp;
        int c=Ctemp;
        int d=Dtemp;
        for(int i = 0; i < 64; i ++){
            if(i<16){
                F=(b&c)|((~b)&d);
                g=i;
            }else if(i<32){
                F=(d&b)|((~d)&c);
                g=(5*i+1)%16;
            }else if(i<48){
                F=b^c^d;
                g=(3*i+5)%16;
            }else{
                F=c^(b|(~d));
                g=(7*i)%16;
            }
            int tmp=d;
            d=c;
            c=b;
            b=b+shift(a+F+K[i]+M[g],s[i]);
            a=tmp;
        }
        Atemp=a+Atemp;
        Btemp=b+Btemp;
        Ctemp=c+Ctemp;
        Dtemp=d+Dtemp;
     
    }
    /*
    * Filled Function
    * After treatment should meet bits≡448 (mod512), the byte is bytes≡56 (mode64)
    * A fill mode as the first plus 0, the other bits zero padding
    * Finally, the length of the original 64-bit plus
    * / 
    Private  int [] the Add (String STR) {
         int NUM = ((str.length () +. 8) / 64) + 1'd; // In 512, a set of 64 bytes 
        int strByte [] = new new  int [NUM * 16]; // 64 / = 16. 4, so there are 16 integer 
        for ( int I = 0; I <NUM * 16; I ++) { // all initialized to 0 
            strByte [I] = 0 ;
        }
        int    i;
        for(i=0;i<str.length();i++){
            strByte [I >> 2] | = str.charAt (I) << ((I. 4%) *. 8); // a four-byte integer store, the little-endian 
        }
        strByte [I >> 2] | = 0x80 << ((I. 4%) *. 8); // tail addition of 1 
        / *
        * Add the original length, the length of pointing, so by 8, and then the little-endian, so on the penultimate, where only a 32-bit length
        */
        strByte[num*16-2]=str.length()*8;
            return strByte;
    }
    /*
    *call function
    */
    public String getMD5(String source){
        init();
        int strByte[]=add(source);
        for(int i=0;i<strByte.length/16;i++){
        int num[]=new int[16];
        for(int j=0;j<16;j++){
            num[j]=strByte[i*16+j];
        }
        MainLoop (num);
        }
        return changeHex(Atemp)+changeHex(Btemp)+changeHex(Ctemp)+changeHex(Dtemp);
     
    }
    /*
    * Integer become a hexadecimal string
    */
    private String changeHex(int a){
        String str="";
        for(int i=0;i<4;i++){
            str+=String.format("%2s", Integer.toHexString(((a>>i*8)%(1<<8))&0xff)).replace(' ', '0');
 
        }
        return str;
    }
    /*
    * Singleton
    */
    private static MD5 instance;
    public static MD5 getInstance(){
        if(instance==null){
            instance=new MD5();
        }
        return instance;
    }
     
    private MD5(){};
     
    public static void main(String[] args){
        String str=MD5.getInstance().getMD5("");
        System.out.println(str);
    }
}

 

Reproduced in: https: //www.cnblogs.com/gne-hwz/p/9343199.html

Guess you like

Origin blog.csdn.net/weixin_30252709/article/details/94788726
Recommended