導航:首頁 > 源碼編譯 > md5演算法c實現

md5演算法c實現

發布時間:2022-09-22 07:16:35

❶ 跪求大蝦幫忙實現下MD5的演算法(用C語言),剛找到工作,忘大家幫個忙,謝謝啊!

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "../md5/md5.h"

#define T1 0xd76aa478
#define T2 0xe8c7b756
#define T3 0x242070db
#define T4 0xc1bdceee
#define T5 0xf57c0faf
#define T6 0x4787c62a
#define T7 0xa8304613
#define T8 0xfd469501
#define T9 0x698098d8
#define T10 0x8b44f7af
#define T11 0xffff5bb1
#define T12 0x895cd7be
#define T13 0x6b901122
#define T14 0xfd987193
#define T15 0xa679438e
#define T16 0x49b40821
#define T17 0xf61e2562
#define T18 0xc040b340
#define T19 0x265e5a51
#define T20 0xe9b6c7aa
#define T21 0xd62f105d
#define T22 0x02441453
#define T23 0xd8a1e681
#define T24 0xe7d3fbc8
#define T25 0x21e1cde6
#define T26 0xc33707d6
#define T27 0xf4d50d87
#define T28 0x455a14ed
#define T29 0xa9e3e905
#define T30 0xfcefa3f8
#define T31 0x676f02d9
#define T32 0x8d2a4c8a
#define T33 0xfffa3942
#define T34 0x8771f681
#define T35 0x6d9d6122
#define T36 0xfde5380c
#define T37 0xa4beea44
#define T38 0x4bdecfa9
#define T39 0xf6bb4b60
#define T40 0xbebfbc70
#define T41 0x289b7ec6
#define T42 0xeaa127fa
#define T43 0xd4ef3085
#define T44 0x04881d05
#define T45 0xd9d4d039
#define T46 0xe6db99e5
#define T47 0x1fa27cf8
#define T48 0xc4ac5665
#define T49 0xf4292244
#define T50 0x432aff97
#define T51 0xab9423a7
#define T52 0xfc93a039
#define T53 0x655b59c3

#define T54 0x8f0ccc92
#define T55 0xffeff47d
#define T56 0x85845dd1
#define T57 0x6fa87e4f
#define T58 0xfe2ce6e0
#define T59 0xa3014314
#define T60 0x4e0811a1
#define T61 0xf7537e82
#define T62 0xbd3af235
#define T63 0x2ad7d2bb
#define T64 0xeb86d391

static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
{
md5_word_t
a = pms->abcd[0], b = pms->abcd[1],
c = pms->abcd[2], d = pms->abcd[3];
md5_word_t t;

#ifndef ARCH_IS_BIG_ENDIAN
# define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */
#endif
#if ARCH_IS_BIG_ENDIAN

/*
* On big-endian machines, we must arrange the bytes in the right
* order. (This also works on machines of unknown byte order.)
*/
md5_word_t X[16];
const md5_byte_t *xp = data;
int i;

for (i = 0; i < 16; i, xp = 4)
X[i] = xp[0] (xp[1] << 8) (xp[2] << 16) (xp[3] << 24);

#else /* !ARCH_IS_BIG_ENDIAN */

/*
* On little-endian machines, we can process properly aligned data
* without ing it.
*/
md5_word_t xbuf[16];
const md5_word_t *X;

if (!((data - (const md5_byte_t *)0) & 3)) {
/* data are properly aligned */
X = (const md5_word_t *)data;
} else {
/* not aligned */
memcpy(xbuf, data, 64);
X = xbuf;
}
#endif

#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))

/* Round 1. */
/* Let [abcd k s i] denote the operation
a = b ((a F(b,c,d) X[k] T[i]) <<< s). */
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define SET(a, b, c, d, k, s, Ti)\
t = a F(b,c,d) X[k] Ti;\
a = ROTATE_LEFT(t, s) b
/* Do the following 16 operations. */
SET(a, b, c, d, 0, 7, T1);
SET(d, a, b, c, 1, 12, T2);
SET(c, d, a, b, 2, 17, T3);
SET(b, c, d, a, 3, 22, T4);
SET(a, b, c, d, 4, 7, T5);
SET(d, a, b, c, 5, 12, T6);
SET(c, d, a, b, 6, 17, T7);

SET(b, c, d, a, 7, 22, T8);
SET(a, b, c, d, 8, 7, T9);
SET(d, a, b, c, 9, 12, T10);
SET(c, d, a, b, 10, 17, T11);
SET(b, c, d, a, 11, 22, T12);
SET(a, b, c, d, 12, 7, T13);
SET(d, a, b, c, 13, 12, T14);
SET(c, d, a, b, 14, 17, T15);
SET(b, c, d, a, 15, 22, T16);
#undef SET

/* Round 2. */
/* Let [abcd k s i] denote the operation
a = b ((a G(b,c,d) X[k] T[i]) <<< s). */
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define SET(a, b, c, d, k, s, Ti)\
t = a G(b,c,d) X[k] Ti;\
a = ROTATE_LEFT(t, s) b
/* Do the following 16 operations. */
SET(a, b, c, d, 1, 5, T17);
SET(d, a, b, c, 6, 9, T18);
SET(c, d, a, b, 11, 14, T19);
SET(b, c, d, a, 0, 20, T20);
SET(a, b, c, d, 5, 5, T21);
SET(d, a, b, c, 10, 9, T22);
SET(c, d, a, b, 15, 14, T23);
SET(b, c, d, a, 4, 20, T24);
SET(a, b, c, d, 9, 5, T25);
SET(d, a, b, c, 14, 9, T26);
SET(c, d, a, b, 3, 14, T27);
SET(b, c, d, a, 8, 20, T28);
SET(a, b, c, d, 13, 5, T29);
SET(d, a, b, c, 2, 9, T30);
SET(c, d, a, b, 7, 14, T31);
SET(b, c, d, a, 12, 20, T32);
#undef SET

❷ 如何對字元串進行MD5加密,用C語言實現,給出源代碼和加密函數

#include <stdio.h>
#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
#define R_memset(x, y, z) memset(x, y, z)
#define R_memcpy(x, y, z) memcpy(x, y, z)
#define R_memcmp(x, y, z) memcmp(x, y, z)
typedef unsigned long UINT4;
typedef unsigned char *POINTER;
typedef struct {
/*四個32bits數,用於存放最終計算得到的消息摘要.當消息長度>512bits時,也用於存放每個512bits的中間結果*/
UINT4 state[4];
/*存儲原始信息的bits數長度,不包括填充的bits,最長為2^64 bits*/
UINT4 count[2];
/*存放輸入的信息的緩沖區,512bits*/
unsigned char buffer[64];
} MD5_CTX;
static void MD5Transform(UINT4[4], unsigned char[64]);
static void Encode(unsigned char *, UINT4 *, unsigned int);
static void Decode(UINT4 *, unsigned char *, unsigned int);
/*
用於bits填充的緩沖區,當欲加密的信息的bits數被512除其餘數為448時,需要填充的bits的最大值為512=64*8*/
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
};
/*接下來的這幾個宏定義是md5演算法規定的,就是對信息進行md5加密都要做的運算*/
#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)))
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define FF(a, b, c, d, x, s, ac) {\
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}
#define GG(a, b, c, d, x, s, ac) {\
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}
#define HH(a, b, c, d, x, s, ac) {\
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}
#define II(a, b, c, d, x, s, ac) {\
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac);\
(a) = ROTATE_LEFT ((a), (s));\
(a) += (b);\
}

