當前位置:首頁 » 編程語言 » md5c語言加密演算法

md5c語言加密演算法

發布時間: 2024-10-28 21:16:45

1. 如何對字元串進行MD5加密,用C語言實現,給出源代碼和加密函數

UINT4 state[4]; /*存儲原始信息的bits數長度,不包括填充的bits,最長為2^64 bits*/ UINT4 count[2]; /*存放輸入的信息的緩沖區,512bits*/ unsigned char buffer[64];} MD5_CTX;static void MD5Transform(UINT4[4], unsigned char[64]); static void Encode(unsigned char *, UINT4 *, unsigned int); static void Decode(UINT4 *, unsigned char *, unsigned int);/*用於bits填充的緩沖區,當欲加密的信息的bits數被512除其餘數為448時,需要填充的bits的最大值為512=64*8*/ static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};/*接下來的這幾個宏定義是md5演算法規定的,就是對信息進行md5加密都要做的運算*/

2. 浠涔堟槸md5鍔犲瘑

浠庝簨緗戠粶瀹夊叏榪欎竴琛屼笟鐨勪漢閮界煡閬擄紝鎴戜滑鍋氭笚閫忔祴璇曟椂緇忓父浼氶亣鍒伴渶瑕佽В瀵嗙殑Md5瀵嗘枃銆備負浜嗚兘澶熸彁鍗囨晥鐜囷紝鎴戜滑鍙浠ュ湪緗戜笂鎵句竴浜涜兘澶熷湪綰縨d5瑙e瘑鐨勭綉絝欍備笅闈㈢粰澶у跺垎浜鍥藉唴甯哥敤鐨勫嚑涓鍏嶈垂MD5鍦ㄧ嚎瑙e瘑緗戠珯銆

涓銆丮D5鍦ㄧ嚎鍔犲瘑瑙e瘑

閽堝筸d5絳夊叏鐞冮氱敤鍏寮鐨勫姞瀵嗙畻娉曡繘琛屽弽鍚戞煡璇錛屽緩絝嬩簡瀵嗘枃瀵瑰簲鏌ヨ㈡暟鎹搴擄紝MD5鍔犲瘑瑙e瘑騫沖彴鍙鏌ヨ㈠緢澶氬嶆潅瀵嗘枃錛屾敮鎸佸氱嶇畻娉曪紝瀹炴椂鏌ヨ㈣板綍瓚呰繃24涓囦嚎鏉★紝鍏卞崰鐢160T紜鐩橈紝鎴愬姛鐜95%浠ヤ笂銆傝繖涓緗戠珯鍥介楅厤鍐呭緢澶氫漢鍦ㄤ嬌鐢錛屽洜涓哄畠鏄鍏嶈垂鐨勩傚傛灉鍦╩d5涓婅В瀵嗕笉浜嗙殑瀵嗘枃鍦ㄥ叾浠栬В瀹㈢綉絝欎篃鍩烘湰娌℃垙浜嗐

3. 求生成MD5碼的c或c++代碼

自己用C語言寫的簡單的MD5演算法實現。

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>

typedef unsigned char BYTE;
typedef unsigned int UINT;
typedef UINT MD5_SUB_ARRAY[16];
typedef UINT MD5_TRANSORM_FUNC(UINT,UINT,UINT);
typedef struct
{
UINT a;
UINT b;
UINT c;
UINT d;
MD5_SUB_ARRAY sub_array;
}MD5_TRANSFORM_PARAM;

const double MAX_INT = (double)0xFFFFFFFF + 1.0;

