中国安防论坛

 找回密码
 注册
查看: 5828|回复: 1

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; [) Z! |: R* p: A; W4 \ #include "md5.h"
6 ?' s& V. O" i3 t) k
5 s! x2 U! C/ e0 ?; x" ?- M /* Constants for MD5Transform routine.
*/
8 R+ A" I9 g6 r: x
. w2 Z) C8 L2 B, Y- Y" W1 ^( S; W, i
$ q ~4 x' S4 l- N4 g0 k #define S11 7
3 b( I. y! T( p* d" |#define S12 12
( F8 {6 ?& k. ?#define S13 17
( R+ L9 `1 s" y#define S14 22
: g- C$ L+ Q) G8 X#define S21 5
8 I$ a* ]1 \6 X+ G0 R- E; X#define S22 9
/ W) h3 a; t+ r$ q+ O#define S23 14
" j- i3 d1 @1 _) I8 G9 H1 X#define S24 20
E; U( l3 ]4 \ #define S31 4
5 w; {8 n# C4 F/ E#define S32 11
9 b* z6 i. ? @ b! |( N: p& L #define S33 16
# y; v7 G# w3 `( a7 i#define S34 23
* \& W, c, A- _: S. E( N4 p& u5 J#define S41 6
# {* r4 E+ y8 K! {1 K2 Y#define S42 10
. D& `4 ^0 M5 ?+ A+ J }#define S43 15
. _8 a r1 P# w8 J% w- F #define S44 21
# j9 L. G- e( ^: `
/ B0 S/ M& W2 D" h static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 h; y! b9 g9 Z' \static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' V, a. l1 O3 k3 g i* N) g static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 l; T6 c0 e# x2 jstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ ^8 s& v$ w* R3 x& s; K) W static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
6 p* x5 k( H* p
4 e. { w+ f* A1 J2 | p 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
, Z- J+ S8 }' \8 s7 @- Q2 \& M7 @ };
3 x1 x* h% p8 D" `* x* r
7 D% Y0 G0 V7 M* S) y6 l/ z/* F, G, H and I are basic MD5 functions.
*/
7 D7 P. g# Z, { v# V #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ _! \1 k0 ^+ H: n! W, E #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& E% }; u- b5 \" s; H9 [#define H(x, y, z) ((x) ^ (y) ^ (z))
$ Y4 I' r- ?! u4 Y3 I; E+ p p #define I(x, y, z) ((y) ^ ((x) | (~z)))
: D- w% P |- A* |/ X* E+ |% d
( C, D% F8 L; V @! q9 w/* ROTATE_LEFT rotates x left n bits.
*/
; f/ x4 V" ?( B B! K4 r#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 A* @4 n+ O5 G: {5 n6 Q6 x
9 o h* _! N7 o8 n2 D. X8 l/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
' F) ]' v; Z: v* r Rotation is separate from addition to prevent recomputation.
*/
7 c4 S. Y7 g- L: u7 n& r#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' g* z+ U0 V0 z e+ C#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( E, m4 `3 K) Z" d" _4 p#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* i% G4 Z/ ]1 S9 r #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 E6 R3 E: ]7 J: B4 t+ j5 Q
& i/ p" [2 U: ?/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 M! C- v; {$ ? void MD5Init (context)
' v$ f% X% E. k0 m, p# ~- Q5 mMD5_CTX *context; /* context */
( c! M1 y0 h- ?' S8 ~2 A {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
6 f9 u# n+ Q3 b8 V: Z7 z */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
a1 t2 i2 R2 U" b( f }
V; I3 |! z$ N
2 i7 l# E% p9 H Z9 E9 C7 v! L/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' ]& @* I+ f! N$ K+ o3 Jvoid MD5Update (context, input, inputLen)
. T% z: L4 Z5 j( p* m" w; G MD5_CTX *context; /* context */
" C" y" Z9 Y+ @unsigned char *input; /* input block */
+ G& h" ` H" K/ T! v$ t; cunsigned int inputLen; /* length of input block */
* ^. Z! w: i" e. `9 Y' T2 [) E9 Y) Y{
unsigned int i, index, partLen;
/ e0 p, U4 z5 z' O4 d! e; s
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
2 n4 z7 c$ A% {* W' q
/* Update number of bits */
if ((context->count[0] += ((UINT4)inputLen << 3))
&
不在競爭中變坏,就在沉默中變態

安防知名大师

探花

Rank: 7Rank: 7Rank: 7

积分
1057
QQ
发表于 2004-11-26 20:22:48 | 显示全部楼层

md5加密算法C++程序


) A1 j* J7 u0 O# e/ o* o
+ Z i; B1 q- ~; |- K: H+ [. S0 s
guoyun786@sohu.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

安豆网|Archiver|手机版|中国安防论坛 ( 粤ICP备09063021号 )

GMT+8, 2026-8-11 08:51 , Processed in 0.068464 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表