❸ C++ md5演算法

調用:
CMD5_VC::MD5(pBuf, nLength);
CMD5_VC::MD5(fFile);
CMD5_VC::MD5(strFilePath);

原代碼:

// MD5Checksum.h: interface for the MD5Checksum class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)
#define AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMD5_VC
{
public:
//interface functions for the RSA MD5 calculation
static CString MD5(BYTE* pBuf, UINT nLength);
static CString MD5(CFile& File);
static CString MD5(const CString& strFilePath);

protected:
//constructor/destructor
CMD5_VC();
virtual ~CMD5_VC() {};

//RSA MD5 implementation
void Transform(BYTE Block[64]);
void Update(BYTE* Input, ULONG nInputLen);
CString Final();
inline DWORD RotateLeft(DWORD x, int n);
inline void FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);
inline void GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);
inline void HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);
inline void II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);

//utility functions
void DWordToByte(BYTE* Output, DWORD* Input, UINT nLength);
void ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength);

private:
BYTE m_lpszBuffer[64]; //input buffer
ULONG m_nCount[2]; //number of bits, molo 2^64 (lsb first)
ULONG m_lMD5[4]; //MD5 checksum
};

#endif // !defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)

//////////////////////////////////////////////////////////////////////////////////

//Magic initialization constants
#define MD5_INIT_STATE_0 0x67452301
#define MD5_INIT_STATE_1 0xefcdab89
#define MD5_INIT_STATE_2 0x98badcfe
#define MD5_INIT_STATE_3 0x10325476

//Constants for Transform routine.
#define MD5_S11 7
#define MD5_S12 12
#define MD5_S13 17
#define MD5_S14 22
#define MD5_S21 5
#define MD5_S22 9
#define MD5_S23 14
#define MD5_S24 20
#define MD5_S31 4
#define MD5_S32 11
#define MD5_S33 16
#define MD5_S34 23
#define MD5_S41 6
#define MD5_S42 10
#define MD5_S43 15
#define MD5_S44 21

//Transformation Constants - Round 1
#define MD5_T01 0xd76aa478 //Transformation Constant 1
#define MD5_T02 0xe8c7b756 // 2
#define MD5_T03 0x242070db // 3
#define MD5_T04 0xc1bdceee // 4
#define MD5_T05 0xf57c0faf // 5
#define MD5_T06 0x4787c62a // 6
#define MD5_T07 0xa8304613 // 7
#define MD5_T08 0xfd469501 // 8
#define MD5_T09 0x698098d8 // 9
#define MD5_T10 0x8b44f7af // 10
#define MD5_T11 0xffff5bb1 // 11
#define MD5_T12 0x895cd7be // 12
#define MD5_T13 0x6b901122 // 13
#define MD5_T14 0xfd987193 // 14
#define MD5_T15 0xa679438e // 15
#define MD5_T16 0x49b40821 // 16

//s - Round 2
#define MD5_T17 0xf61e2562 // 17
#define MD5_T18 0xc040b340 // 18
#define MD5_T19 0x265e5a51 // 19
#define MD5_T20 0xe9b6c7aa // 20
#define MD5_T21 0xd62f105d // 21
#define MD5_T22 0x02441453 // 22
#define MD5_T23 0xd8a1e681 // 23
#define MD5_T24 0xe7d3fbc8 // 24
#define MD5_T25 0x21e1cde6 // 25
#define MD5_T26 0xc33707d6 // 26
#define MD5_T27 0xf4d50d87 // 27
#define MD5_T28 0x455a14ed // 28
#define MD5_T29 0xa9e3e905 // 29
#define MD5_T30 0xfcefa3f8 // 30
#define MD5_T31 0x676f02d9 // 31
#define MD5_T32 0x8d2a4c8a // 32

//s - Round 3
#define MD5_T33 0xfffa3942 // 33
#define MD5_T34 0x8771f681 // 34
#define MD5_T35 0x6d9d6122 // 35
#define MD5_T36 0xfde5380c // 36
#define MD5_T37 0xa4beea44 // 37
#define MD5_T38 0x4bdecfa9 // 38
#define MD5_T39 0xf6bb4b60 // 39
#define MD5_T40 0xbebfbc70 // 40
#define MD5_T41 0x289b7ec6 // 41
#define MD5_T42 0xeaa127fa // 42
#define MD5_T43 0xd4ef3085 // 43
#define MD5_T44 0x04881d05 // 44
#define MD5_T45 0xd9d4d039 // 45
#define MD5_T46 0xe6db99e5 // 46
#define MD5_T47 0x1fa27cf8 // 47
#define MD5_T48 0xc4ac5665 // 48

//s - Round 4
#define MD5_T49 0xf4292244 // 49
#define MD5_T50 0x432aff97 // 50
#define MD5_T51 0xab9423a7 // 51
#define MD5_T52 0xfc93a039 // 52
#define MD5_T53 0x655b59c3 // 53
#define MD5_T54 0x8f0ccc92 // 54
#define MD5_T55 0xffeff47d // 55
#define MD5_T56 0x85845dd1 // 56
#define MD5_T57 0x6fa87e4f // 57
#define MD5_T58 0xfe2ce6e0 // 58
#define MD5_T59 0xa3014314 // 59
#define MD5_T60 0x4e0811a1 // 60
#define MD5_T61 0xf7537e82 // 61
#define MD5_T62 0xbd3af235 // 62
#define MD5_T63 0x2ad7d2bb // 63
#define MD5_T64 0xeb86d391 // 64

