MD5算法C语言实现

 使用说明:
  将斜杠下面的源码复制到自己的源码中,在主函数之前加上函数声明,
  之后便可在主函数中调用调用该函数了。
  参数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[15]+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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用C语言实现MD5加密算法的示例: ```c #include <stdio.h> #include <string.h> #include <stdint.h> #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) void md5(uint8_t *initial_msg, size_t initial_len) { uint32_t h0, h1, h2, h3; uint8_t *msg = NULL; uint32_t *w = NULL; uint32_t a, b, c, d, i, f, g, temp; size_t new_len, offset; // 初始化变量 h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476; // 对消息进行预处理 new_len = ((((initial_len + 8) / 64) + 1) * 64); msg = (uint8_t *)malloc(new_len); memcpy(msg, initial_msg, initial_len); msg[initial_len] = 128; offset = initial_len + 1; while (offset < new_len - 8) { msg[offset] = 0; offset++; } uint64_t bits_len = (uint64_t)initial_len * 8; memcpy(msg + new_len - 8, &bits_len, 8); // 处理消息的每个块 for (offset = 0; offset < new_len; offset += 64) { w = (uint32_t *)(msg + offset); // 初始化散列值 a = h0; b = h1; c = h2; d = h3; // 主循环 for (i = 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; } temp = d; d = c; c = b; b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]); a = temp; } // 更新散列值 h0 += a; h1 += b; h2 += c; h3 += d; } // 释放内存 free(msg); // 输出散列值 printf("MD5 Hash: %08x%08x%08x%08x\n", h0, h1, h2, h3); } int main() { char *msg = "Hello, world!"; size_t len = strlen(msg); md5((uint8_t *)msg, len); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值