您的位置:首页 > 编程语言 > C语言/C++

MD5算法C语言实现

2014-02-24 17:06 239 查看
 使用说明:

  将斜杠下面的源码复制到自己的源码中,在主函数之前加上函数声明,

  之后便可在主函数中调用调用该函数了。

  参数add是需要散列的数据的地址;

  参数MessageLen是需要散列的数据的长度(单位字节);

  参数add1是用来接受散列值的内存块的地址,该内存块的长度必须大于等于16字节;
Basisqqh(1181565087)Basisqqh

Otherqqh(1181565087)Otherqqh

Adandadd(http://192.168.1.178:9991)Adandadd

Usandadd(http://192.168.1.178:9993)Usandadd

Usiosadd(http://192.168.1.178:9994)Usiosadd

Uswpcadd(http://192.168.1.178:9996)Uswpcadd

Vavsgadd(http://192.168.1.178:9997)Vavsgadd

//////////////////////////////////////////////////////////////////////////

int md5( void *add,int MessageLen,void *add1)

{

   unsigned long length;
   unsigned long a,b,c,d;
   unsigned long A,B,C,D;
   unsigned long long raw;
   unsigned long buffer[16],temp;
   unsigned long offset=0,offset1,offset2;
   unsigned long count,count1,count2,count3=0;
   length=(unsigned long)MessageLen;
   A=a=0x67452301;
   B=b=0xEFCDAB89;
   C=c=0x98BADCFE;
   D=d=0x10325476;
   raw=length*8;
   offset1=length;
   offset2=length*8%512;
   if(offset2>=0&&offset2<=447)
     offset2=(512-offset2)/8;
   else
     offset2=(1024-offset2)/8;
   count=(offset2+length)/64;
   offset2=offset2+length-8;
   for(count1=1;count1<=count;count1++)

   {

      for(count2=0;count2<=63;count2++,offset++)

      {

         if(offset>=0&&offset<offset1)
           {*((unsigned char *)buffer+count2)=*((unsigned char *)add+offset);}
         else if (offset==offset1)
           {*((unsigned char *)buffer+count2)=128;}
         else if (offset>offset1&&offset<offset2)
           {*((unsigned char *)buffer+count2)=0;}
         else if (offset>=offset2)

         {

            *((unsigned char *)buffer+count2)=*((unsigned char *)(&raw)+count3);
            count3++;

         }

         else

         {;}
      }

      temp=a+(b&c|~b&d)+buffer[0]+0xd76aa478;
      a=b+(temp<<7|temp>>25);
      temp=d+(a&b|~a&c)+buffer[1]+0xe8c7b756;
      d=a+(temp<<12|temp>>20);
      temp=c+(d&a|~d&b)+buffer[2]+0x242070db;
      c=d+(temp<<17|temp>>15);
      temp=b+(c&d|~c&a)+buffer[3]+0xc1bdceee;
      b=c+(temp<<22|temp>>10);
      temp=a+(b&c|~b&d)+buffer[4]+0xf57c0faf;
      a=b+(temp<<7|temp>>25);
      temp=d+(a&b|~a&c)+buffer[5]+0x4787c62a;
      d=a+(temp<<12|temp>>20);
      temp=c+(d&a|~d&b)+buffer[6]+0xa8304613;
      c=d+(temp<<17|temp>>15);
      temp=b+(c&d|~c&a)+buffer[7]+0xfd469501;
      b=c+(temp<<22|temp>>10);
      temp=a+(b&c|~b&d)+buffer[8]+0x698098d8;
      a=b+(temp<<7|temp>>25);
      temp=d+(a&b|~a&c)+buffer[9]+0x8b44f7af;
      d=a+(temp<<12|temp>>20);
      temp=c+(d&a|~d&b)+buffer[10]+0xffff5bb1;
      c=d+(temp<<17|temp>>15);
      temp=b+(c&d|~c&a)+buffer[11]+0x895cd7be;
      b=c+(temp<<22|temp>>10);
      temp=a+(b&c|~b&d)+buffer[12]+0x6b901122;
      a=b+(temp<<7|temp>>25);
      temp=d+(a&b|~a&c)+buffer[13]+0xfd987193;
      d=a+(temp<<12|temp>>20);
      temp=c+(d&a|~d&b)+buffer[14]+0xa679438e;
      c=d+(temp<<17|temp>>15);
      temp=b+(c&d|~c&a)+buffer[15]+0x49b40821;
      b=c+(temp<<22|temp>>10);
      temp=a+(b&d|c&~d)+buffer[1]+0xf61e2562;
      a=b+(temp<<5|temp>>27);
      temp=d+(a&c|b&~c)+buffer[6]+0xc040b340;
      d=a+(temp<<9|temp>>23);
      temp=c+(d&b|a&~b)+buffer[11]+0x265e5a51;
      c=d+(temp<<14|temp>>18);
      temp=b+(c&a|d&~a)+buffer[0]+0xe9b6c7aa;
      b=c+(temp<<20|temp>>12);
      temp=a+(b&d|c&~d)+buffer[5]+0xd62f105d;
      a=b+(temp<<5|temp>>27);
      temp=d+(a&c|b&~c)+buffer[10]+0x02441453;
      d=a+(temp<<9|temp>>23);
      temp=c+(d&b|a&~b)+buffer[15]+0xd8a1e681;
      c=d+(temp<<14|temp>>18);
      temp=b+(c&a|d&~a)+buffer[4]+0xe7d3fbc8;
      b=c+(temp<<20|temp>>12);
      temp=a+(b&d|c&~d)+buffer[9]+0x21e1cde6;
      a=b+(temp<<5|temp>>27);
      temp=d+(a&c|b&~c)+buffer[14]+0xc33707d6;
      d=a+(temp<<9|temp>>23);
      temp=c+(d&b|a&~b)+buffer[3]+0xf4d50d87;
      c=d+(temp<<14|temp>>18);
      temp=b+(c&a|d&~a)+buffer[8]+0x455a14ed;
      b=c+(temp<<20|temp>>12);
      temp=a+(b&d|c&~d)+buffer[13]+0xa9e3e905;
      a=b+(temp<<5|temp>>27);
      temp=d+(a&c|b&~c)+buffer[2]+0xfcefa3f8;
      d=a+(temp<<9|temp>>23);
      temp=c+(d&b|a&~b)+buffer[7]+0x676f02d9;
      c=d+(temp<<14|temp>>18);
      temp=b+(c&a|d&~a)+buffer[12]+0x8d2a4c8a;
      b=c+(temp<<20|temp>>12);
      temp=a+(b^c^d)+buffer[5]+0xfffa3942;
      a=b+(temp<<4|temp>>28);
      temp=d+(a^b^c)+buffer[8]+0x8771f681;
      d=a+(temp<<11|temp>>21);
      temp=c+(d^a^b)+buffer[11]+0x6d9d6122;
      c=d+(temp<<16|temp>>16);
      temp=b+(c^d^a)+buffer[14]+0xfde5380c;
      b=c+(temp<<23|temp>>9);
      temp=a+(b^c^d)+buffer[1]+0xa4beea44;
      a=b+(temp<<4|temp>>28);
      temp=d+(a^b^c)+buffer[4]+0x4bdecfa9;
      d=a+(temp<<11|temp>>21);
      temp=c+(d^a^b)+buffer[7]+0xf6bb4b60;
      c=d+(temp<<16|temp>>16);
      temp=b+(c^d^a)+buffer[10]+0xbebfbc70;
      b=c+(temp<<23|temp>>9);
      temp=a+(b^c^d)+buffer[13]+0x289b7ec6;
      a=b+(temp<<4|temp>>28);
      temp=d+(a^b^c)+buffer[0]+0xeaa127fa;
      d=a+(temp<<11|temp>>21);
      temp=c+(d^a^b)+buffer[3]+0xd4ef3085;
      c=d+(temp<<16|temp>>16);
      temp=b+(c^d^a)+buffer[6]+0x04881d05;
      b=c+(temp<<23|temp>>9);
      temp=a+(b^c^d)+buffer[9]+0xd9d4d039;
      a=b+(temp<<4|temp>>28);
      temp=d+(a^b^c)+buffer[12]+0xe6db99e5;
      d=a+(temp<<11|temp>>21);
      temp=c+(d^a^b)+buffer[15]+0x1fa27cf8;
      c=d+(temp<<16|temp>>16);
      temp=b+(c^d^a)+buffer[2]+0xc4ac5665;
      b=c+(temp<<23|temp>>9);
      temp=a+(c^(b|~d))+buffer[0]+0xf4292244;
      a=b+(temp<<6|temp>>26);
      temp=d+(b^(a|~c))+buffer[7]+0x432aff97;
      d=a+(temp<<10|temp>>22);
      temp=c+(a^(d|~b))+buffer[14]+0xab9423a7;
      c=d+(temp<<15|temp>>17);
      temp=b+(d^(c|~a))+buffer[5]+0xfc93a039;
      b=c+(temp<<21|temp>>11);
      temp=a+(c^(b|~d))+buffer[12]+0x655b59c3;
      a=b+(temp<<6|temp>>26);
      temp=d+(b^(a|~c))+buffer[3]+0x8f0ccc92;
      d=a+(temp<<10|temp>>22);
      temp=c+(a^(d|~b))+buffer[10]+0xffeff47d;
      c=d+(temp<<15|temp>>17);
      temp=b+(d^(c|~a))+buffer[1]+0x85845dd1;
      b=c+(temp<<21|temp>>11);
      temp=a+(c^(b|~d))+buffer[8]+0x6fa87e4f;
      a=b+(temp<<6|temp>>26);
      temp=d+(b^(a|~c))+buffer[1
9724
5]+0xfe2ce6e0;
      d=a+(temp<<10|temp>>22);
      temp=c+(a^(d|~b))+buffer[6]+0xa3014314;
      c=d+(temp<<15|temp>>17);
      temp=b+(d^(c|~a))+buffer[13]+0x4e0811a1;
      b=c+(temp<<21|temp>>11);
      temp=a+(c^(b|~d))+buffer[4]+0xf7537e82;
      a=b+(temp<<6|temp>>26);
      temp=d+(b^(a|~c))+buffer[11]+0xbd3af235;
      d=a+(temp<<10|temp>>22);
      temp=c+(a^(d|~b))+buffer[2]+0x2ad7d2bb;
      c=d+(temp<<15|temp>>17);
      temp=b+(d^(c|~a))+buffer[9]+0xeb86d391;
      b=c+(temp<<21|temp>>11);
      A=A+a;B=B+b;C=C+c;D=D+d;
      a=A;b=B;c=C;d=D;

   }

   *((unsigned long *)add1)=A;

   *((unsigned long *)add1+1)=B;

   *((unsigned long *)add1+2)=C;

   *((unsigned long *)add1+3)=D;

  

   return 0;

}





 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  md5 算法 c语言 源码