中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
6 J$ j3 A% C3 W5 f" {, E#include "md5.h"
' T! W7 j- n. C! x. D2 F
& W7 G" H4 s& P0 n9 V: E /* Constants for MD5Transform routine.
*/
0 L3 `/ e/ V/ f, T P* b& f
& P! U$ B# j H" b6 p8 h! `6 r$ u; c
: |# k6 z3 W _* v4 X0 V% m #define S11 7
% B3 f" _' s2 S+ ~' x: p z#define S12 12
9 K# `! O% }7 Q/ d' O7 Y8 u#define S13 17
) w+ b3 X1 T | k* r #define S14 22
7 B( ~2 n) w+ F2 C4 @* m- U1 K#define S21 5
9 V, ?2 F* S" _- F& p0 }/ O# ^' a7 `#define S22 9
! s1 E: U" J) \) ^/ c3 J/ T" e#define S23 14
3 `4 l$ j2 [0 B #define S24 20
3 G. |# L( C4 {- p0 e( G0 G0 k #define S31 4
7 w' U4 q8 \$ y p( T8 f#define S32 11
- n1 S# u+ h# m( b: g) }. n! d #define S33 16
4 k) F# [5 P" X- a #define S34 23
9 c. R% r1 m3 ?1 n6 `2 x! m4 l#define S41 6
6 `- y$ i2 F* E/ Y# V8 u2 x$ @+ V#define S42 10
. R+ {* g! {/ B$ q V#define S43 15
* G4 G1 F$ G! T #define S44 21
$ h2 q5 s% c2 k9 C0 ?( B6 F7 d
3 K) v" m7 C% _0 L8 N# G static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 W& T+ f" o& I8 H. V static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 l8 v) ?) s7 M$ Y0 T static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ D4 F3 U2 j+ G7 q static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, A( J" E- W# e2 d/ Astatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
9 Q9 Y+ L: J! c/ j" U k6 R0 k
# r% M8 ~# d1 x6 ~ 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
0 s' N5 v0 _0 {* p };
. q# d7 \) B6 i' A" V1 t
r" w8 \8 [& M# h2 A' f /* F, G, H and I are basic MD5 functions.
*/
" E$ B( @3 x9 s0 R/ a) s& z #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" V+ {0 T/ v' N1 |2 A #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 A) E+ U7 Q1 B6 x0 g+ P- D" r#define H(x, y, z) ((x) ^ (y) ^ (z))
7 n/ G1 b. k5 T- i* Z/ d) A8 r#define I(x, y, z) ((y) ^ ((x) | (~z)))
% Y% A+ j$ u" W V5 \
" r' e6 r- D: R# O /* ROTATE_LEFT rotates x left n bits.
*/
2 N! W K, G, N4 ?1 F% j#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) h! y* z+ Z# S1 a
0 B5 _* c* C& k/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
% |$ E9 k$ ^; t( s7 VRotation is separate from addition to prevent recomputation.
*/
. ]6 ]& _4 o$ S2 R K% J" 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); \
}
& t" T4 F2 O$ Q0 Y/ D+ R6 ? #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 h) I" U' W) i* u8 f* Q#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( V' u& q8 G: @( e. 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); \
}
: x/ b% X$ q3 O5 p
$ O3 s, h8 q- h- K/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" J7 S, p( U% x& u" ?6 F& B& Ovoid MD5Init (context)
" l5 e" Y8 b: F! I4 _& b! t MD5_CTX *context; /* context */
/ y7 N4 }& B, O {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
8 r2 P2 k1 j5 _0 M */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
: z% o9 s- {8 d+ R }
6 |7 o+ h c h7 `5 M2 O% |. B
* A6 _' T/ J# `/ J /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
) _5 O; t7 r. Q* p; Q9 Q) G void MD5Update (context, input, inputLen)
+ p- V# Z4 v# m MD5_CTX *context; /* context */
6 B- o3 o+ s* U' D+ D/ eunsigned char *input; /* input block */
/ M6 `: m) b+ b" q5 u) `% \ unsigned int inputLen; /* length of input block */
" Q8 @4 r% o/ [$ n8 Z{
unsigned int i, index, partLen;
" C4 p. X( _- T. p* ^; ~5 v& l
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
# }. H `$ Q% v4 q; f
/* 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++程序


" y8 x# D1 S2 W5 W$ }
, g5 m$ O7 n$ @, q6 D' A0 t
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-9 19:26 , Processed in 0.058622 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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