MFC下MD5加密演算法實現

2022-07-17 10:21:11 字數 4194 閱讀 4054

以下就做一下簡單的介紹和使用:

1、首先是.h的標頭檔案。

#ifndef md5_h

#define md5_h

#include #include /* type define */

typedef unsigned char byte;

typedef unsigned int uint32;

using std::string;

using std::ifstream;

/* md5 declaration. */

class md5 ;

};#endif /*md5_h*/

2、接下來是.cpp的內容:

#include "stdafx.h"

#include "md5.h"

using namespace std;

/* constants for md5transform routine. */

#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

/* f, g, h and i are basic md5 functions.

*/#define f(x, y, z) (((x) & (y)) | ((~x) & (z)))

#define g(x, y, z) (((x) & (z)) | ((y) & (~z)))

#define h(x, y, z) ((x) ^ (y) ^ (z))

#define i(x, y, z) ((y) ^ ((x) | (~z)))

/* rotate_left rotates x left n bits.

*/#define rotate_left(x, n) (((x) << (n)) | ((x) >> (32-(n))))

/* ff, gg, hh, and ii transformations for rounds 1, 2, 3, and 4.

rotation is separate from addition to prevent recomputation.

*/#define ff(a, b, c, d, x, s, ac)

#define gg(a, b, c, d, x, s, ac)

#define hh(a, b, c, d, x, s, ac)

#define ii(a, b, c, d, x, s, ac)

const byte md5::padding[64] = ;

const char md5::hex[16] = ;

/* default construct. */

md5::md5()

/* construct a md5 object with a input buffer. */

md5::md5(const void* input, size_t length)

/* construct a md5 object with a string. */

md5::md5(const string& str)

/* construct a md5 object with a file. */

md5::md5(ifstream& in)

/* return the message-digest */

const byte* md5::digest()

return _digest;

}/* reset the calculate state */

void md5::reset()

/* updating the context with a input buffer. */

void md5::update(const void* input, size_t length)

/* updating the context with a string. */

void md5::update(const string& str)

/* updating the context with a file. */

void md5::update(ifstream& in)

std::streamsize length;

char buffer[buffer_size];

while (!in.eof())

} in.close();

}/* md5 block update operation. continues an md5 message-digest

operation, processing another message block, and updating the

context.

*/void md5::update(const byte* input, size_t length)

_count[1] += ((uint32)length >> 29);

partlen = 64 - index;

/* transform as many times as possible. */

if (length >= partlen)

index = 0;

} else

/* buffer remaining input */

memcpy(&_buffer[index], &input[i], length - i);

}/* md5 finalization. ends an md5 message-_digest operation, writing the

the message _digest and zeroizing the context.

*/void md5::final()

/* md5 basic transformation. transforms _state based on block. */

void md5::transform(const byte block[64])

/* encodes input (ulong) into output (byte). assumes length is

a multiple of 4.

*/void md5::encode(const uint32* input, byte* output, size_t length)

}/* decodes input (byte) into output (ulong). assumes length is

a multiple of 4.

*/void md5::decode(const byte* input, uint32* output, size_t length)

}/* convert byte array to hex string. */

string md5::bytestohexstring(const byte* input, size_t length)

return str;

}/* convert digest to string value */

string md5::tostring()

3、檔名為:md5.h 和  md5.cpp

4、使用時直接include的引用:#include "md5.h"

5、使用方法

md5 md5;                 //定義md5的類

cstring sql="123456";           //需要加密的字串

md5.update(sql.getbuffer());          //因為update函式只接收string型別,所以使用getbuffer()函式轉換cstring為string

sqlvalue=md5.tostring().c_str();     //tostring()函式獲得加密字串,c_str();函式重新轉換成cstring型別

md5加密演算法

md5.h ifndef md5h define md5h include include void rol unsigned int s,unsigned short cx 32位數迴圈左移實現函式 void ltob unsigned int i b l互轉,接受uint型別 unsigned ...

MD5加密演算法

md5訊息摘要演算法 message digest algorithm 它對輸入的任意長度的訊息進行運算,產生乙個128位的訊息摘要。演算法原理 資料填充 填充訊息使其長度與448模512同餘 長度 448 mod 512 即時訊息長度本身已經滿足了上述長度要求也需要填充。填充方法 附乙個1在訊息後...

加密演算法 MD5

一 簡介 md5的全稱是message digest algorithm 5 資訊摘要演算法 在90年代初由mit laboratory for computer science和rsa data security inc的ronald l.rivest開發出來,經md2 md3和md4發展而來。訊...