//Null data (except for first BYTE) used to finalise the checksum calculation
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
};

// MD5Checksum.cpp: implementation of the MD5Checksum class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MD5_VC.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CString CMD5_VC::MD5(const CString& strFilePath)
{
//open the file as a binary file in readonly mode, denying write access
CFile File(strFilePath, CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary);

//the file has been successfully opened, so now get and return its checksum
return MD5(File);
}

CString CMD5_VC::MD5(CFile& File)
{
try
{
CMD5_VC MD5_VC; //MD5_VC object
int nLength = 0; //number of bytes read from the file
const int nBufferSize = 1024; //checksum the file in blocks of 1024 bytes
BYTE Buffer[nBufferSize]; //buffer for data read from the file

//checksum the file in blocks of 1024 bytes
while ((nLength = File.Read( Buffer, nBufferSize )) > 0 )
{
MD5_VC.Update( Buffer, nLength );
}

//finalise the checksum and return it
return MD5_VC.Final();
}

//report any file exceptions in debug mode only
catch (CFileException* e )
{
TRACE0("CMD5_VC::MD5: CFileException caught");
throw e;
}
}

CString CMD5_VC::MD5(BYTE* pBuf, UINT nLength)
{
//entry invariants
AfxIsValidAddress(pBuf,nLength,FALSE);

//calculate and return the checksum
CMD5_VC MD5_VC;
MD5_VC.Update( pBuf, nLength );
return MD5_VC.Final();
}

DWORD CMD5_VC::RotateLeft(DWORD x, int n)
{
//check that DWORD is 4 bytes long - true in Visual C++ 6 and 32 bit Windows
ASSERT( sizeof(x) == 4 );

//rotate and return x
return (x << n) | (x >> (32-n));
}

void CMD5_VC::FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
DWORD F = (B & C) | (~B & D);
A += F + X + T;
A = RotateLeft(A, S);
A += B;
}

void CMD5_VC::GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
DWORD G = (B & D) | (C & ~D);
A += G + X + T;
A = RotateLeft(A, S);
A += B;
}

void CMD5_VC::HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
DWORD H = (B ^ C ^ D);
A += H + X + T;
A = RotateLeft(A, S);
A += B;
}

void CMD5_VC::II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
DWORD I = (C ^ (B | ~D));
A += I + X + T;
A = RotateLeft(A, S);
A += B;
}

void CMD5_VC::Transform(BYTE Block[64])
{
//initialise local data with current checksum
ULONG a = m_lMD5[0];
ULONG b = m_lMD5[1];
ULONG c = m_lMD5[2];
ULONG d = m_lMD5[3];

// BYTES from input 'Block' to an array of ULONGS 'X'
ULONG X[16];
ByteToDWord( X, Block, 64 );

//Perform Round 1 of the transformation
FF (a, b, c, d, X[ 0], MD5_S11, MD5_T01);
FF (d, a, b, c, X[ 1], MD5_S12, MD5_T02);
FF (c, d, a, b, X[ 2], MD5_S13, MD5_T03);
FF (b, c, d, a, X[ 3], MD5_S14, MD5_T04);
FF (a, b, c, d, X[ 4], MD5_S11, MD5_T05);
FF (d, a, b, c, X[ 5], MD5_S12, MD5_T06);
FF (c, d, a, b, X[ 6], MD5_S13, MD5_T07);
FF (b, c, d, a, X[ 7], MD5_S14, MD5_T08);
FF (a, b, c, d, X[ 8], MD5_S11, MD5_T09);
FF (d, a, b, c, X[ 9], MD5_S12, MD5_T10);
FF (c, d, a, b, X[10], MD5_S13, MD5_T11);
FF (b, c, d, a, X[11], MD5_S14, MD5_T12);
FF (a, b, c, d, X[12], MD5_S11, MD5_T13);
FF (d, a, b, c, X[13], MD5_S12, MD5_T14);
FF (c, d, a, b, X[14], MD5_S13, MD5_T15);
FF (b, c, d, a, X[15], MD5_S14, MD5_T16);

//Perform Round 2 of the transformation
GG (a, b, c, d, X[ 1], MD5_S21, MD5_T17);
GG (d, a, b, c, X[ 6], MD5_S22, MD5_T18);
GG (c, d, a, b, X[11], MD5_S23, MD5_T19);
GG (b, c, d, a, X[ 0], MD5_S24, MD5_T20);
GG (a, b, c, d, X[ 5], MD5_S21, MD5_T21);
GG (d, a, b, c, X[10], MD5_S22, MD5_T22);
GG (c, d, a, b, X[15], MD5_S23, MD5_T23);
GG (b, c, d, a, X[ 4], MD5_S24, MD5_T24);
GG (a, b, c, d, X[ 9], MD5_S21, MD5_T25);
GG (d, a, b, c, X[14], MD5_S22, MD5_T26);
GG (c, d, a, b, X[ 3], MD5_S23, MD5_T27);
GG (b, c, d, a, X[ 8], MD5_S24, MD5_T28);
GG (a, b, c, d, X[13], MD5_S21, MD5_T29);
GG (d, a, b, c, X[ 2], MD5_S22, MD5_T30);
GG (c, d, a, b, X[ 7], MD5_S23, MD5_T31);
GG (b, c, d, a, X[12], MD5_S24, MD5_T32);

//Perform Round 3 of the transformation
HH (a, b, c, d, X[ 5], MD5_S31, MD5_T33);
HH (d, a, b, c, X[ 8], MD5_S32, MD5_T34);
HH (c, d, a, b, X[11], MD5_S33, MD5_T35);
HH (b, c, d, a, X[14], MD5_S34, MD5_T36);
HH (a, b, c, d, X[ 1], MD5_S31, MD5_T37);
HH (d, a, b, c, X[ 4], MD5_S32, MD5_T38);
HH (c, d, a, b, X[ 7], MD5_S33, MD5_T39);
HH (b, c, d, a, X[10], MD5_S34, MD5_T40);
HH (a, b, c, d, X[13], MD5_S31, MD5_T41);
HH (d, a, b, c, X[ 0], MD5_S32, MD5_T42);
HH (c, d, a, b, X[ 3], MD5_S33, MD5_T43);
HH (b, c, d, a, X[ 6], MD5_S34, MD5_T44);
HH (a, b, c, d, X[ 9], MD5_S31, MD5_T45);
HH (d, a, b, c, X[12], MD5_S32, MD5_T46);
HH (c, d, a, b, X[15], MD5_S33, MD5_T47);
HH (b, c, d, a, X[ 2], MD5_S34, MD5_T48);

//Perform Round 4 of the transformation
II (a, b, c, d, X[ 0], MD5_S41, MD5_T49);
II (d, a, b, c, X[ 7], MD5_S42, MD5_T50);
II (c, d, a, b, X[14], MD5_S43, MD5_T51);
II (b, c, d, a, X[ 5], MD5_S44, MD5_T52);
II (a, b, c, d, X[12], MD5_S41, MD5_T53);
II (d, a, b, c, X[ 3], MD5_S42, MD5_T54);
II (c, d, a, b, X[10], MD5_S43, MD5_T55);
II (b, c, d, a, X[ 1], MD5_S44, MD5_T56);
II (a, b, c, d, X[ 8], MD5_S41, MD5_T57);
II (d, a, b, c, X[15], MD5_S42, MD5_T58);
II (c, d, a, b, X[ 6], MD5_S43, MD5_T59);
II (b, c, d, a, X[13], MD5_S44, MD5_T60);
II (a, b, c, d, X[ 4], MD5_S41, MD5_T61);
II (d, a, b, c, X[11], MD5_S42, MD5_T62);
II (c, d, a, b, X[ 2], MD5_S43, MD5_T63);
II (b, c, d, a, X[ 9], MD5_S44, MD5_T64);

//add the transformed values to the current checksum
m_lMD5[0] += a;
m_lMD5[1] += b;
m_lMD5[2] += c;
m_lMD5[3] += d;
}

