MD5 encryption ~ Display the password of a database in ciphertext

Source: Internet
Author: User

Public class md5code {<br/>/* <br/> * the following S11-S44 is actually a 4*4 matrix, which is implemented by # define in the original C implementation, here they are implemented as static <br/> * Final indicates read-only, it can be shared among multiple instances in the same process space <br/> */<br/> static final int S11 = 7; </P> <p> static final int S12 = 12; </P> <p> static final int S13 = 17; </P> <p> static final int S14 = 22; </P> <p> static final int S21 = 5; </P> <p> static final int s22 = 9; </P> <p> static final int S23 = 14; </P> <p> S Tatic final int S24 = 20; </P> <p> static final int s31 = 4; </P> <p> static final int s32 = 11; </P> <p> static final int s33 = 16; </P> <p> static final int s34 = 23; </P> <p> static final int s41 = 6; </P> <p> static final int S42 = 10; </P> <p> static final int s43 = 15; </P> <p> static final int s44 = 21; </P> <p> static final byte [] padding = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0, 0, 0 }; </P> <p>/* <br/> * the following three members are the three core data used in MD5 calculation, the md5_ctx structure is defined in the original C implementation. <br/> */<br/> private long [] State = new long [4]; // state (ABCD) </P> <p> private long [] Count = new long [2]; // number of bits, modulo 2 ^ 64 (LSB </P> <p>/First) </P> <p> private byte [] Buffer = new byte [64]; // input buffer </P> <p>/* <br/> * digesthexstr is the only public member of MD5, it is the hexadecimal ASCII representation of the latest calculation result. <br/> */</P> <p> Public String digesthexstr; </P> <p>/* <br/> * digest, is the binary representation of the latest calculation result, indicating the MD5 value of bits. <br/> */<br/> private byte [] digest = new byte [16]; </P> <p>/* <br/> * getmd5ofstr is the primary public method of MD5, the entry parameter is the string you want to perform MD5 transformation <br/> * returns the result of the transformation. This result is obtained from the public member digesthexstr. <br/> */<br/> Public String Getmd5ofstr (string inbuf) {<br/> md5init (); <br/> md5update (inbuf. getbytes (), inbuf. length (); <br/> md5final (); <br/> digesthexstr = ""; <br/> for (INT I = 0; I <16; I ++) {<br/> digesthexstr + = bytehex (Digest [I]); <br/>}< br/> return digesthexstr; <br/>}</P> <p> // This is the standard constructor of the MD5 class, javaBean requires a public constructor without parameters <br/> Public md5code () {<br/> md5init (); <br/> return; <br/>}</P> <p>/* md5ini T is an initialization function that initializes the core variable and loads the standard magic number */<br/> private void md5init () {<br/> count [0] = 0l; <br/> count [1] = 0l; <br/> // * load magic initialization constants. <br/> State [0] = 0x67452301l; <br/> State [1] = 0xefcdab89l; <br/> State [2] = 0x98badcfel; <br/> State [3] = 0x10325476l; <br/> return; <br/>}</P> <p>/* <br/> * F, G, h, I are four basic MD5 functions. In the original MD5 C implementation, because they are <br/> * simple bitwise operations, they may be implemented into macros for efficiency consideration. In Java, we implement them The private method is ready for use, and the name is kept in the original C. <Br/> */<br/> private long F (long X, long y, long Z) {<br/> return (X & Y) | ((~ X) & Z); <br/>}</P> <p> private long g (long X, long y, long Z) {<br/> return (X & z) | (Y &(~ Z); <br/>}</P> <p> private long H (long X, long y, long Z) {<br/> return x ^ y ^ Z; <br/>}</P> <p> private long I (long X, long y, long Z) {<br/> return y ^ (X | (~ Z); <br/>}</P> <p>/* <br/> * ff, GG, HH, and II will call F, G, H, I perform the next step transformation ff, GG, HH, and II transformations for <br/> * Rounds 1, 2, 3, and 4. rotation is separate from addition to prevent <br/> * recomputation. <br/> */<br/> private long ff (long a, long B, long C, long d, long X, long s, long AC) {<br/> A + = f (B, c, d) + x + AC; <br/> A = (INT) A <s) | (INT) A >>> (32-S); <br/> A + = B; <br/> return; <Br/>}</P> <p> private long Gg (long a, long B, long C, long d, long X, long s, long AC) {<br/> A + = g (B, c, d) + x + AC; <br/> A = (INT) A <s) | (INT) A >>> (32-S); <br/> A + = B; <br/> return; <br/>}</P> <p> private long HH (long a, long B, long C, long d, long X, long s, long AC) {<br/> A + = H (B, c, d) + x + AC; <br/> A = (INT) A <s) | (INT) A >>> (32-S); <br/> A + = B; <br/> RET Urn A; <br/>}</P> <p> private long II (long a, long B, long C, long d, long X, long s, long AC) {<br/> A + = I (B, C, D) + x + AC; <br/> A = (INT) A <s) | (INT) A >>> (32-S); <br/> A + = B; <br/> return; <br/>}</P> <p>/* <br/> * md5update is the main calculation process of MD5, inbuf is the byte string to be transformed, and inputlen is the length, this <br/> * function is called by getmd5ofstr. Before calling this function, you must call md5init, therefore, it is designed to be private <br/> */<br/> private void md5update (byte [] inbuf, int in Putlen) {<br/> int I, index, partlen; <br/> byte [] block = new byte [64]; <br/> Index = (INT) (count [0] >>> 3) & 0x3f; <br/> // * UPDATE Number of BITs */<br/> If (count [0] + = (inputlen <3 )) <(inputlen <3) <br/> count [1] ++; <br/> count [1] + = (inputlen >>> 29 ); <br/> partlen = 64-index; <br/> // transform as same times as possible. <br/> If (inputlen> = partlen) {<br/> md5memcpy (buffer, Inbuf, index, 0, partlen); <br/> md5transform (buffer); <br/> for (I = partlen; I + 63 <inputlen; I + = 64) {<br/> md5memcpy (Block, inbuf, 0, I, 64); <br/> md5transform (Block); <br/>}< br/> Index = 0; <br/>} else <br/> I = 0; <br/> // * buffer remaining input */<br/> md5memcpy (buffer, inbuf, index, i, inputlen-I ); <br/>}</P> <p>/* <br/> * sort and enter the output result in md5final mode. <br/> */<br/> private void md5fi Nal () {<br/> byte [] bits = new byte [8]; <br/> int index, padlen; <br/> // * save Number of BITs */<br/> encode (bits, Count, 8 ); <br/> // * pad out to 56 mod 64. <br/> Index = (INT) (count [0]> 3) & 0x3f; <br/> padlen = (index <56 )? (56-index): (120-index); <br/> md5update (padding, padlen); <br/> // * append length (before padding) */<br/> md5update (bits, 8); <br/> // * store state in Digest */<br/> encode (Digest, state, 16 ); <br/>}</P> <p>/* <br/> * md5memcpy is a block copy function of the byte array used internally, start from inpos of input to copy the LEN Length <br/> * bytes to the outpos position of output <br/> */<br/> private void md5memcpy (byte [] Output, byte [] input, int outpos, int in POs, <br/> int Len) {<br/> int I; <br/> for (I = 0; I <Len; I ++) <br/> output [outpos + I] = input [inpos + I]; <br/>}</P> <p>/* <br/> * md5transform is the MD5 core conversion program called by md5update, block is the original byte of the block <br/> */<br/> private void md5transform (byte block []) {<br/> long a = State [0], B = State [1], c = State [2], D = State [3]; <br/> long [] x = new long [16]; <br/> decode (x, block, 64); <br/>/* Round 1 */<br/> A = Ff (a, B, c, d, X [0], S11, 0xd76aa478l);/* 1 */<br/> d = ff (D, A, B, c, X [1], S12, 0xe8c7b756l);/* 2 */<br/> C = ff (c, d, A, B, X [2], S13, 0x242070dbl);/* 3 */<br/> B = ff (B, c, d, A, X [3], S14, 0xc1bdceeel ); /* 4 */<br/> A = ff (a, B, c, d, X [4], S11, 0xf57c0fafl ); /* 5 */<br/> d = ff (D, a, B, c, X [5], S12, 0x4787c62al ); /* 6 */<br/> C = ff (c, d, A, B, X [6], S13, 0xa8304613l);/* 7 */<br /> B = ff (B, c, d, A, X [7], S14, 0xfd469501l);/* 8 */<br/> A = ff (, b, c, d, X [8], S11, 0x698098d8l);/* 9 */<br/> d = ff (D, a, B, c, X [9], S12, 0x8b44f7afl);/* 10 */<br/> C = ff (c, d, A, B, X [10], S13, 0xffff5bb1l);/* 11 */<br/> B = ff (B, c, d, A, X [11], S14, 0x895cd7bel ); /* 12 */<br/> A = ff (a, B, c, d, X [12], S11, 0x6b901122l ); /* 13 */<br/> d = ff (D, a, B, c, X [13], S12, 0xfd987193l );/* 14 */<br/> C = ff (c, d, A, B, X [14], S13, 0xa679438el ); /* 15 */<br/> B = ff (B, c, d, A, X [15], S14, 0x49b40821l ); /* 16 */<br/>/* Round 2 */<br/> A = Gg (a, B, c, d, X [1], S21, 0xf61e2562l);/* 17 */<br/> d = Gg (D, a, B, c, X [6], s22, 0xc040b340l ); /* 18 */<br/> C = Gg (c, d, A, B, X [11], S23, 0x265e5a51l ); /* 19 */<br/> B = Gg (B, c, d, A, X [0], S24, 0xe9b6c7aal ); /* 20 */<br/> A = G G (A, B, C, D, X [5], S21, 0xd62f105dl);/* 21 */<br/> d = Gg (D, A, B, c, X [10], s22, 0x2441453l);/* 22 */<br/> C = Gg (c, d, A, B, X [15], S23, 0xd8a1e681l);/* 23 */<br/> B = Gg (B, c, d, A, X [4], S24, 0xe7d3fbc8l ); /* 24 */<br/> A = Gg (a, B, c, d, X [9], S21, 0x21e1cde6l ); /* 25 */<br/> d = Gg (D, a, B, c, X [14], s22, 0xc33707d6l ); /* 26 */<br/> C = Gg (c, d, A, B, X [3], S23, 0xf4d50d87l);/* 27 */<Br/> B = Gg (B, c, d, A, X [8], S24, 0x455a14edl ); /* 28 */<br/> A = Gg (a, B, c, d, X [13], S21, 0xa9e3e905l ); /* 29 */<br/> d = Gg (D, a, B, c, X [2], s22, 0xfcefa3f8l ); /* 30 */<br/> C = Gg (c, d, A, B, X [7], S23, 0x676f02d9l ); /* 31 */<br/> B = Gg (B, c, d, A, X [12], S24, 0x8d2a4c8al ); /* 32 */<br/>/* Round 3 */<br/> A = HH (a, B, c, d, X [5], s31, 0xfffa3942l);/* 33 */<br/> d = HH (D, a, B, C, X [8], s32, 0x8771f681l);/* 34 */<br/> C = HH (c, d, A, B, X [11], s33, 0x6d9d6122l);/* 35 */<br/> B = HH (B, c, d, A, X [14], s34, 0xfde5380cl ); /* 36 */<br/> A = HH (a, B, c, d, X [1], s31, 0xa4beea44l ); /* 37 */<br/> d = HH (D, a, B, c, X [4], s32, 0x4bdecfa9l ); /* 38 */<br/> C = HH (c, d, A, B, X [7], s33, 0xf6bb4b60l ); /* 39 */<br/> B = HH (B, c, d, A, X [10], s34, 0xbebfbc70l);/* 40 */<br/> A = HH (a, B, c, d, X [13], s31, 0x289b7ec6l);/* 41 */<br/> d = HH (D,, b, c, X [0], s32, 0xeaa1_fal);/* 42 */<br/> C = HH (c, d, A, B, X [3], s33, 0xd4ef3085l);/* 43 */<br/> B = HH (B, c, d, A, X [6], s34, 0x4881d05l ); /* 44 */<br/> A = HH (a, B, c, d, X [9], s31, 0xd9d4d039l ); /* 45 */<br/> d = HH (D, a, B, c, X [12], s32, 0xe6db99e5l ); /* 46 */<br/> C = HH (c, d, A, B, X [15], s33, 0x1fa27cf8l) ;/* 47 */<br/> B = HH (B, c, d, A, X [2], s34, 0xc4ac5665l ); /* 48 */<br/>/* Round 4 */<br/> A = II (a, B, c, d, X [0], s41, 0xf4292244l);/* 49 */<br/> d = II (D, a, B, c, X [7], S42, 0x432aff97l ); /* 50 */<br/> C = II (c, d, A, B, X [14], s43, 0xab9423a7l ); /* 51 */<br/> B = II (B, c, d, A, X [5], s44, 0xfc93a039l ); /* 52 */<br/> A = II (a, B, c, d, X [12], s41, 0x655b59c3l ); /* 53 */<br/> d = II (D, a, B, c, X [3], S42, 0x8f0ccc92l);/* 54 */<br/> C = II (c, d, A, B, X [10], s43, 0xffeff47dl);/* 55 */<br/> B = II (B, c, d, A, X [1], s44, 0x85845dd1l);/* 56 */<br/> A = II (a, B, c, d, X [8], s41, 0x6fa87e4fl ); /* 57 */<br/> d = II (D, a, B, c, X [15], S42, 0xfe2ce6e0l ); /* 58 */<br/> C = II (c, d, A, B, X [6], s43, 0xa3014314l ); /* 59 */<br/> B = II (B, c, d, A, X [13], s44, 0x4e0811a1l);/* 60 */<Br/> A = II (a, B, c, d, X [4], s41, 0xf7537e82l ); /* 61 */<br/> d = II (D, a, B, c, X [11], S42, 0xbd3af235l ); /* 62 */<br/> C = II (c, d, A, B, X [2], s43, 0x2ad7d2bbl ); /* 63 */<br/> B = II (B, c, d, A, X [9], s44, 0xeb86d391l ); /* 64 */<br/> State [0] + = A; <br/> State [1] + = B; <br/> State [2] + = C; <br/> State [3] + = D; <br/>}</P> <p>/* <br/> * encode splits the long array into byte arrays in sequence, Because Java's long type is 64bit, low splitting only 32bit to adapt to the use of the original C implementation <br/> */<br/> private void encode (byte [] output, long [] input, int Len) {<br/> int I, j; <br/> for (I = 0, j = 0; j <Len; I ++, J + = 4) {<br/> output [J] = (byte) (input [I] & 0 xffl); <br/> output [J + 1] = (byte) (input [I] >>> 8) & 0 xffl); <br/> output [J + 2] = (byte) (input [I] >>> 16) & 0 xffl); <br/> output [J + 3] = (byte) (input [I] >>> 24) & 0 xffl); <br/>}</P> <p>/ * <Br/> * decode combines byte arrays into long arrays in sequence, Because Java's long type is 64bit. <br/> * only low 32bit is merged, and high 32bit is cleared, to adapt to the use of the original C implementation <br/> */<br/> private void decode (long [] output, byte [] input, int Len) {<br/> int I, j; <br/> for (I = 0, j = 0; j <Len; I ++, J + = 4) <br/> output [I] = b2iu (input [J]) | (b2iu (input [J + 1]) <8) <br/> | (b2iu (input [J + 2]) <16) | (b2iu (input [J + 3]) <24 ); <br/> return; <br/>}</P> <p>/* <br/> * B2iu is a "Escalation" program I wrote that disregards the positive and negative signs of byte, because Java does not have the unsigned operation <br/> */<br/> Public static long b2iu (byte B) {<br/> return B <0? B & 0x7f + 128: B; <br/>}</P> <p>/* <br/> * bytehex (), it is used to convert the number of byte types into hexadecimal ASCII representation. <br/> * this is not possible because the tostring of byte in Java, we do not have sprintf (outbuf, "% 02x", IB) in C Language <br/> */<br/> Public static string bytehex (byte Ib) {<br/> char [] digit = {'0', '1', '2', '3', '4', '5', '6 ', '7', '8', '9', 'A', <br/> 'B', 'C', 'D', 'E ', 'F' }; <br/> char [] Ob = new char [2]; <br/> ob [0] = digit [(ib >>> 4) & 0x0f]; <br/> ob [1] = digit [IB & 0x0f]; <br/> string S = new string (OB); <br/> return S; <br/>}< br/>}

Usage:

Public static void main (string [] ARGs ){
Md5code MD5 = new md5code ();
String STR = "123456 ";
System. Out. println (md5.getmd5ofstr (STR ));
}

No matter how long the password is, the number of digits after encryption is fixed.

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.