21xrx.com
2024-11-22 09:59:24 Friday
登录
文章检索 我的文章 写文章
C++实现MD5数学加密
2023-07-01 01:20:12 深夜i     --     --
C++ MD5 数学加密

MD5是一种常见的加密算法,常被用于网络安全和数据传输中。在C++编程中,实现MD5加密需要使用第三方库或自己编写MD5算法的代码。

一种常用的第三方库是OpenSSL,它提供了MD5加密的函数实现。以下是使用OpenSSL库实现MD5加密的代码示例:


#include <openssl/md5.h>

#include <cstring>

#include <iostream>

using namespace std;

int main()

{

  string message = "hello world";

  unsigned char digest[MD5_DIGEST_LENGTH];

  MD5((unsigned char*)(message.c_str()), message.length(), digest);

  char mdString[33];

  for(int i = 0; i < MD5_DIGEST_LENGTH; i++)

    sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

  cout << "MD5 digest: " << mdString << endl;

  return 0;

}

以上代码将字符数组“hello world”加密成MD5摘要,并打印出来。

如果不想使用第三方库,也可以自己编写MD5算法的代码。以下是一个示例:


#include <cstring>

#include <iostream>

using namespace std;

unsigned int rotate_left(unsigned int x, int n)

{

  return (x << n) | (x >> (32-n));

}

void md5_transform(unsigned int state[], unsigned char block[])

{

  unsigned int a = state[0], b = state[1], c = state[2], d = state[3], x[16];

  for(int i = 0; i < 64; i++)

  {

    if(i < 16)

      x[i] = (unsigned int)block[i*4] + ((unsigned int)block[i*4+1] << 8)

        + ((unsigned int)block[i*4+2] << 16) + ((unsigned int)block[i*4+3] << 24);

    else

      x[i%16] = x[(i-1)%16] + rotate_left((x[(i-5)%16] ^ x[(i-10)%16] ^ x[(i-15)%16]), 1);

    unsigned int f, g;

    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;

    }

    unsigned int temp = d;

    d = c;

    c = b;

    b = b + rotate_left((a + f + 0x5a827999 + x[g]), 12);

    a = temp;

  }

  state[0] += a;

  state[1] += b;

  state[2] += c;

  state[3] += d;

}

void md5(unsigned char* message, unsigned int len, unsigned char* digest)

{

  unsigned int state[4] = 0x10325476;

  unsigned int bit_len = len * 8;

  unsigned int index = 0;

  unsigned char block[64];

  memset(block, 0, 64);

  for(int i = 0; i < len; i++)

  {

    block[index++] = message[i];

    if(index == 64)

    {

      md5_transform(state, block);

      index = 0;

    }

  }

  block[index++] = 0x80;

  if(index > 56)

  {

    while(index < 64)

      block[index++] = 0;

    md5_transform(state, block);

    memset(block, 0, 64);

  }

  while(index < 56)

    block[index++] = 0;

  block[56] = bit_len & 0xff;

  block[57] = (bit_len >> 8) & 0xff;

  block[58] = (bit_len >> 16) & 0xff;

  block[59] = (bit_len >> 24) & 0xff;

  md5_transform(state, block);

  for(int i = 0; i < 4; i++)

  {

    digest[i] = (state[0] >> (i*8)) & 0xff;

    digest[i+4] = (state[1] >> (i*8)) & 0xff;

    digest[i+8] = (state[2] >> (i*8)) & 0xff;

    digest[i+12] = (state[3] >> (i*8)) & 0xff;

  }

}

int main()

{

  string message = "hello world";

  unsigned char digest[16];

  md5((unsigned char*)message.c_str(), message.length(), digest);

  char mdString[33];

  for(int i = 0; i < 16; i++)

    sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

  cout << "MD5 digest: " << mdString << endl;

  return 0;

}

以上是用C++实现MD5加密的两种方法,可以根据实际情况选择使用OpenSSL库或自己编写代码实现。

  
  
下一篇: C++ 容器面试题

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章