md5加解密代码c语言实现
时间: 2023-09-22 07:03:15 浏览: 189
MD5(Message Digest Algorithm 5)是一种常用的哈希算法,用于生成数据的摘要。下面是使用C语言实现MD5加解密的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef uint8_t BYTE;
typedef uint32_t WORD;
#define BUFFER_SIZE 64
#define CHUNK_SIZE 512
// 填充原始消息
void padding(BYTE *message, int length) {
int remainder = length % BUFFER_SIZE;
int padding_size = (remainder < (BUFFER_SIZE - 8)) ? (BUFFER_SIZE - 8 - remainder) : (2 * BUFFER_SIZE - 8 - remainder);
BYTE padding[padding_size];
padding[0] = 0x80;
for (int i = 1; i < padding_size; i++) {
padding[i] = 0;
}
WORD message_length_bits = length * 8;
memcpy(message + length, padding, padding_size);
memcpy(message + length + padding_size, &message_length_bits, sizeof(WORD));
}
// 左循环移位
WORD rotate_left(WORD x, int n) {
return (x << n) | (x >> (32 - n));
}
// MD5压缩函数
void md5_compress(BYTE *chunk, WORD *state) {
WORD a = state[0], b = state[1], c = state[2], d = state[3];
WORD x[16];
for (int i = 0; i < 16; i++) {
x[i] = *(WORD *)(chunk + i * 4);
}
// Round 1
WORD round1[] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821};
for (int i = 0; i < 16; i++) {
WORD f = (b & c) | ((~b) & d);
WORD g = i;
WORD temp = d;
d = c;
c = b;
b = b + rotate_left((a + f + round1[i] + x[g]), 7);
a = temp;
}
// Round 2
WORD round2[] = {0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a};
for (int i = 16; i < 32; i++) {
WORD f = (d & b) | ((~d) & c);
WORD g = (5 * i + 1) % 16;
WORD temp = d;
d = c;
c = b;
b = b + rotate_left((a + f + round2[i - 16] + x[g]), 12);
a = temp;
}
// Round 3
WORD round3[] = {0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665};
for (int i = 32; i < 48; i++) {
WORD f = b ^ c ^ d;
WORD g = (3 * i + 5) % 16;
WORD temp = d;
d = c;
c = b;
b = b + rotate_left((a + f + round3[i - 32] + x[g]), 17);
a = temp;
}
// Round 4
WORD round4[] = {0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
for (int i = 48; i < 64; i++) {
WORD f = c ^ (b | (~d));
WORD g = (7 * i) % 16;
WORD temp = d;
d = c;
c = b;
b = b + rotate_left((a + f + round4[i - 48] + x[g]), 22);
a = temp;
}
// 更新状态
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
// 计算MD5
void md5_hash(BYTE *message, int length, BYTE *hash) {
WORD state[] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476};
int num_chunks = (length + BUFFER_SIZE) / CHUNK_SIZE;
// 遍历消息的所有块进行压缩
for (int i = 0; i < num_chunks; i++) {
BYTE chunk[CHUNK_SIZE] = {0};
if (i != num_chunks - 1) {
// 非末尾块
memcpy(chunk, message + i * CHUNK_SIZE, CHUNK_SIZE);
} else {
// 末尾块
int chunk_size = length % CHUNK_SIZE;
memcpy(chunk, message + i * CHUNK_SIZE, chunk_size);
padding(chunk, chunk_size);
}
md5_compress(chunk, state);
}
// 生成摘要
memcpy(hash, state, 16);
}
int main() {
char input[] = "Hello, World!";
BYTE hash[16];
md5_hash((BYTE *)input, strlen(input), hash);
printf("MD5 hash: ");
for (int i = 0; i < 16; i++) {
printf("%02x", hash[i]);
}
printf("\n");
return 0;
}
```
以上是一个简单的用C语言实现的MD5加解密代码示例。它包括填充原始消息、左循环移位、MD5压缩函数、计算MD5等功能。示例代码通过将字符串"Hello, World!"计算出其对应的MD5摘要,并将其打印出来。您可以根据自己的需求修改该代码以适应其他情况。
阅读全文