You are on page 1of 6

Page no.

Aim: To Implement MD5 Algorithm


Theory:
The MD5 (message-digest algorithm) is a widely used cryptographic hash function producing a
128-bit (16-byte) hash value, typically expressed in text format as a 32
digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic
applications, and is also commonly used to verify data integrity.
MD5 was designed by Ronald Rivest in 1991 to replace an earlier hash function, MD4.
Description of Algorithm:
Takes as input a message of arbitrary length and produces as output a 128 bit fingerprint or
message digest of the input.
It is conjectured that it is computationally infeasible to produce two messages having the same
message digest.
Intended where a large file must be compressed in a secure manner before being encrypted
with a private key under a public-key cryptosystem such as PGP.
MD5 Algorithm:
Suppose a b-bit message as input, and that we need to find its message digest.

Step 1: Append padding bits


The input message is "padded" (extended) so that its length (in bits) equals to 448 mod
512. Padding is always performed, even if the length of the message is already 448 mod 512.
Padding is performed as follows: a single "1" bit is appended to the message, and then
"0" bits are appended so that the length in bits of the padded message becomes congruent to 448
mod 512. At least one bit and at most 512 bits are appended.
Step 2: Append length
A 64-bit representation of the length of the message is appended to the result of step1. If
the length of the message is greater than 2^64, only the low-order 64 bits will be used.

Page no.

The resulting message (after padding with bits and with b) has a length that is an exact multiple
of 512 bits. The input message will have a length that is an exact multiple of 16 (32-bit) words.
Step 3: Initialize MD buffer
A four-word buffer (A, B, C, D) is used to compute the message digest. Each of A, B, C,
D is a 32-bit register. These registers are initialized to the following values in hexadecimal, loworder bytes first):
word A: 01 23 45 67
word B: 89 ab cd ef
word C: fe dc ba 98
word D: 76 54 32 10
Step 4: Process message in 16-word blocks
Four functions will be defined such that each function takes an input of three 32-bit
words and produces a 32-bit word output.
F (X, Y, Z) = XY or not (X) Z
G (X, Y, Z) = XZ or Y not (Z)
H (X, Y, Z) = X xor Y xor Z
I (X, Y, Z) = Y xor (X or not (Z))
if the bits of X, Y, and Z are independent and unbiased, the each bit of F(X,Y,Z), G(X,Y,Z),
H(X,Y,Z), and I(X,Y,Z) will be independent and unbiased.
Applications of MD5 Algorithm:
MD5 digests have been widely used in the software world to provide some assurance that a
transferred file has arrived intact. For example, file servers often provide a pre-computed MD5
(known asMd5sum) checksum for the files, so that a user can compare the checksum of the
downloaded file to it. Most unix-based operating systems include MD5 sum utilities in their
distribution packages; Windows users may install a Microsoft utility or use third-party
applications. Android ROMs also utilize this type of checksum.
However, now that it is easy to generate MD5 collisions, it is possible for the person who created
the file to create a second file with the same checksum, so this technique cannot protect against
some forms of malicious tampering. Also, in some cases, the checksum cannot be trusted (for
example, if it was obtained over the same channel as the downloaded file), in which case MD5
can only provide error-checking functionality: it will recognize a corrupt or incomplete
download, which becomes more likely when downloading larger files.
MD5 can be used to store a one-way hash of a password, often with key stretching. Along with
other hash functions, it is also used in the field of electronic discovery, in order to provide a
unique identifier for each document that is exchanged during the legal discovery process. This
method can be used to replace the Bates stamp numbering system that has been used for decades
during the exchange of paper documents.

Page no.