CMD5_VC::CMD5_VC()
{
// zero members
memset( m_lpszBuffer, 0, 64 );
m_nCount[0] = m_nCount[1] = 0;

// Load magic state initialization constants
m_lMD5[0] = MD5_INIT_STATE_0;
m_lMD5[1] = MD5_INIT_STATE_1;
m_lMD5[2] = MD5_INIT_STATE_2;
m_lMD5[3] = MD5_INIT_STATE_3;
}

void CMD5_VC::ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength)
{
//entry invariants
ASSERT( nLength % 4 == 0 );
ASSERT( AfxIsValidAddress(Output, nLength/4, TRUE) );
ASSERT( AfxIsValidAddress(Input, nLength, FALSE) );

//initialisations
UINT i=0; //index to Output array
UINT j=0; //index to Input array

//transfer the data by shifting and ing
for ( ; j < nLength; i++, j += 4)
{
Output[i] = (ULONG)Input[j] |
(ULONG)Input[j+1] << 8 |
(ULONG)Input[j+2] << 16 |
(ULONG)Input[j+3] << 24;
}
}

void CMD5_VC::DWordToByte(BYTE* Output, DWORD* Input, UINT nLength )
{
//entry invariants
ASSERT( nLength % 4 == 0 );
ASSERT( AfxIsValidAddress(Output, nLength, TRUE) );
ASSERT( AfxIsValidAddress(Input, nLength/4, FALSE) );

//transfer the data by shifting and ing
UINT i = 0;
UINT j = 0;
for ( ; j < nLength; i++, j += 4)
{
Output[j] = (UCHAR)(Input[i] & 0xff);
Output[j+1] = (UCHAR)((Input[i] >> 8) & 0xff);
Output[j+2] = (UCHAR)((Input[i] >> 16) & 0xff);
Output[j+3] = (UCHAR)((Input[i] >> 24) & 0xff);
}
}

CString CMD5_VC::Final()
{
//Save number of bits
BYTE Bits[8];
DWordToByte( Bits, m_nCount, 8 );

//Pad out to 56 mod 64.
UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3f);
UINT nPadLen = (nIndex < 56) ? (56 - nIndex) : (120 - nIndex);
Update( PADDING, nPadLen );

//Append length (before padding)
Update( Bits, 8 );

//Store final state in 'lpszMD5'
const int nMD5Size = 16;
unsigned char lpszMD5[ nMD5Size ];
DWordToByte( lpszMD5, m_lMD5, nMD5Size );

//Convert the hexadecimal checksum to a CString
CString strMD5;
for ( int i=0; i < nMD5Size; i++)
{
CString Str;
if (lpszMD5[i] == 0) {
Str = CString("00");
}
else if (lpszMD5[i] <= 15) {
Str.Format("0%X",lpszMD5[i]);
}
else {
Str.Format("%X",lpszMD5[i]);
}

ASSERT( Str.GetLength() == 2 );
strMD5 += Str;
}
ASSERT( strMD5.GetLength() == 32 );
return strMD5;
}

void CMD5_VC::Update( BYTE* Input, ULONG nInputLen )
{
//Compute number of bytes mod 64
UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3F);

//Update number of bits
if ( ( m_nCount[0] += nInputLen << 3 ) < ( nInputLen << 3) )
{
m_nCount[1]++;
}
m_nCount[1] += (nInputLen >> 29);

//Transform as many times as possible.
UINT i=0;
UINT nPartLen = 64 - nIndex;
if (nInputLen >= nPartLen)
{
memcpy( &m_lpszBuffer[nIndex], Input, nPartLen );
Transform( m_lpszBuffer );
for (i = nPartLen; i + 63 < nInputLen; i += 64)
{
Transform( &Input[i] );
}
nIndex = 0;
}
else
{
i = 0;
}

// Buffer remaining input
memcpy( &m_lpszBuffer[nIndex], &Input[i], nInputLen-i);
}

❹ 求MD5加密演算法的C語言源碼

