您好,登錄后才能下訂單哦!
以下代碼是自己用C++語言對MD5算法的實現,編程工具是VC2013。雖然代碼的封裝性、安全性和效率遠遠比不上RFC文檔中的標準實現或者OpenSSL的源碼(這些代碼研究了很久依舊沒看懂,它們的效率雖高,但可讀性不高,不適合于我這種基礎比較薄弱的童鞋閱讀),但算法思路比較直觀,清晰易懂,方便讓基礎比較薄弱的童鞋閱讀。本人屬于C++初學者,編寫的代碼可能不夠簡潔,不夠美觀,很多地方也沒有考慮周到,希望大家見諒,也希望能有高手能指正批評。
注意:每處理64個字符就進行一次MD5操作,MD5算法本身不算太難,主要是輸入消息的預處理過程比較麻煩。
#pragma warning(disable : 4996)//VC2013增加了安全機制,如果不加這句會出現編譯錯誤 #include<iostream> #include<string> #include<stdio.h> #include<stdlib.h> #include <bitset> using namespace std; typedef unsigned int UNINT;//為unsigned int重命名,方便編程 #define S11 7 #define S12 12 #define S13 17 #define S14 22 #define S21 5 #define S22 9 #define S23 14 #define S24 20 #define S31 4 #define S32 11 #define S33 16 #define S34 23 #define S41 6 #define S42 10 #define S43 15 #define S44 21 UNINT hash_state[4]; UNINT word_32[16]; char block[64]; //初始化向量(A,B,C,D) void Init() { hash_state[0] = 0x67452301; hash_state[1] = 0xefcdab89; hash_state[2] = 0x98badcfe; hash_state[3] = 0x10325476; } //將長度為64的字符數組轉換成長度為16的整型數組,即將四個字符數組單元轉換為一個整型數組 int char64_to_word32(int n){ int i; for (i = 0; i < n; i++){ word_32[i] = (block[4 * i + 0] << 24) ^ (block[4 * i + 1] << 16) ^ (block[4 * i + 2] << 8) ^ block[4 * i + 3]; } return i; } //新建一個分組存放消息長度 void newblock_length(UNINT l){ for (int i = 0; i < 15; i++){ word_32[i] = 0; } //將消息長度存放在最后4個字節 word_32[15] = l; } UNINT rotate_left(UNINT x, UNINT n) { return ((x << n) | (x >> (32 - n))); } UNINT F(UNINT x, UNINT y, UNINT z) { return ((x & y) | (~x & z)); } UNINT G(UNINT x, UNINT y, UNINT z) { return ((x & z) | (y & ~z)); } UNINT H(UNINT x, UNINT y, UNINT z) { return (x ^ y ^ z); } UNINT I(UNINT x, UNINT y, UNINT z) { return (y ^ (x | ~z)); } void FF(UNINT* a, UNINT b, UNINT c, UNINT d, UNINT x, UNINT s, UNINT ac) { *a += F(b, c, d) + x + ac; *a = rotate_left(*a, s); *a += b; } void GG(UNINT* a, UNINT b, UNINT c, UNINT d, UNINT x, UNINT s, UNINT ac) { *a += G(b, c, d) + x + ac; *a = rotate_left(*a, s); *a += b; } void HH(UNINT* a, UNINT b, UNINT c, UNINT d, UNINT x, UNINT s, UNINT ac) { *a += H(b, c, d) + x + ac; *a = rotate_left(*a, s); *a += b; } void II(UNINT* a, UNINT b, UNINT c, UNINT d, UNINT x, UNINT s, UNINT ac) { *a += I(b, c, d) + x + ac; *a = rotate_left(*a, s); *a += b; } void MD5_Transform(UNINT* x) { UNINT a, b, c, d; a = hash_state[0]; b = hash_state[1]; c = hash_state[2]; d = hash_state[3]; // Round 1 FF(&a, b, c, d, x[0], S11, 0xd76aa478); FF(&d, a, b, c, x[1], S12, 0xe8c7b756); FF(&c, d, a, b, x[2], S13, 0x242070db); FF(&b, c, d, a, x[3], S14, 0xc1bdceee); FF(&a, b, c, d, x[4], S11, 0xf57c0faf); FF(&d, a, b, c, x[5], S12, 0x4787c62a); FF(&c, d, a, b, x[6], S13, 0xa8304613); FF(&b, c, d, a, x[7], S14, 0xfd469501); FF(&a, b, c, d, x[8], S11, 0x698098d8); FF(&d, a, b, c, x[9], S12, 0x8b44f7af); FF(&c, d, a, b, x[10], S13, 0xffff5bb1); FF(&b, c, d, a, x[11], S14, 0x895cd7be); FF(&a, b, c, d, x[12], S11, 0x6b901122); FF(&d, a, b, c, x[13], S12, 0xfd987193); FF(&c, d, a, b, x[14], S13, 0xa679438e); FF(&b, c, d, a, x[15], S14, 0x49b40821); // Round 2 GG(&a, b, c, d, x[1], S21, 0xf61e2562); GG(&d, a, b, c, x[6], S22, 0xc040b340); GG(&c, d, a, b, x[11], S23, 0x265e5a51); GG(&b, c, d, a, x[0], S24, 0xe9b6c7aa); GG(&a, b, c, d, x[5], S21, 0xd62f105d); GG(&d, a, b, c, x[10], S22, 0x2441453); GG(&c, d, a, b, x[15], S23, 0xd8a1e681); GG(&b, c, d, a, x[4], S24, 0xe7d3fbc8); GG(&a, b, c, d, x[9], S21, 0x21e1cde6); GG(&d, a, b, c, x[14], S22, 0xc33707d6); GG(&c, d, a, b, x[3], S23, 0xf4d50d87); GG(&b, c, d, a, x[8], S24, 0x455a14ed); GG(&a, b, c, d, x[13], S21, 0xa9e3e905); GG(&d, a, b, c, x[2], S22, 0xfcefa3f8); GG(&c, d, a, b, x[7], S23, 0x676f02d9); GG(&b, c, d, a, x[12], S24, 0x8d2a4c8a); // Round 3 HH(&a, b, c, d, x[5], S31, 0xfffa3942); HH(&d, a, b, c, x[8], S32, 0x8771f681); HH(&c, d, a, b, x[11], S33, 0x6d9d6122); HH(&b, c, d, a, x[14], S34, 0xfde5380c); HH(&a, b, c, d, x[1], S31, 0xa4beea44); HH(&d, a, b, c, x[4], S32, 0x4bdecfa9); HH(&c, d, a, b, x[7], S33, 0xf6bb4b60); HH(&b, c, d, a, x[10], S34, 0xbebfbc70); HH(&a, b, c, d, x[13], S31, 0x289b7ec6); HH(&d, a, b, c, x[0], S32, 0xeaa127fa); HH(&c, d, a, b, x[3], S33, 0xd4ef3085); HH(&b, c, d, a, x[6], S34, 0x4881d05); HH(&a, b, c, d, x[9], S31, 0xd9d4d039); HH(&d, a, b, c, x[12], S32, 0xe6db99e5); HH(&c, d, a, b, x[15], S33, 0x1fa27cf8); HH(&b, c, d, a, x[2], S34, 0xc4ac5665); // Round 4 II(&a, b, c, d, x[0], S41, 0xf4292244); II(&d, a, b, c, x[7], S42, 0x432aff97); II(&c, d, a, b, x[14], S43, 0xab9423a7); II(&b, c, d, a, x[5], S44, 0xfc93a039); II(&a, b, c, d, x[12], S41, 0x655b59c3); II(&d, a, b, c, x[3], S42, 0x8f0ccc92); II(&c, d, a, b, x[10], S43, 0xffeff47d); II(&b, c, d, a, x[1], S44, 0x85845dd1); II(&a, b, c, d, x[8], S41, 0x6fa87e4f); II(&d, a, b, c, x[15], S42, 0xfe2ce6e0); II(&c, d, a, b, x[6], S43, 0xa3014314); II(&b, c, d, a, x[13], S44, 0x4e0811a1); II(&a, b, c, d, x[4], S41, 0xf7537e82); II(&d, a, b, c, x[11], S42, 0xbd3af235); II(&c, d, a, b, x[2], S43, 0x2ad7d2bb); II(&b, c, d, a, x[9], S44, 0xeb86d391); hash_state[0] += a; hash_state[1] += b; hash_state[2] += c; hash_state[3] += d; } int main() { UNINT word_pos, block_pos, counter, counter_update; string str ; Init();//初始化向量(A,B,C,D) cin >> str; counter = str.length();//計算輸入消息的長度 counter_update = counter; block_pos = 0; //對輸入消息進行分組,每組64bytes while (counter_update / 64>0){ str.copy(block, 64, 64 * block_pos);//將起始位置為64*j的后續64個字符拷貝到block數組 char64_to_word32(16);//將64個字符轉換成16個字 //MD5 MD5_Transform(word_32); block_pos++; counter_update -= 64; } if (counter_update % 64 == 0){ newblock_length(counter); //MD5 MD5_Transform(word_32); } else{ //對剩余不足64bytes的分組進行處理 str.copy(block, counter_update, 64 * block_pos); //如果最后一個分組剩余不足4bytes空間來存放原始消息長度,則用0將該分組填充滿,然后再新建一個分組存放消息長度 if (counter_update > 60){ //將block數組前面4k個字符轉換成k個整型數 word_pos = char64_to_word32(counter_update / 4); //更新block最后剩余的字符數 counter_update -= 4 * word_pos; //根據剩余字符數模4的不同結果進行不同的處理 switch (counter_update){ case 0:{ break; } //如果最后剩余一個字符,則將其左移到最高8位,后面24位補0 case 1:{ word_32[word_pos] = (block[4 * word_pos] << 24) ^ 0x000000; word_pos++; break; } //如果最后剩余兩個字符,則將其左移到最高16位,后面16位補0 case 2:{ word_32[word_pos] = (block[4 * word_pos] << 24) ^ (block[4 * word_pos + 1] << 16) ^ 0x0000; word_pos++; break; } //如果最后剩余三個字符,則將其左移到最高24位,后面8位補0 case 3:{ word_32[word_pos] = (block[4 * word_pos] << 24) ^ (block[4 * word_pos + 1] << 16) ^ (block[4 * word_pos + 2] << 8) ^ 0x00; word_pos++; break; } } //對word_36數組的剩余單元填充 while (word_pos < 16){ word_32[word_pos] = 0; word_pos++; } //MD5 MD5_Transform(word_32); //新建一個數組存放消息長度 newblock_length(counter); //MD5 MD5_Transform(word_32); } //如果最后一個分組剩余超過4bytes的空間,則用最后4bytes存放消息長度,其他位置用0填充 else{ word_pos = char64_to_word32(counter_update / 4); counter_update -= 4 * word_pos; switch (counter_update){ case 0:{ break; } case 1:{ word_32[word_pos] = (block[4 * word_pos] << 24) ^ 0x000000; word_pos++; break; } case 2:{ word_32[word_pos] = (block[4 * word_pos] << 24) ^ (block[4 * word_pos + 1] << 16) ^ 0x0000; word_pos++; break; } case 3:{ word_32[word_pos] = (block[4 * word_pos] << 24) ^ (block[4 * word_pos + 1] << 16) ^ (block[4 * word_pos + 2] << 8) ^ 0x00; word_pos++; break; } } while (word_pos < 15){ word_32[word_pos] = 0; word_pos++; } word_32[15] = counter; //MD5 MD5_Transform(word_32); } } cout << hex << hash_state[0] << endl; cout << hex << hash_state[1] << endl; cout << hex << hash_state[2] << endl; cout << hex << hash_state[3] << endl; system("pause"); return 0; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。