MD5 is an irreversible encryption algorithm
A message digest is a secure, one-way hash function that converts string data of any size to a fixed-length hash value.
The encrypted string typically has 8-bit, 16-bit, 32-bit, 64-bit characters that do not seem to have these three-length strings
Encryption of 32-bit string lengths
Messagedigest.getinstance (String algorithm)
This method can be used to obtain three instances of cryptographic objects
MD5, SHA-1, SHA-256
1, get the MessageDigest instance of MD5 encryption algorithm,
2, Md.update (Readencryptstr.getbytes ()) to convert the encrypted data to a byte array is updated to the MD5 object instance of the byte array saved up.
3. Complete the hash calculation by performing final operations such as padding. Returns the encrypted result, which is the 128-byte data
4. Convert 128-bit, 16-byte data to 16-binary data output a total of 32-length strings
intrger.tohexstring (int i)
This method is to convert a shape into a 16-binary string, since int is 32 bits, and the parameter is byte 8-bit, and the hexadecimal number oxff is required and the operation will be preceded by a method of 24 bit 0 processing after the parameter
The result is two hexadecimal digits, but if the number is less than 10, the method will only return the hexadecimal character, need to be preceded by a 0, and then append the returned result.
The result of the final output is a 32-length hexadecimal string
/*** MD5 32bit Encrypt Methods. * @paramReadyencryptstr Ready Encrypt String *@returnstring Encrypt result String *@throwsnosuchalgorithmexception **/ Public Static FinalString Md5_32bit (String readyencryptstr)throwsnosuchalgorithmexception{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 stringStringBuilder su =NewStringBuilder (); //byte array switch hexadecimal number. for(intoffset = 0,blen = B.length; Offset < Blen; offset++) {String Haxhex= Integer.tohexstring (B[offset] & 0xFF); if(Haxhex.length () < 2) {su.append ("0"); } su.append (Haxhex); } returnsu.tostring (); }Else{ return NULL; } }
Baidu Encyclopedia's
Java version of the original encryption algorithm
Public classmd5{/** Four link variables*/ Private Final inta=0x67452301; Private Final intB=0xefcdab89; Private Final intC=0x98badcfe; Private Final intd=0x10325476; /*temporary variables for *ABCD*/ Private intatemp,btemp,ctemp,dtemp; /** Constant TI * formula: Floor (ABS (sin (i+1)) x (2POW32)*/ Private Final intk[]={ 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}; /** Left Displacement number, calculation method unknown*/ Private Final ints[]={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 voidinit () {atemp=A; Btemp=C; Ctemp=b; Dtemp=D; } /** Move a certain number of digits*/ Private intShiftintAints) { return(a<<s) | (A>>> (32-s));//when you move to the right, the high level must be 0, not the complement sign . } /** Main Loop*/ Private voidMainloop (intm[]) { intf,g; intA=atemp; intb=btemp; intC=temp; intD=dtemp; for(inti = 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; } inttmp=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; } /** Fill function * After processing should satisfy bits≡448 (mod512), Byte is bytes≡56 (mode64) * Filling method is first plus a 0, the other bit 0 * Last plus 64 bits of the original length*/ Private int[] Add (String str) {intNum= ((Str.length () +8)/64) +1;//a set of 512-bit, 64-byte intstrbyte[]=New int[NUM*16];//64/4=16, so there are 16 integers for(inti=0;i<num*16;i++) {//Initialize all 0Strbyte[i]=0; } inti; for(I=0;i<str.length (); i++) {Strbyte[i>>2]|=str.charat (i) << ((i%4) *8);//An integer that stores four bytes, a small- endian} strbyte[i>>2]|=0x80<< ((i%4) *8);//trailing add 1 /** Add the original length, length refers to the length of the bit, so to multiply 8, then the small end sequence, so placed in the penultimate, where the length of only 32 bits*/Strbyte[num*16-2]=str.length () *8; returnStrbyte; } /** Call Function*/ Publicstring getMD5 (string source) {init (); intstrbyte[]=Add (source); for(inti=0;i<strbyte.length/16;i++){ intnum[]=New int[16]; for(intj=0;j<16;j++) {Num[j]=strbyte[i*16+J]; } mainloop (num); } returnChangehex (atemp) +changehex (btemp) +changehex (ctemp) +Changehex (dtemp); } /** Integer becomes 16 binary string*/ PrivateString Changehex (inta) {String str=""; for(inti=0;i<4;i++) {str+=string.format ("%2s", Integer.tohexstring ((a>>i*8)% (1<<8))). Replace (', ' 0 '); } returnstr; } /** Single Case*/ Private StaticMD5 instance; Public StaticMD5 getinstance () {if(instance==NULL) {instance=NewMD5 (); } returninstance; } PrivateMD5 () {}; Public Static voidMain (string[] args) {String str=md5.getinstance (). GetMD5 (""); System.out.println (str); }}
MD5 Encryption algorithm