MD5 使用C 語言實現

2021-08-29 03:54:16 字數 3044 閱讀 5961

md5是一種訊息摘要演算法。

#include #include #include #include #include using namespace std;

typedef unsigned char uint8;

typedef unsigned short uint16;

typedef unsigned int uint32;

typedef unsigned long long uint64;

const uint32 max_msg_size = 10000;

void add(uint8 *msg);

uint32 a = 0x67452301;

uint32 b = 0xefcdab89;

uint32 c = 0x98badcfe;

uint32 d = 0x10325476;

uint32 loop_count = 0;

/*迴圈次數 以512位長度作為乙個分組*/

uint32 get_loop_count();

/* 迴圈左移count位 */

uint32 circle_left(uint32 number, uint32 count);

/* 4個非線性函式 */

uint32 f(uint32 x, uint32 y, uint32 z);

uint32 g(uint32 x, uint32 y, uint32 z);

uint32 h(uint32 x, uint32 y, uint32 z);

uint32 i(uint32 x, uint32 y, uint32 z);

void ff(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t);

void gg(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t);

void hh(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t);

void ii(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t);

uint64 msg_len;

void add(uint8 *msg, uint32 len)

// 在最後填充64位原始字串長度

uint8 c1 = (msg_len >> 56) & 0xff;

uint8 c2 = (msg_len >> 48) & 0xff;

uint8 c3 = (msg_len >> 40) & 0xff;

uint8 c4 = (msg_len >> 32) & 0xff;

uint8 c5 = (msg_len >> 24) & 0xff;

uint8 c6 = (msg_len >> 16) & 0xff;

uint8 c7 = (msg_len >> 8) & 0xff;

uint8 c8 = (msg_len & 0xff);

msg[msg_length] = c8; msg_length++;

msg[msg_length] = c7; msg_length++;

msg[msg_length] = c6; msg_length++;

msg[msg_length] = c5; msg_length++;

msg[msg_length] = c4; msg_length++;

msg[msg_length] = c3; msg_length++;

msg[msg_length] = c2; msg_length++;

msg[msg_length] = c1; msg_length++;

msg_len = msg_length;

}/* 迴圈次數 以512位的長度作為乙個分組*/

uint32 get_loop_count()

uint32 f(uint32 x, uint32 y, uint32 z)

uint32 g(uint32 x, uint32 y, uint32 z)

uint32 h(uint32 x, uint32 y, uint32 z)

uint32 i(uint32 x, uint32 y, uint32 z)

/* 迴圈左移 */

uint32 circle_left(uint32 number, uint32 count)

void ff(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t)

void gg(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t)

void hh(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t)

void ii(uint32 *a, uint32 *b, uint32 *c, uint32 *d, uint32 m, uint32 s, uint32 t)

void one_step(const uint8* const msg)

/* 列印成字串 **網路

void gethexstr(unsigned int num_str) ;

unsigned char *tmptr = (unsigned char *)&num_str;

int len = sizeof(num_str);

// 小端位元組序,逆序列印

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

cout << hexstr ;

}void solve(uint8 *msg, uint32 len)

int main()

md5的C 語言實現

include iostream include string include math.h using namespace std typedef char byte 初始化四個數 long a 0x67452301l long b 0xefcdab89l long c 0x98badcfel l...

MD5加密演算法C語言實現

include include include include include define block len 64 define max blocks 9 define message len block len max blocks 1 define result len 33 md5演算法的...

Hash(雜湊)演算法及MD5的C語言實現

什麼是雜湊演算法?雜湊演算法又叫雜湊演算法,是將任意長度的二進位制值對映為較短的固定長度的二進位制值,這個小的二進位制值稱為雜湊值。它的原理其實很簡單,就是把一段交易資訊轉換成乙個固定長度的字串。這串字串具有一些特點 1.資訊相同,字串也相同。2.資訊相似不會影響字串相同。3.可以生成無數的資訊,但...