(轉載) #include <stdio.h> #include <stdlib.h> #include <memory.h> #include <time.h> #include <errno.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include "../md5/md5.h" #define T1 0xd76aa478 #define T2 0xe8c7b756 #define T3 0x242070db #define T4 0xc1bdceee #define T5 0xf57c0faf #define T6 0x4787c62a #define T7 0xa8304613 #define T8 0xfd469501 #define T9 0x698098d8 #define T10 0x8b44f7af #define T11 0xffff5bb1 #define T12 0x895cd7be #define T13 0x6b901122 #define T14 0xfd987193 #define T15 0xa679438e #define T16 0x49b40821 #define T17 0xf61e2562 #define T18 0xc040b340 #define T19 0x265e5a51 #define T20 0xe9b6c7aa #define T21 0xd62f105d #define T22 0x02441453 #define T23 0xd8a1e681 #define T24 0xe7d3fbc8 #define T25 0x21e1cde6 #define T26 0xc33707d6 #define T27 0xf4d50d87 #define T28 0x455a14ed #define T29 0xa9e3e905 #define T30 0xfcefa3f8 #define T31 0x676f02d9 #define T32 0x8d2a4c8a #define T33 0xfffa3942 #define T34 0x8771f681 #define T35 0x6d9d6122 #define T36 0xfde5380c #define T37 0xa4beea44 #define T38 0x4bdecfa9 #define T39 0xf6bb4b60 #define T40 0xbebfbc70 #define T41 0x289b7ec6 #define T42 0xeaa127fa #define T43 0xd4ef3085 #define T44 0x04881d05 #define T45 0xd9d4d039 #define T46 0xe6db99e5 #define T47 0x1fa27cf8 #define T48 0xc4ac5665 #define T49 0xf4292244 #define T50 0x432aff97 #define T51 0xab9423a7 #define T52 0xfc93a039 #define T53 0x655b59c3 #define T54 0x8f0ccc92 #define T55 0xffeff47d #define T56 0x85845dd1 #define T57 0x6fa87e4f #define T58 0xfe2ce6e0 #define T59 0xa3014314 #define T60 0x4e0811a1 #define T61 0xf7537e82 #define T62 0xbd3af235 #define T63 0x2ad7d2bb #define T64 0xeb86d391 static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/) { md5_word_t a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2], d = pms->abcd[3]; md5_word_t t; #ifndef ARCH_IS_BIG_ENDIAN # define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */ #endif #if ARCH_IS_BIG_ENDIAN /* * On big-endian machines, we must arrange the bytes in the right * order. (This also works on machines of unknown byte order.) */ md5_word_t X[16]; const md5_byte_t *xp = data; int i; for (i = 0; i < 16; i, xp = 4) X[i] = xp[0] (xp[1] << 8) (xp[2] << 16) (xp[3] << 24); #else /* !ARCH_IS_BIG_ENDIAN */ /* * On little-endian machines, we can process properly aligned data * without ing it. */ md5_word_t xbuf[16]; const md5_word_t *X; if (!((data - (const md5_byte_t *)0) & 3)) { /* data are properly aligned */ X = (const md5_word_t *)data; } else { /* not aligned */ memcpy(xbuf, data, 64); X = xbuf; } #endif #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) /* Round 1. */ /* Let [abcd k s i] denote the operation a = b ((a F(b,c,d) X[k] T[i]) <<< s). */ #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) #define SET(a, b, c, d, k, s, Ti)\ t = a F(b,c,d) X[k] Ti;\ a = ROTATE_LEFT(t, s) b /* Do the following 16 operations. */ SET(a, b, c, d, 0, 7, T1); SET(d, a, b, c, 1, 12, T2); SET(c, d, a, b, 2, 17, T3); SET(b, c, d, a, 3, 22, T4); SET(a, b, c, d, 4, 7, T5); SET(d, a, b, c, 5, 12, T6); SET(c, d, a, b, 6, 17, T7); SET(b, c, d, a, 7, 22, T8); SET(a, b, c, d, 8, 7, T9); SET(d, a, b, c, 9, 12, T10); SET(c, d, a, b, 10, 17, T11); SET(b, c, d, a, 11, 22, T12); SET(a, b, c, d, 12, 7, T13); SET(d, a, b, c, 13, 12, T14); SET(c, d, a, b, 14, 17, T15); SET(b, c, d, a, 15, 22, T16); #undef SET /* Round 2. */ /* Let [abcd k s i] denote the operation a = b ((a G(b,c,d) X[k] T[i]) <<< s). */ #define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) #define SET(a, b, c, d, k, s, Ti)\ t = a G(b,c,d) X[k] Ti;\ a = ROTATE_LEFT(t, s) b /* Do the following 16 operations. */ SET(a, b, c, d, 1, 5, T17); SET(d, a, b, c, 6, 9, T18); SET(c, d, a, b, 11, 14, T19); SET(b, c, d, a, 0, 20, T20); SET(a, b, c, d, 5, 5, T21); SET(d, a, b, c, 10, 9, T22); SET(c, d, a, b, 15, 14, T23); SET(b, c, d, a, 4, 20, T24); SET(a, b, c, d, 9, 5, T25); SET(d, a, b, c, 14, 9, T26); SET(c, d, a, b, 3, 14, T27); SET(b, c, d, a, 8, 20, T28); SET(a, b, c, d, 13, 5, T29); SET(d, a, b, c, 2, 9, T30); SET(c, d, a, b, 7, 14, T31); SET(b, c, d, a, 12, 20, T32); #undef SET

❺ 如何對字元串進行MD5加密,用C語言實現,給出源代碼和加密函數

/*四個32bits數,用於存放最終計算得到的消息摘要.當消息長度>512bits時,也用於存放每個512bits的中間結果*/ UINT4 state[4]; /*存儲原始信息的bits數長度,不包括填充的bits,最長為2^64 bits*/ UINT4 count[2]; /*存放輸入的信息的緩沖區,512bits*/ unsigned char buffer[64];} MD5_CTX;static void MD5Transform(UINT4[4], unsigned char[64]); static void Encode(unsigned char *, UINT4 *, unsigned int); static void Decode(UINT4 *, unsigned char *, unsigned int);/*用於bits填充的緩沖區,當欲加密的信息的bits數被512除其餘數為448時,需要填充的bits的最大值為512=64*8*/ 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};/*接下來的這幾個宏定義是md5演算法規定的,就是對信息進行md5加密都要做的運算*/ #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))) #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) #define FF(a, b, c, d, x, s, ac) {\ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac);\ (a) = ROTATE_LEFT ((a), (s));\ (a) += (b);\}#define GG(a, b, c, d, x, s, ac) {\ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac);\ (a) = ROTATE_LEFT ((a), (s));\ (a) += (b);\}#define HH(a, b, c, d, x, s, ac) {\ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac);\ (a) = ROTATE_LEFT ((a), (s));\ (a) += (b);\}#define II(a, b, c, d, x, s, ac) {\ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac);\

❻ 如何用C語言實現MD5演算法計算一個文本的消息摘要