Program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
typedef union uwb {
unsigned w;
unsigned char b[4];
} MD5union;
typedef unsigned DigestArray[4];
unsigned func0( unsigned abcd[] ){
return ( abcd[1] & abcd[2]) | (~abcd[1] & abcd[3]);}
unsigned func1( unsigned abcd[] ){
return ( abcd[3] & abcd[1]) | (~abcd[3] & abcd[2]);}
unsigned func2( unsigned abcd[] ){
return abcd[1] ^ abcd[2] ^ abcd[3];}
unsigned func3( unsigned abcd[] ){
return abcd[2] ^ (abcd[1] |~ abcd[3]);}
typedef unsigned (*DgstFctn)(unsigned a[]);
unsigned *calctable( unsigned *k)
{
double s, pwr;
int i;
pwr = pow( 2, 32);
for (i=0; i<64; i++) {
s = fabs(sin(1+i));
k[i] = (unsigned)( s * pwr );
}
return k;
}
unsigned rol( unsigned r, short N )
{
unsigned mask1 = (1<<N) -1;
return ((r>>(32-N)) & mask1) | ((r<<N) & ~mask1);
}
unsigned *md5( const char *msg, int mlen)
{
/*Initialize Digest Array as A , B, C, D */
static DigestArray h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 };
static DgstFctn ff[] = { &func0, &func1, &func2, &func3 };
static short M[] = { 1, 5, 3, 7 };
static short O[] = { 0, 1, 5, 0 };

Page no.

static short rot0[] = { 7,12,17,22};


static short rot1[] = { 5, 9,14,20};
static short rot2[] = { 4,11,16,23};
static short rot3[] = { 6,10,15,21};
static short *rots[] = {rot0, rot1, rot2, rot3 };
static unsigned kspace[64];
static unsigned *k;
static DigestArray h;
DigestArray abcd;
DgstFctn fctn;
short m, o, g;
unsigned f;
short *rotn;
union {
unsigned w[16];
char b[64];
}mm;
int os = 0;
int grp, grps, q, p;
unsigned char *msg2;
if (k==NULL) k= calctable(kspace);
for (q=0; q<4; q++) h[q] = h0[q]; // initialize
{
grps = 1 + (mlen+8)/64;
msg2 = malloc( 64*grps);
memcpy( msg2, msg, mlen);
msg2[mlen] = (unsigned char)0x80;
q = mlen + 1;
while (q < 64*grps){ msg2[q] = 0; q++ ; }
{
MD5union u;
u.w = 8*mlen;
q -= 8;
memcpy(msg2+q, &u.w, 4 );
}
}
for (grp=0; grp<grps; grp++)
{
memcpy( mm.b, msg2+os, 64);
for(q=0;q<4;q++) abcd[q] = h[q];
for (p = 0; p<4; p++) {
fctn = ff[p];
rotn = rots[p];
m = M[p]; o= O[p];
for (q=0; q<16; q++) {

Page no.

g = (m*q + o) % 16;
f = abcd[1] + rol( abcd[0]+ fctn(abcd) + k[q+16*p] + mm.w[g], rotn[q%4]);
abcd[0] = abcd[3];
abcd[3] = abcd[2];
abcd[2] = abcd[1];
abcd[1] = f;
}
}
for (p=0; p<4; p++)
h[p] += abcd[p];
os += 64;
}
return h;
}
int main()
{
int j,k;
unsigned *d;
MD5union u;
const char *msg = "WE ARE COMPUTER ENGINEERS!";
printf("\t MD5 ENCRYPTION ALGORITHM IN C \n\n");
printf("Input String to be Encrypted using MD5 : \n\t%s",msg);
d = md5(msg, strlen(msg));
printf("\n\n\nThe MD5 code for input string is : \n");
printf("\t= 0x");
for (j=0;j<4; j++){
u.w = d[j];
for (k=0;k<4;k++) printf("%02x",u.b[k]);
}
printf("\n");
printf("\n\t MD5 Encyption Successfully Completed!!!\n\n");
getch();
return 0;
}

Output:

Page no.

Conclusion:
Thus we Studied and Successfully Implemented MD5 Algorithm in C in Centos

You might also like