中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
/ S) q4 D u+ f* W$ Y/ W2 p#include "md5.h"
/ ^3 g! K! r9 Z, z+ @
. P; ]. w: T) ^8 l+ x" E7 q /* Constants for MD5Transform routine.
*/
( F% x+ {0 U. O8 J/ |5 U
8 |5 f. ^/ d0 \: h
1 a( |3 j# T2 b L; l- ~+ q% } #define S11 7
% d7 G) S" P, v; ?2 n #define S12 12
# h( p7 n+ L- ?5 a/ w#define S13 17
. w, q1 s+ Z |! @* ? #define S14 22
: Q; h' Q# x3 B5 ` #define S21 5
5 n4 _% y: u# u5 M- V9 F #define S22 9
s- M) y6 v3 W #define S23 14
. I& y4 F% `% h; `4 P* b2 n #define S24 20
2 S2 V8 I9 s! r #define S31 4
' w! J* F# z: p5 y. |" D4 U#define S32 11
+ `9 _$ K: {* P* e9 ]. q#define S33 16
: ?6 M* \% Y. K# C1 v0 {& Y #define S34 23
) z/ N5 t$ ^4 k' i X #define S41 6
" T* J1 ?3 s1 E! [9 y% U M3 C #define S42 10
; u$ j4 d. Z( Q" \2 I/ W#define S43 15
4 n! j" U ^; j2 G#define S44 21
& O/ b/ ?- A/ ~) M4 ~7 V
9 v6 T# |3 Y+ P) S( Cstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 H% c8 w0 f) Q( i+ G7 h static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) S8 c8 V4 j M* k9 h; {( Z, astatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. q9 E2 }4 X2 M7 B, r2 R static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; f/ F" ?" s3 _! {6 m static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' n( J7 h/ Q# S# Q% A
3 R1 S1 H+ R$ [ 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
! k! g. t% j' D& c& i+ Y};
: _- C9 P/ X5 K
4 ?! S1 P' l! d: V, }/* F, G, H and I are basic MD5 functions.
*/
/ C9 Z: R) h. ~! I8 H/ X; V #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ K7 A' ~* l% o# d#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 c* C) Q. a% k3 T, k& c1 a9 l" l#define H(x, y, z) ((x) ^ (y) ^ (z))
' ~9 v$ {) l/ f4 V #define I(x, y, z) ((y) ^ ((x) | (~z)))
1 v6 }: A7 r# X6 E1 W, Y8 [, ]" \$ ?
' M. D% R0 k0 \; _0 m2 j# u$ F" Z0 [ /* ROTATE_LEFT rotates x left n bits.
*/
8 L8 F6 d) H- B$ u0 a8 Q- m#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) b. Q* H7 b8 v3 q; z& ~1 b6 r
: X) x! E# T7 r# m! ~1 z/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* `3 P2 Q' `; @3 ? G3 K Rotation is separate from addition to prevent recomputation.
*/
! f8 }( p2 m) @2 U#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) Q. O3 W4 K4 C7 w. \( Z0 h* 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); \
}
5 i$ g& N2 `( K0 d7 u3 F' h: g #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 E: }( E+ c' @. W#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( U1 v! w* h2 h7 x, ]/ w
$ C9 \8 \5 t p6 C/ m, T /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
4 I* N' v8 R) d+ h# ]0 Y# h+ v) Z# v void MD5Init (context)
+ k8 F( \9 X1 c( F& N& b4 EMD5_CTX *context; /* context */
' y: S' J9 s4 G6 L{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 @0 T8 F! b& h+ w0 i) p*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
. d3 q G5 F( n8 o$ p8 ~9 J! u }
: l6 _- Y5 B' W% I
1 K S8 o, e, A, }: D/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 `% L4 R! M, X7 Z& _5 e5 Gvoid MD5Update (context, input, inputLen)
' w8 T! g) `- Z: v MD5_CTX *context; /* context */
1 m+ \5 w! G! b unsigned char *input; /* input block */
* ]/ W7 A& |: e. kunsigned int inputLen; /* length of input block */
" j- d" G: e" j) s# w* E{
unsigned int i, index, partLen;
: e7 F# H$ O( \6 {
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
) h B3 g( B. y6 d$ i$ k
/* 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: t" y: I1 y% k2 n
# E& d" q+ b( ~, P! S# I8 \ m( N
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-3 05:54 , Processed in 0.069400 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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