MD5是不可能逆向的。
王教授的碰撞法是利用了MD5或者SHA1演算法的一個特性,
根據MD5和SHA1等Hash演算法的特點,因為他們是任意長度的字元串變成固定長度的摘要信息。
那麼這里就有可能發生一個問題,就是不同的字元串在理論上是有可能產生相同的摘要信息。
王教授所謂的碰撞法,碰撞的就是不同的字元串所產生的摘要信息是一樣的那些字元串。因此得名碰撞法。
碰撞就是體現在這里。沒有什麼其它的傳神的東西了。根據SHA1和MD5等Hash演算法,在設計時候,設計這個演算法的人認為不同的字元串要產生相同結果的摘要信息的可能性幾乎為零。而王教授則證明了SHA1和MD5等Hash演算法產生的摘要信息規則是可以在比較短時間內被破解的。這樣一來,原始數據的
Integrity
就被打破了。
所謂的破解,也就是體現在這里。
至於破解工具,下面的地址提供一些免費的破解服務,能破解一些簡單的密碼,其實都是採用字典或暴力破解。
www.cmd5.com
www.xmd5.org
我有時去破解一些常用的密碼,有一定的成功率

❼ C語言求文件MD5的函數用法

#ifndef MD5_H
#define MD5_H

typedef struct
{
unsigned int count[2];
unsigned int state[4];
unsigned char buffer[64];
}MD5_CTX;