const UINT MD5_TRANSFORM_MATRIX[4][16][3] =
{

{
{ 0, 7, 1}, { 1,12, 2}, { 2,17, 3}, { 3,22, 4},
{ 4, 7, 5}, { 5,12, 6}, { 6,17, 7}, { 7,22, 8},
{ 8, 7, 9}, { 9,12,10}, {10,17,11}, {11,22,12},
{12, 7,13}, {13,12,14}, {14,17,15}, {15,22,16},
},

{
{ 1, 5,17}, { 6, 9,18}, {11,14,19}, { 0,20,20},
{ 5, 5,21}, {10, 9,22}, {15,14,23}, { 4,20,24},
{ 9, 5,25}, {14, 9,26}, { 3,14,27}, { 8,20,28},
{13, 5,29}, { 2, 9,30}, { 7,14,31}, {12,20,32},
},

{
{5, 4, 33}, { 8,11,34}, {11,16,35},{14, 23,36},
{1, 4, 37}, { 4,11,38}, { 7,16,39},{10, 23,40},
{13,4, 41}, { 0,11,42}, { 3,16,43},{ 6, 23,44},
{9, 4, 45}, {12,11,46}, {15,16,47},{ 2, 23,48},
},

{
{ 0,6,49}, { 7,10,50}, {14,15,51},{ 5, 21,52},
{12,6,53}, { 3,10,54}, {10,15,55},{ 1, 21,56},
{ 8,6,57}, {15,10,58}, { 6,15,59},{13, 21,60},
{ 4,6,61}, {11,10,62}, { 2,15,63},{ 9, 21,64},
},
};

static UINT MD5_TRANSFORM_ARRAY[65];

void MD5_Init()
{
int x;
for(x = 1; x <= 64; x++)
{
MD5_TRANSFORM_ARRAY[x] = (UINT)(MAX_INT * fabs(sin(x)));
}
}

UINT F(UINT x,UINT y,UINT z)
{
return ((x & y) | ((~x) & z));
}

UINT G(UINT x,UINT y,UINT z)
{
return ((x & z) | (y & (~z)));
}

UINT H(UINT x,UINT y,UINT z)
{
return (x ^ y ^ z);
}

UINT I(UINT x,UINT y,UINT z)
{
return (y ^ (x | (~z)));
}

BYTE* MD5_prepare_data(const BYTE* data,int len,int* new_len)
{
int rest,fill,size;
BYTE* new_data;
UINT bit_len;

// (1) 位元組補齊
rest = len % 56;
if (rest <= 56) fill = 56 - rest;
else fill = (64 - rest) + 56;

new_data = (BYTE*)malloc(len + fill + 8);
if (NULL == new_data) return NULL;

if (len > 0) memcpy(new_data,data,len);
if (fill > 0) memset(new_data + len,0x80,1);
if (fill > 1) memset(new_data + len + 1,0,fill - 1);

size = fill + len;

// (2) 附加數據的比特長度
bit_len = len * 8;
// (64位二進制數表示的)比特長度的低32位
memset(new_data + size + 0,(bit_len & 0x000000FF), 1);
memset(new_data + size + 1,(bit_len & 0x0000FF00) >> 8, 1);
memset(new_data + size + 2,(bit_len & 0x00FF0000) >> 16,1);
memset(new_data + size + 3,(bit_len & 0xFF000000) >> 24,1);
// 不考慮比特長度超出32位無符號數表示範圍,所以高32位總是0
memset(new_data + size + 4,0,4);

*new_len = size + 8;

return new_data;
}

void MD5_transform(MD5_TRANSFORM_PARAM* param,int ring,MD5_TRANSORM_FUNC func)
{
UINT a,b,c,d,s,k,i;
UINT abcd[4];
UINT *X,*T;
int index;

abcd[0] = param->a;
abcd[1] = param->b;
abcd[2] = param->c;
abcd[3] = param->d;
X = param->sub_array;
T = MD5_TRANSFORM_ARRAY;

for(index = 0; index < 16; index++)
{
a = abcd[(3 * index + 0) % 4];
b = abcd[(3 * index + 1) % 4];
c = abcd[(3 * index + 2) % 4];
d = abcd[(3 * index + 3) % 4];

k = MD5_TRANSFORM_MATRIX[ring][index][0];
s = MD5_TRANSFORM_MATRIX[ring][index][1];
i = MD5_TRANSFORM_MATRIX[ring][index][2];

a = a + func(b,c,d) + X[k] + T[i];
a = ( a << s) | ( a >> (32 - s)); // 循環左移
a = a + b;

abcd[(3 * index + 0) % 4] = a;
}

param->a = abcd[0];
param->b = abcd[1];
param->c = abcd[2];
param->d = abcd[3];
}

