中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& y$ g* T6 B+ }& g2 O/ b #include "md5.h"
3 D. o* Y; }: d9 b3 L Z* j
' a% X. _! c4 c/* Constants for MD5Transform routine.
*/
# q! i9 J3 b2 F8 D$ I. Z: D: r+ k
: k/ D9 ]; t% S* s
5 y8 v3 }1 l. R) D& A; v+ n#define S11 7
, o( t+ V: m. l" u& X! Y#define S12 12
7 Q" c( j: H+ K u# M#define S13 17
) k4 s2 ~8 h) U7 R+ Q h) d #define S14 22
2 e0 g* }, R, N- Y- B Z& M r #define S21 5
' z( s# b2 t: `, z9 S #define S22 9
9 T5 ~( j0 i, a" H w#define S23 14
. z0 k) K% }+ S- }7 r#define S24 20
! p6 {- b/ H# l( O#define S31 4
( _) d- }2 Y( l# D #define S32 11
; _& ^8 G9 N; N; m4 P) l- \; y) |; y3 p #define S33 16
- p: t. @5 ^$ [( j& P #define S34 23
1 E5 ]6 `) A$ ^3 v2 h! \ e# r#define S41 6
, F; H- x7 @! T) \) G, K- m#define S42 10
H3 }5 Z1 } B4 ] #define S43 15
0 k: |* j e% G$ ^( P#define S44 21
* z7 L: E) k R& s
: s5 t" k9 n! }static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
# @3 ^: x o) ^4 w! H! vstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 V' q U& `& k: o. U8 i# S static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 j$ E: [9 v& g. p static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; ^: o8 v# K2 N/ ], l static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' K/ ?% R2 Q5 W f. Z3 v& g! t% }) I
0 I" x( u' t9 X4 W: L, astatic 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
8 n1 P- n0 T; ~" `: C};
- V, S1 g5 l. T! k+ j3 J2 ]& |# m9 S
, K. y0 M, Q0 K9 { /* F, G, H and I are basic MD5 functions.
*/
- h9 ^/ [8 [ F% B#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* z' ?9 F$ e- D9 ]% H, M9 I" ]9 a; [#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" K$ A1 R$ [0 u, l6 G0 _2 d2 x6 G #define H(x, y, z) ((x) ^ (y) ^ (z))
& s3 k( N x; `# M5 x2 y9 H4 ? #define I(x, y, z) ((y) ^ ((x) | (~z)))
: l( A; ~0 g O; J# L) u3 `% t n( T* e
% q' v, T# C( g; i7 z /* ROTATE_LEFT rotates x left n bits.
*/
& ?0 C. b. e4 U$ R#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 o2 H/ {" z1 U2 Z3 X+ Q2 F' ?
$ M2 u/ G$ {2 F8 |( W, U/ p/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& r3 p4 F5 S" ~Rotation is separate from addition to prevent recomputation.
*/
- m' ` [8 Q$ ] #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' d) `: |( _6 S8 r1 o#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ \! f4 n9 d+ U, z" a#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 |' z3 i+ ], F+ Q#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( u; F" g6 @3 n8 C6 v
0 @0 m$ a& n8 b, [4 j5 [ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
7 O% s; M6 T. C' |: K p. Svoid MD5Init (context)
4 U( u( h- \" v+ e" wMD5_CTX *context; /* context */
% G3 d3 n4 i4 C3 g9 A{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, _7 p) w3 S O/ M, m# u0 m2 @: A) D*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
5 a7 K$ d/ D3 O2 y6 `- F}
7 ]. C7 K( [. g2 Z. b
3 S- ?* T) c" D9 O* R, p' \4 _ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
! U% {3 ~6 \0 p" rvoid MD5Update (context, input, inputLen)
4 ]- N0 R" N7 r$ \3 Z8 o8 WMD5_CTX *context; /* context */
4 k" R% c% a* }) t unsigned char *input; /* input block */
; G1 h. Y' m% A* O+ L0 Y, B7 V1 k5 u unsigned int inputLen; /* length of input block */
+ \0 g/ n0 h4 {, }# X% \{
unsigned int i, index, partLen;
) d0 c- X* i C( h5 E
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
2 h: S) q: X( C* A
/* 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++程序


- E* U5 l2 r7 E! m1 e, O4 Z" U5 l! @
) I. g& t7 A( ~* v1 Z
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-1 15:25 , Processed in 0.352266 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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