#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))
#define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
#define FF(a,b,c,d,x,s,ac) \
{ \
a += F(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define GG(a,b,c,d,x,s,ac) \
{ \
a += G(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define HH(a,b,c,d,x,s,ac) \
{ \
a += H(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define II(a,b,c,d,x,s,ac) \
{ \
a += I(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
void MD5Init(MD5_CTX *context);
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen);
void MD5Final(MD5_CTX *context,unsigned char digest[16]);
void MD5Transform(unsigned int state[4],unsigned char block[64]);
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len);
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len);

#endif

源文件md5.c

#include <memory.h>
#include "md5.h"

unsigned char PADDING[]={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};

void MD5Init(MD5_CTX *context)
{
context->count[0] = 0;
context->count[1] = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
}
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen)
{
unsigned int i = 0,index = 0,partlen = 0;
index = (context->count[0] >> 3) & 0x3F;
partlen = 64 - index;
context->count[0] += inputlen << 3;
if(context->count[0] < (inputlen << 3))
context->count[1]++;
context->count[1] += inputlen >> 29;

if(inputlen >= partlen)
{
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i = partlen;i+64 <= inputlen;i+=64)
MD5Transform(context->state,&input[i]);
index = 0;
}
else
{
i = 0;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}
void MD5Final(MD5_CTX *context,unsigned char digest[16])
{
unsigned int index = 0,padlen = 0;
unsigned char bits[8];
index = (context->count[0] >> 3) & 0x3F;
padlen = (index < 56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);
}
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[j] = input[i] & 0xFF;
output[j+1] = (input[i] >> 8) & 0xFF;
output[j+2] = (input[i] >> 16) & 0xFF;
output[j+3] = (input[i] >> 24) & 0xFF;
i++;
j+=4;
}
}
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[i] = (input[j]) |
(input[j+1] << 8) |
(input[j+2] << 16) |
(input[j+3] << 24);
i++;
j+=4;
}
}
void MD5Transform(unsigned int state[4],unsigned char block[64])
{
unsigned int a = state[0];
unsigned int b = state[1];
unsigned int c = state[2];
unsigned int d = state[3];
unsigned int x[64];
MD5Decode(x,block,64);
FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */

❽ C#中怎樣實現MD5加密MD5中加密演算法16位加密演算法與32位加密演算法怎麼實現

作者:FlyMe聯系方式:7826-45-210 707-628-841 public class md5 {
//static state variables
private static uint32 a;
private static uint32 b;
private static uint32 c;
private static uint32 d;

//number of bits to rotate in tranforming
private const int s11 = 7;
private const int s12 = 12;
private const int s13 = 17;
private const int s14 = 22;
private const int s21 = 5;
private const int s22 = Array;
private const int s23 = 14;
private const int s24 = 20;
private const int s31 = 4;
private const int s32 = 11;
private const int s33 = 16;
private const int s34 = 23;
private const int s41 = 6;
private const int s42 = 10;
private const int s43 = 15;
private const int s44 = 21;

/* f, g, h and i are basic md5 functions.
* 四個非線性函數:
*
* f(x,y,z) =(x&y)|((~x)&z)
* g(x,y,z) =(x&z)|(y&(~z))
* h(x,y,z) =x^y^z
* i(x,y,z)=y^(x|(~z))
*
* (&與,|或,~非,^異或)
*/
private static uint32 f(uint32 x,uint32 y,uint32 z){
return (x&y)|((~x)&z);
}
private static uint32 g(uint32 x,uint32 y,uint32 z){
return (x&z)|(y&(~z));
}
private static uint32 h(uint32 x,uint32 y,uint32 z){
return x^y^z;
}
private static uint32 i(uint32 x,uint32 y,uint32 z){
return y^(x|(~z));
}

/* ff, gg, hh, and ii transformations for rounds 1, 2, 3, and 4.
* rotation is separate from addition to prevent recomputation.
*/
private static void ff(ref uint32 a,uint32 b,uint32 c,uint32 d,uint32 mj,int s,uint32 ti){
a = a + f(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void gg(ref uint32 a,uint32 b,uint32 c,uint32 d,uint32 mj,int s,uint32 ti){
a = a + g(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void hh(ref uint32 a,uint32 b,uint32 c,uint32 d,uint32 mj,int s,uint32 ti){
a = a + h(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}
private static void ii(ref uint32 a,uint32 b,uint32 c,uint32 d,uint32 mj,int s,uint32 ti){
a = a + i(b,c,d) + mj + ti;
a = a << s | a >> (32-s);
a += b;
}

private static void md5_init(){
a=0x67452301; //in memory, this is 0x01234567
b=0xefcdab8Array; //in memory, this is 0x8Arrayabcdef
c=0xArray8badcfe; //in memory, this is 0xfedcbaArray8
d=0x10325476; //in memory, this is 0x76543210
}

private static uint32[] md5_append(byte[] input){
int zeros=0;
int ones =1;
int size=0;
int n = input.length;
int m = n%64;
if( m < 56 ){
zeros = 55-m;
size=n-m+64;
}
else if (m==56){
zeros = 0;
ones = 0;
size=n+8;
}
else{
zeros = 63-m+56;
size=n+64-m+64;
}

arraylist bs = new arraylist(input);
if(ones==1){
bs.add( (byte)0x80 ); // 0x80 = $10000000
}
for(int i=0;i<zeros;i++){
bs.add( (byte)0 );
}

uint64 n = (uint64) n * 8;
byte h1=(byte)(n&0xff);
byte h2=(byte)((n>>8)&0xff);
byte h3=(byte)((n>>16)&0xff);
byte h4=(byte)((n>>24)&0xff);
byte h5=(byte)((n>>32)&0xff);
byte h6=(byte)((n>>40)&0xff);
byte h7=(byte)((n>>48)&0xff);
byte h8=(byte)(n>>56);
bs.add(h1);
bs.add(h2);
bs.add(h3);
bs.add(h4);
bs.add(h5);
bs.add(h6);
bs.add(h7);
bs.add(h8);
byte[] ts=(byte[])bs.toarray(typeof(byte));

/* decodes input (byte[]) into output (uint32[]). assumes len is
* a multiple of 4.
*/
uint32[] output = new uint32[size/4];
for(int64 i=0,j=0;i<size;j++,i+=4){
output[j]=(uint32)(ts[i] | ts[i+1]<<8 | ts[i+2]<<16 | ts[i+3]<<24);
}
return output;
}
private static uint32[] md5_trasform(uint32[] x){

uint32 a,b,c,d;

for(int k=0;k<x.length;k+=16){
a=a;
b=b;
c=c;
d=d;

/* round 1 */
ff (ref a, b, c, d, x[k+ 0], s11, 0xd76aa478); /* 1 */
ff (ref d, a, b, c, x[k+ 1], s12, 0xe8c7b756); /* 2 */
ff (ref c, d, a, b, x[k+ 2], s13, 0x242070db); /* 3 */
ff (ref b, c, d, a, x[k+ 3], s14, 0xc1bdceee); /* 4 */
ff (ref a, b, c, d, x[k+ 4], s11, 0xf57c0faf); /* 5 */
ff (ref d, a, b, c, x[k+ 5], s12, 0x4787c62a); /* 6 */
ff (ref c, d, a, b, x[k+ 6], s13, 0xa8304613); /* 7 */
ff (ref b, c, d, a, x[k+ 7], s14, 0xfd46Array501); /* 8 */
ff (ref a, b, c, d, x[k+ 8], s11, 0x6Array80Array8d8); /* Array */
ff (ref d, a, b, c, x[k+ Array], s12, 0x8b44f7af); /* 10 */
ff (ref c, d, a, b, x[k+10], s13, 0xffff5bb1); /* 11 */
ff (ref b, c, d, a, x[k+11], s14, 0x8Array5cd7be); /* 12 */
ff (ref a, b, c, d, x[k+12], s11, 0x6bArray01122); /* 13 */
ff (ref d, a, b, c, x[k+13], s12, 0xfdArray871Array3); /* 14 */
ff (ref c, d, a, b, x[k+14], s13, 0xa67Array438e); /* 15 */
ff (ref b, c, d, a, x[k+15], s14, 0x4Arrayb40821); /* 16 */

/* round 2 */
gg (ref a, b, c, d, x[k+ 1], s21, 0xf61e2562); /* 17 */
gg (ref d, a, b, c, x[k+ 6], s22, 0xc040b340); /* 18 */
gg (ref c, d, a, b, x[k+11], s23, 0x265e5a51); /* 1Array */
gg (ref b, c, d, a, x[k+ 0], s24, 0xeArrayb6c7aa); /* 20 */
gg (ref a, b, c, d, x[k+ 5], s21, 0xd62f105d); /* 21 */
gg (ref d, a, b, c, x[k+10], s22, 0x2441453); /* 22 */
gg (ref c, d, a, b, x[k+15], s23, 0xd8a1e681); /* 23 */
gg (ref b, c, d, a, x[k+ 4], s24, 0xe7d3fbc8); /* 24 */
gg (ref a, b, c, d, x[k+ Array], s21, 0x21e1cde6); /* 25 */
gg (ref d, a, b, c, x[k+14], s22, 0xc33707d6); /* 26 */
gg (ref c, d, a, b, x[k+ 3], s23, 0xf4d50d87); /* 27 */
gg (ref b, c, d, a, x[k+ 8], s24, 0x455a14ed); /* 28 */
gg (ref a, b, c, d, x[k+13], s21, 0xaArraye3eArray05); /* 2Array */
gg (ref d, a, b, c, x[k+ 2], s22, 0xfcefa3f8); /* 30 */
gg (ref c, d, a, b, x[k+ 7], s23, 0x676f02dArray); /* 31 */
gg (ref b, c, d, a, x[k+12], s24, 0x8d2a4c8a); /* 32 */

/* round 3 */
hh (ref a, b, c, d, x[k+ 5], s31, 0xfffa3Array42); /* 33 */
hh (ref d, a, b, c, x[k+ 8], s32, 0x8771f681); /* 34 */
hh (ref c, d, a, b, x[k+11], s33, 0x6dArrayd6122); /* 35 */
hh (ref b, c, d, a, x[k+14], s34, 0xfde5380c); /* 36 */
hh (ref a, b, c, d, x[k+ 1], s31, 0xa4beea44); /* 37 */
hh (ref d, a, b, c, x[k+ 4], s32, 0x4bdecfaArray); /* 38 */
hh (ref c, d, a, b, x[k+ 7], s33, 0xf6bb4b60); /* 3Array */
hh (ref b, c, d, a, x[k+10], s34, 0xbebfbc70); /* 40 */
hh (ref a, b, c, d, x[k+13], s31, 0x28Arrayb7ec6); /* 41 */
hh (ref d, a, b, c, x[k+ 0], s32, 0xeaa127fa); /* 42 */
hh (ref c, d, a, b, x[k+ 3], s33, 0xd4ef3085); /* 43 */
hh (ref b, c, d, a, x[k+ 6], s34, 0x4881d05); /* 44 */
hh (ref a, b, c, d, x[k+ Array], s31, 0xdArrayd4d03Array); /* 45 */
hh (ref d, a, b, c, x[k+12], s32, 0xe6dbArrayArraye5); /* 46 */
hh (ref c, d, a, b, x[k+15], s33, 0x1fa27cf8); /* 47 */
hh (ref b, c, d, a, x[k+ 2], s34, 0xc4ac5665); /* 48 */

/* round 4 */
ii (ref a, b, c, d, x[k+ 0], s41, 0xf42Array2244); /* 4Array */
ii (ref d, a, b, c, x[k+ 7], s42, 0x432affArray7); /* 50 */
ii (ref c, d, a, b, x[k+14], s43, 0xabArray423a7); /* 51 */
ii (ref b, c, d, a, x[k+ 5], s44, 0xfcArray3a03Array); /* 52 */
ii (ref a, b, c, d, x[k+12], s41, 0x655b5Arrayc3); /* 53 */
ii (ref d, a, b, c, x[k+ 3], s42, 0x8f0cccArray2); /* 54 */
ii (ref c, d, a, b, x[k+10], s43, 0xffeff47d); /* 55 */
ii (ref b, c, d, a, x[k+ 1], s44, 0x85845dd1); /* 56 */
ii (ref a, b, c, d, x[k+ 8], s41, 0x6fa87e4f); /* 57 */
ii (ref d, a, b, c, x[k+15], s42, 0xfe2ce6e0); /* 58 */
ii (ref c, d, a, b, x[k+ 6], s43, 0xa3014314); /* 5Array */
ii (ref b, c, d, a, x[k+13], s44, 0x4e0811a1); /* 60 */
ii (ref a, b, c, d, x[k+ 4], s41, 0xf7537e82); /* 61 */
ii (ref d, a, b, c, x[k+11], s42, 0xbd3af235); /* 62 */
ii (ref c, d, a, b, x[k+ 2], s43, 0x2ad7d2bb); /* 63 */
ii (ref b, c, d, a, x[k+ Array], s44, 0xeb86d3Array1); /* 64 */

a+=a;
b+=b;
c+=c;
d+=d;
}
return new uint32[]{a,b,c,d};
}
public static byte[] md5array(byte[] input){
md5_init();
uint32[] block = md5_append(input);
uint32[] bits = md5_trasform(block);

/* encodes bits (uint32[]) into output (byte[]). assumes len is
* a multiple of 4.
*/
byte[] output=new byte[bits.length*4];
for(int i=0,j=0;i<bits.length;i++,j+=4){
output[j] = (byte)(bits[i] & 0xff);
output[j+1] = (byte)((bits[i] >> 8) & 0xff);
output[j+2] = (byte)((bits[i] >> 16) & 0xff);
output[j+3] = (byte)((bits[i] >> 24) & 0xff);
}
return output;
}

public static string arraytohexstring(byte[] array,bool uppercase){
string hexstring="";
string format="x2";
if(uppercase){
format="x2";
}
foreach(byte b in array){
hexstring += b.tostring(format);
}
return hexstring;
}

public static string mdstring(string message){
char[] c = message.tochararray();
byte[] b = new byte[c.length];
for(int i=0;i<c.length;i++){
b[i]=(byte)c[i];
}
byte[] digest = md5array(b);
return arraytohexstring(digest,false);
}
public static string mdfile(string filename){
filestream fs=file.open(filename,filemode.open,fileaccess.read);
byte[] array=new byte[fs.length];
fs.read(array,0,(int)fs.length);
byte[] digest = md5array(array);
fs.close();
return arraytohexstring(digest,false);
}

public static string test(string message){
return "rnmd5 (""+message+"") = " + md5.mdstring(message);
}
public static string testsuite(){
string s = "";
s+=test("");
s+=test("a");
s+=test("abc");
s+=test("message digest");
s+=test("abcdefghijklmnopqrstuvwxyz");
s+=test("");
s+=test("");
return s;
}
}

❾ 用c語言實現python的md5功能

題中所示代碼中,python實現了計算空字元串的MD5值,並對MD5的值的十六進制的字元串所表示的位元組進行BASE64處理。

不像Python內部有實現md5功能,根據ANSI C標准,C語言的標准庫里是沒有md5功能的;

但是RFC1231規定了MD5功能的C實現並提供了附件,可以直接用,也可以直接獲取現成的實現,在編譯鏈接時指定正確的.h頭文件和.lib靜態鏈接庫文件;

這里我採取前者的做法(電腦上沒有裝VC,有VC就簡單很多,使用的是minGW)大概六七百行代碼左右。

然後這里展示不完,給個實現效果圖

BASE64的編碼原理

❿ 請教MD5演算法 用C語言實現

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifdefined(__APPLE__)
#defineCOMMON_DIGEST_FOR_OPENSSL
#include<CommonCrypto/CommonDigest.h>
#defineSHA1CC_SHA1
#else
#include<openssl/md5.h>
#endif

//這是我自己寫的函數,用於計算MD5
//參數str:要轉換的字元串
//參數lengthL:字元串的長度可以用strlen(str)直接獲取參數str的長度
//返回值:MD5字元串
char*str2md5(constchar*str,intlength){
intn;
MD5_CTXc;
unsignedchardigest[16];
char*out=(char*)malloc(33);

MD5_Init(&c);

while(length>0){
if(length>512){
MD5_Update(&c,str,512);
}else{
MD5_Update(&c,str,length);
}
length-=512;
str+=512;
}

MD5_Final(digest,&c);

for(n=0;n<16;++n){
snprintf(&(out[n*2]),16*2,"%02x",(unsignedint)digest[n]);
}

returnout;
}

intmain(intargc,char**argv){
char*output=str2md5("hello",strlen("hello"));

printf("%s ",output);
//上面會輸出hello的MD5字元串:
//
free(output);
return0;
}

閱讀全文

與md5演算法c實現相關的資料

熱點內容
飢荒如何掛伺服器 瀏覽:242
GM附魔命令 瀏覽:599
反編譯自己製作的軟體 瀏覽:35
ros用c還是python好 瀏覽:78
安卓九系統提醒怎麼關 瀏覽:656
我的世界伺服器有領地怎麼抄家 瀏覽:36
python實現多態 瀏覽:300
幼師pdf 瀏覽:941
你怎麼用python開發游戲 瀏覽:647
雷霆戰機伺服器異常是什麼問題 瀏覽:669
程序員客棧20 瀏覽:254
化妝pdf下載 瀏覽:923
takla伺服器ip地址 瀏覽:357
歐盟加密資產法律 瀏覽:573
威綸通反編譯密碼是多少 瀏覽:201
51單片機有40個外部引腳 瀏覽:956
山西撥號伺服器雲空間 瀏覽:714
python中階乘怎麼計算 瀏覽:530
linux查看塊大小 瀏覽:554
空調壓縮機壓力低 瀏覽:184