中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 P7 F7 L$ @, U$ M7 [ #include "md5.h"
3 [. P4 P9 m6 l1 ^
9 D3 e4 V6 e7 i) T( ~3 _8 A1 M# ^/* Constants for MD5Transform routine.
*/
8 @1 F6 H: j0 l. r, I( m, h
0 j( {8 g$ z8 d& i( v4 n. C
6 \: B3 T' i$ A) ^4 @ c0 I! |; a #define S11 7
" ?4 n; E+ ]7 l- V: l& U2 {#define S12 12
! ?+ y: N, y8 D8 d1 U* R#define S13 17
) s: B& R8 O7 C" f#define S14 22
; \+ {: z; k6 E+ U- \#define S21 5
4 e, d' \& }/ p1 G; A+ z& }: U #define S22 9
6 q# l' c# x& |* b #define S23 14
: K2 R3 P( _9 b* i$ L0 t5 j. ?) I#define S24 20
, c% {- _$ U* L' r7 O#define S31 4
0 A7 I% Q, _4 |3 H#define S32 11
3 p {/ g( I m6 E6 ` #define S33 16
8 }# l" C, l8 D, d u: d( P#define S34 23
3 K" a/ _& j% `0 [ V! W#define S41 6
$ T, y; w' Q, X- s, @/ i#define S42 10
: ?' |: j' t$ h9 O#define S43 15
3 w$ W/ U; n4 E) Z# I' ~3 C #define S44 21
2 f r- R' |4 @2 T5 _) t+ @
/ u& j( {0 L* S static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
* j& S; M, U0 l- e: i static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ f5 X) G- G5 d# e6 ]- b1 E static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 p) ]4 B* g" J: ~static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 }% E* Z5 N/ i+ xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
9 q7 z ~& @7 Y5 G# b6 {: S7 n
/ A' T% b& X1 gstatic 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
7 c$ P0 X6 D r };
! n# S4 l* X7 b; l
/ r; P7 s- C9 J" B3 d /* F, G, H and I are basic MD5 functions.
*/
) N2 f6 [; W0 n1 D4 D, i5 t5 R#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% A" n. \/ c3 Q) A0 s#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
* V6 ]! Q0 b% v% Y9 J #define H(x, y, z) ((x) ^ (y) ^ (z))
5 l5 M9 X" p( I0 _#define I(x, y, z) ((y) ^ ((x) | (~z)))
( S0 t5 q) p+ {
( I5 L/ A( N0 a7 U# @6 [: D/* ROTATE_LEFT rotates x left n bits.
*/
! o; M, f/ Z" r$ m #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
$ Q; A3 A6 m R* b- w" q. Q6 @, r4 w
* r9 E2 f$ `! j, K2 `% @/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
) e- U5 m7 Z3 P8 z4 w/ R Rotation is separate from addition to prevent recomputation.
*/
& o1 M3 \, V) l, l- b#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 E$ z+ P( H n' n+ j; Q' ` #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 b: I' O! Y9 D8 E#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) A' F$ p" ?% H6 A9 C q/ @+ i #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 k+ x4 ~0 k0 q; M( R, u- j
+ O6 W: q9 R3 Q4 }9 ^( X# W /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 O6 O+ u# h. ?4 Jvoid MD5Init (context)
& B) f0 }; E' I2 a MD5_CTX *context; /* context */
( h# ]7 l- Z4 ^% C5 x5 a+ t% a* n3 ?{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 h3 \# F, b7 c1 O! C */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
% R; G s- `8 C; m- V: ?}
3 F# a+ Q0 \1 z2 \ j# }
3 v) f" V2 K8 E: I6 y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 k3 o8 ^0 R2 `) j, [ void MD5Update (context, input, inputLen)
$ A' y$ D* Q6 |' i" | MD5_CTX *context; /* context */
/ d- S2 R3 B5 c) G# { a# c unsigned char *input; /* input block */
5 o6 Q# q \+ p unsigned int inputLen; /* length of input block */
; [1 z0 M% {9 P1 d& K {
unsigned int i, index, partLen;
/ ~; G' [- k; ?! @. e1 Z& _4 j
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% T: t- @, {- r. p7 f9 G- O2 b
/* 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++程序


/ _% Y" w, A/ b# n1 L
$ r3 d9 _3 C: M7 j. _, N. s
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-13 15:47 , Processed in 0.125448 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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