MD5算法簡介:
消息摘要算法第五版(英語:Message-Digest Algorithm 5,縮寫爲MD5),是當前計算機領域用於確保信息傳輸完整一致而廣泛使用的散列算法之一(又譯哈希算法、摘要算法等),主流編程語言普遍已有MD5的實現。將數據 (如一段文字)運算變爲另一固定長度值,是散列算法的基礎原理,MD5的前身有MD2、MD3和MD4。MD5由MD4、MD3、MD2改進而來,主要增強算法複雜度和不可逆性。目前,MD5算法因其普遍、穩定、快速的特點,仍廣泛應用於普通 數據的錯誤檢查領域。例如在一些BitTorrent下載中,軟件將通過計算MD5檢驗下載到的文件片段的完整性。MD5已經廣泛使用在爲文件傳輸提供一定的可靠性方面。例如,服務器預先提供一個MD5校驗和,用戶下載完文件以後, 用MD5算法計算下載文件的MD5校驗和,然後通過檢查這兩個校驗和是否一致,就能判斷下載的文件是否出錯。MD5是輸入不定長度信息,輸出固定長度128-bits的算法。經過程序流程,生成四個32位數據,最後聯合起來成爲一個 128-bits散列。基本方式爲,求餘、取餘、調整長度、與鏈接變量進行循環運算。得出結果。
MD5運算步驟:
假設輸入信息(input message)的長度爲b(bit),我們想要產生它的報文摘要,在此處b爲任意的非負整數:b也可能爲0,也不一定爲8的整數倍,且可能是任意大的長度。設該信息的比特流表示如下: M[0] M[1] M[2] … M[b-1] 計算此信息的報文摘要需要如下5步:
1.補位
信息計算前先要進行位補位,設補位後信息的長度爲LEN(bit),則LEN%512 = 448(bit),即數據擴展至 K * 512 + 448(bit)。即K * 64+56(byte),K爲整數。補位操作始終要執行,即使補位前信息的長度對512求餘的結果是448。具體補位操作:補一個1,然後補0至滿足上述要求。總共最少要補1bit,最多補512bit。
2.尾部加上信息長度
將輸入信息的原始長度b(bit)表示成一個64-bit的數字,把它添加到上一步的結果後面(在32位的機器上,這64位將用2個字來表示並且低位在前)。當遇到b大於2^64這種極少的情況時,b的高位被截去,僅使用b的低64位。經過上面兩步,數據就被填補成長度爲512(bit)的倍數。也就是說,此時的數據長度是16個字(32byte)的整數倍。此時的數據表示爲: M[0 … N-1] 其中的N是16的倍數。
3.初始化緩存區
用一個四個字的緩衝器(A,B,C,D)來計算報文摘要,A,B,C,D分別是32位的寄存器,初始化使用的是十六進制表示的數字,注意低字節在前: word A: 01 23 45 67 word B: 89 ab cd ef word C: fe dc ba 98 word D: 76 54 32 10
A = 0x67452301;
B = 0xEFCDAB89;
C = 0x98BADCFE;
D = 0x10325476;
4.轉換
四輪循環運算:循環的次數是分組的個數(N+1):
1)將每一512字節細分成16個小組,每個小組64位(8個字節)
2)用到的作爲輔助的四個線性函數
unsigned int F(unsigned int x, unsigned int y, unsigned int z) {
return (x & y) | ((~x) & z);
}
unsigned int G(unsigned int x, unsigned int y, unsigned int z) {
return (x & z) | (y & (~z));
}
unsigned int H(unsigned int x, unsigned int y, unsigned int z) {
return x ^ y ^ z;
}
unsigned int I(unsigned int x, unsigned int y, unsigned int z) {
return y ^ (x | (~z));
}
3)x表示消息的某個子分組(從0到15),<<
unsigned int FF(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, int s, unsigned int ac) {
a += F(b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
return a;
}
unsigned int GG(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, int s, unsigned int ac) {
a += G(b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
return a;
}
unsigned int HH(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, int s, unsigned int ac) {
a += H(b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
return a;
}
unsigned int II(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, int s, unsigned int ac) {
a += I(b, c, d) + x + ac;
a = (a << s) | (a >> (32 - s));
a += b;
return a;
}
4)四輪運算
void MD5::transform(const unsigned char *block) {
unsigned int a = A, b = B, c = C, d = D;
unsigned int groups[16];
decode(groups, block, 64);
//groups[15] = 0;
a = FF(a, b, c, d, groups[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, groups[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, groups[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, groups[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, groups[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, groups[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, groups[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, groups[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, groups[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, groups[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, groups[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, groups[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, groups[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, groups[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, groups[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, groups[15], S14, 0x49b40821L); /* 16 */
/*第二輪*/
a = GG(a, b, c, d, groups[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, groups[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, groups[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, groups[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, groups[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, groups[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, groups[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, groups[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, groups[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, groups[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, groups[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, groups[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, groups[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, groups[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, groups[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, groups[12], S24, 0x8d2a4c8aL); /* 32 */
/*第三輪*/
a = HH(a, b, c, d, groups[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, groups[8], S32, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, groups[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, groups[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, groups[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, groups[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, groups[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, groups[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, groups[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, groups[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, groups[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, groups[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, groups[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, groups[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, groups[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, groups[2], S34, 0xc4ac5665L); /* 48 */
/*第四輪*/
a = II(a, b, c, d, groups[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, groups[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, groups[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, groups[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, groups[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, groups[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, groups[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, groups[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, groups[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, groups[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, groups[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, groups[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, groups[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, groups[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, groups[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, groups[9], S44, 0xeb86d391L); /* 64 */
A += a;
B += b;
C += c;
D += d;
}
函數中兩個比較重要的模塊,其中一個是update函數,也是大部分摘要算法都具有的操作:
作用主要是更新MD5塊。然後繼續MD5消息摘要操作,處理另一個消息塊。
void MD5::update(const unsigned char *input, int length) {
inputNum += (length << 3);
int start = 64 - bufferNum;
////將輸入的一部分複製到buffer所以它可以形成一個塊(size = 64)
if(start <= length)
{
//可以形成一個塊,然後對這個塊做四輪循環
memcpy(&buffer[bufferNum], input, start);
transform(buffer);
int i;
for(i = start; i <= length - 64; i += 64)
{
transform(&input[i]);
}
bufferNum = length - i;
memcpy(buffer, &input[i], bufferNum);
}
else
{
//不能形成塊
memcpy(&buffer[bufferNum], input, length);
bufferNum += length;
}
}
然後再是Final函數,作用是使用100000填充buffer, 並添加64位原始大小的輸入
void MD5::Final()
{
int temp = 56 - bufferNum; //56 = 448/8
//填充
if(temp > 0)
{
update(padding, temp);
inputNum -= (temp << 3);
}
else if(temp < 0)
{
update(padding, 64 + temp);
inputNum -= ((64 + temp) << 3);
}
//trans inputNum(輸入的位數) to unsigned char (64bits)
unsigned char Bits[8];
//cout << inputNum << endl;
for(int i = 0; i < 8; i++)
{
//cout << inputNum << endl;
Bits[i] = (inputNum >> 8*i) & 0xff;
//cout << i << endl;
}
//cout << (int)Bits[4] << endl;
//添加最初輸入的num(後64位)
update(Bits, 8);
unsigned int input[4];
input[0] = A;
input[1] = B;
input[2] = C;
input[3] = D;
//將input存入result
encode(result, input, 16);
}
最後將整個模塊整合爲類:
class MD5 {
private:
string cipher;
unsigned int A;
unsigned int B;
unsigned int C;
unsigned int D;
unsigned char buffer[64];
int bufferNum; //
unsigned long long inputNum;
unsigned char result[16];
public:
MD5();
~MD5();
string encrypt(const char* fiename);
private:
void readCipher(const char *filename);
void update(const unsigned char *input, int length);
void encode(unsigned char *output, unsigned int *input, int length);
void decode(unsigned int *output, const unsigned char* input, const int length);
void transform(const unsigned char *block);
void Final();
};
運行結果:
將要加密的文本寫入到密碼本中:
運行結果:
然後與加密網站的結果進行對比:
網站爲:http://tool.chinaz.com/tools/md5.aspx
結果一致,說明加密成功。