int MD5(const BYTE* data,int len)
{
int x,y,new_len;
MD5_TRANSFORM_PARAM param;
UINT AA,BB,CC,DD;
BYTE* buf;

MD5_Init();

buf = MD5_prepare_data(data,len,&new_len);
if (buf == NULL) return -1;

AA = 0x67452301;
BB = 0xefcdab89;
CC = 0x98badcfe;
DD = 0x10325476;

for(x = 0; x < new_len / 64; x++)
{
param.a = AA;
param.b = BB;
param.c = CC;
param.d = DD;

for(y = 0; y < 16; y++)
{
param.sub_array[y] = buf[64 * x + 4 * y + 0];
param.sub_array[y] += buf[64 * x + 4 * y + 1] << 8;
param.sub_array[y] += buf[64 * x + 4 * y + 2] << 16;
param.sub_array[y] += buf[64 * x + 4 * y + 3] << 24;
}
MD5_transform(¶m,0,F);
MD5_transform(¶m,1,G);
MD5_transform(¶m,2,H);
MD5_transform(¶m,3,I);

AA += param.a;
BB += param.b;
CC += param.c;
DD += param.d;
}

printf("MD5(\"%s\")=",data);

printf("%02X%02X%02X%02X",
(AA & 0x000000FF),
(AA & 0x0000FF00) >> 8,
(AA & 0x00FF0000) >> 16,
(AA & 0xFF000000) >> 24);

printf("%02X%02X%02X%02X",
(BB & 0x000000FF),
(BB & 0x0000FF00) >> 8,
(BB & 0x00FF0000) >> 16,
(BB & 0xFF000000) >> 24);

printf("%02X%02X%02X%02X",
(CC & 0x000000FF),
(CC & 0x0000FF00) >> 8,
(CC & 0x00FF0000) >> 16,
(CC & 0xFF000000) >> 24);

printf("%02X%02X%02X%02X",
(DD & 0x000000FF),
(DD & 0x0000FF00) >> 8,
(DD & 0x00FF0000) >> 16,
(DD & 0xFF000000) >> 24);

printf("\n");

return 0;
}

int main()
{
MD5("",0);
MD5("a",1);
MD5("abc",3);
MD5("message digest",14);
MD5("abcdefghijklmnopqrstuvwxyz",26);

return 0;
}

4. 如何在Linux下用C語言實現MD5/DES解密

相關庫函數如下:
#include <openssl/md5.h>
unsigned char *MD5(const unsigned char *d, unsigned long n,unsigned char *md);
int MD5_Init(MD5_CTX *c);
int MD5_Update(MD5_CTX *c, const void *data,unsigned long len);
int MD5_Final(unsigned char *md, MD5_CTX *c);

熱點內容
安卓系統的車機怎麼投屏 發布:2024-11-23 19:08:20 瀏覽:727
php404頁面 發布:2024-11-23 19:08:17 瀏覽:873
安卓如何設置手機快捷方式 發布:2024-11-23 18:30:29 瀏覽:146
安卓怎麼把系統帶的軟體刪了 發布:2024-11-23 18:16:13 瀏覽:319
linux服務程序 發布:2024-11-23 18:07:22 瀏覽:964
我的世界國際版伺服器低延遲推薦ip 發布:2024-11-23 18:02:35 瀏覽:351
文件存儲支持隨機存取 發布:2024-11-23 18:02:24 瀏覽:201
iosapp資料庫 發布:2024-11-23 18:01:36 瀏覽:480
分段函數編譯程序 發布:2024-11-23 17:59:20 瀏覽:508
中間演算法 發布:2024-11-23 17:43:12 瀏覽:815