Home > Back-end >  C algorithm md5 problem
C algorithm md5 problem

Time:09-23

Ever bosses know why the md5 algorithm, under 32 bytes of data encryption will not make a mistake, for more than 32 and 32 bytes of data encryption is the result is not the same md5 encryption tools online and online?
Code:

Void MD5: : MD5Encode
(
_In_ const char * text,
_In_ size_t len,
_In_ char * DST
)
{
//group
Short the output [16].
Char dest [16].
//SHORT_MD5_LEN is exactly equal to the length of the 16 said the output
Deposit//CHAR_MD5_LEN=34, said in the output string length + 2
Memset (dest, 0, SHORT_MD5_LEN);
Memset (DST, 0, CHAR_MD5_LEN);
//four groups of magic number
Unsigned int [] h={x98badcfe x67452301 0, 0 xefcdab89, 0, 0 x10325476};
The static const unsigned int k [64]=
{
X242070db xd76aa478 0, 0 xe8c7b756, 0, 0 xc1bdceee,
Xa8304613 xf57c0faf 0, 0 x4787c62a, 0, 0 xfd469501,
Xffff5bb1 x698098d8 0, 0 x8b44f7af, 0, 0 x895cd7be,
Xa679438e x6b901122 0, 0 xfd987193, 0, 0 x49b40821,
X265e5a51 xf61e2562 0, 0 xc040b340, 0, 0 xe9b6c7aa,
Xd8a1e681 xd62f105d 0, 0 x02441453, 0, 0 xe7d3fbc8,
Xf4d50d87 x21e1cde6 0, 0 xc33707d6, 0, 0 x455a14ed,
X676f02d9 xa9e3e905 0, 0 xfcefa3f8, 0, 0 x8d2a4c8a,
X6d9d6122 xfffa3942 0, 0 x8771f681, 0, 0 xfde5380c,
Xf6bb4b60 xa4beea44 0, 0 x4bdecfa9, 0, 0 xbebfbc70,
Xd4ef3085 x289b7ec6 0, 0 xeaa127fa, 0, 0 x04881d05,
X1fa27cf8 xd9d4d039 0, 0 xe6db99e5, 0, 0 xc4ac5665,
Xab9423a7 xf4292244 0, 0 x432aff97, 0, 0 xfc93a039,
Xffeff47d x655b59c3 0, 0 x8f0ccc92, 0, 0 x85845dd1,
Xa3014314 x6fa87e4f 0, 0 xfe2ce6e0, 0, 0 x4e0811a1,
X2ad7d2bb xf7537e82 0, 0 xbd3af235, 0, 0 xeb86d391
};
//round loop (GG, FF, HH, II) cycle each round every step to displacement digits
The static const unsigned int qz []=
{
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
};
The static const unsigned int [] s=
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,
5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,
0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9
};

Unsigned int H [4], I, j, f, TMP, R.
//N * 512 + 448
//N=(data length + 64 (bit))/512 (bit),
//arbitrary data length + 64 - bit is just 512 multiples
Size_t n_len=(len/64) * 64 + 56;
Unsigned char * n_text=(unsigned char *) malloc (n_len + 8);
Memset (n_text, 0, n_len + 8);
Memcpy (n_text, text, len);
//add the binary 1000 0000
N_text [len]=0 x80;
Len n_text [n_len]=(unsigned int) & lt; <3;
//every 64 bytes (512)
//processing time, because the number is just after the filling in multiples of 64
for (j=0; J & lt; N_len; J +=64)
{
Memcpy (H, H, 4 * sizeof (unsigned int));
//loop 64 times, is divided into four groups
for (i=0; i <64; I++)
{
//four groups of nonlinear functional operation, with the switch statement to determine how many groups of
//0 ~ 16 the first group, 16 ~ 32 second group
//32 ~ 48 a third group, 48 ~ 64 the fourth group
Int the UI=I/16;
The switch (UI)
{
//H [1]=X, H [2]=Y, H [3]=Z
//F (X, Y, Z)
Case: 0 f=(H [1] & amp; [2]) | (H (H ~ [1]) & amp; H [3]); break;
//G (X, Y, Z)
Case 1: f=(H [3] & amp; H [1]) | (H [2] & amp; (the H ~ [3])); break;
//H (X, Y, Z)
Case 2: f=[1] H ^ H [2] ^ H [3]. break;
//I
Case 3: f=H [2] ^ (H [1] | H (~ [3])); break;
}
//the abcd swap places respectively
TMP=H [3];
H [3]=H [2];
H [1] [2]=H;
//R=(a +? (BCD) + M? + ti)
R=H [0] + [I] f + k + ((unsigned int *) n_text) [s + j] [I];
//b + (a +? (BCD) + M? + ti) & lt; H [1] + [1]=H (R & lt; (32 - qz [I])));
H [0]=TMP;
}
//cycle after the end of each round, the ABCD respectively with the ABCD addition
for (i=0; i <4. I++) h +=h [I] [I];
}

Free (n_text);
Memcpy (dest, h, 16);

//with 0 XFF to high ff to 00
for (int i=0; i <16. I++)
The output [I]=dest [I] & amp; 0 XFF.
//prints hexadecimal data into string
for (int i=0; i Sprintf_s (DST + I * 2, 3, "% 02 x", the output [I]);
}
Using a string a tried once personally string length more than 32 bytes is the result and the result of the online tools is not the same as
Less than 32:


More than 32


CodePudding user response:

It's too hard,,,

CodePudding user response:

Data length filling, there is 8 bytes, you just fill the 1 byte.
When the length exceeds 32 bytes (256 bits) original length is wrong.
Len n_text [n_len]=(unsigned int) & lt; <3;
Change
* (unsigned long long *) (n_text + n_len)=(unsigned long long) len & lt; <3;

So, this code can correct calculation is less than 56 bytes of MD5,
More than there are still problems.

CodePudding user response:

Your length calculation formula is wrong,
Size_t n_len=(len/64) * 64 + 56;
When the len=58, this will work out n_len=56, but smaller than len.

CodePudding user response:

reference double cup with wine reply: 3/f
your length calculation formula is wrong,
Size_t n_len=(len/64) * 64 + 56;
When the len=58, this will work out n_len=56, but smaller than len.

For more than 56 bytes of data that have what way

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related