32位md5加密算法c语言,MD5加密算法代码(C语言)

//MD5.h#include #include #include typedef unsigned int uint4;typedef unsigned short int uint2;typedef unsigned char uchar;uint4 m_State[4] ...

//MD5.h

#include

#include

#include

typedef unsigned       int uint4;

typedef unsigned short int uint2;

typedef unsigned      char uchar;

uint4    m_State[4];

uint4    m_Count[2];

uchar    m_Buffer[64];

uchar    m_Digest[16];

uchar    m_Finalized;

char* Prin真美妙5(uchar md5Digest[16]);

char* MD5String(char* szString);

char* MD5File(char* szFilename);

void Init();

void Update(uchar* chInput, uint4 nInputLen);

void Finalize();

uchar* Digest() { return m_Digest; }

void Transform(uchar* block);

void Encode(uchar* dest, uint4* src, uint4 nLength);

void Decode(uint4* dest, uchar* src, uint4 nLength);

inline uint4 rotate_left(uint4 x, uint4 n)

{ return ((x << n) | (x >> (32-n))); }

inline uint4 F(uint4 x, uint4 y, uint4 z)

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

inline uint4 G(uint4 x, uint4 y, uint4 z)

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

inline uint4 H(uint4 x, uint4 y, uint4 z)

{ return (x ^ y ^ z); }

inline uint4 I(uint4 x, uint4 y, uint4 z)

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

inline void FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)

{ a += F(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

inline void GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)

{ a += G(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

inline void HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)

{ a += H(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

inline void II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)

{ a += I(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

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

};

#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

char* Prin真美妙5(uchar md5Digest[16])

{

char chBuffer[256];

char chEach[10];

int nCount;

memset(chBuffer,0,256);

memset(chEach, 0, 10);

for (nCount = 0; nCount < 16; nCount++)

{

sprintf(chEach, "%02x", md5Digest[nCount]);

strncat(chBuffer, chEach, sizeof(chEach));

}

return strdup(chBuffer);

}

char* MD5String(char* szString)

{

Init();

int nLen = strlen(szString);

Update((unsigned char*)szString, (unsigned int)nLen);

Finalize();

return Prin真美妙5(Digest());

}

char* MD5File(char* szFilename)

{

FILE* file;

int nLen;

unsigned char chBuffer[1024];

try

{

memset(chBuffer, 0, 1024);

if ((file = fopen (szFilename, "rb")) != NULL)

{

while (nLen = fread (chBuffer, 1, 1024, file))

Update(chBuffer, nLen);

Finalize();

fclose (file);

return Prin真美妙5(Digest());

}

}

catch(...)

{

}

return NULL; // failed

}

void Init()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值