中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& t6 N$ G0 w, s( B+ z. g2 ?3 l* C #include "md5.h"
5 ^+ [" ?' p. s
{2 T' J# p2 ^+ p/* Constants for MD5Transform routine.
*/
5 [( X5 z$ \% ]8 v# e- @- k3 q' o* I5 t
8 @! W6 |; _; z3 B: \2 r
) S: j& f! e0 ]0 o; ^$ z9 W #define S11 7
" R( a, j* j! e0 e4 p% [ #define S12 12
. s2 [5 H- S7 ^/ w #define S13 17
- r9 G( V' D7 |0 H& j #define S14 22
. h: K# L, p( R& n #define S21 5
$ e9 B- }) t' y0 k6 l$ a% d; O$ g#define S22 9
7 f. c' {& H! h0 l/ ]# Q& D#define S23 14
" H$ q; m. J7 q9 y$ V#define S24 20
- r0 S4 d6 G9 I% Q" Z0 C #define S31 4
# v) \8 w4 ]+ E7 H3 z" D #define S32 11
) B T" C A; z* }! e4 Z, H, H$ ^ #define S33 16
7 _: J* l/ N4 ^4 e#define S34 23
* s8 w" c: f: g0 n; r [ #define S41 6
! A! H6 d+ K. L) G" E6 M2 I#define S42 10
2 t5 h5 r, b/ d. X* ? #define S43 15
: c* @: Y) Z/ [2 C1 c8 H0 @ #define S44 21
# x7 i- L* w( [ m
# F# Z; s' ~. z6 Q) e+ y, R! ~static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 x: e+ Z' R- x% Vstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% d, p7 _& w9 A static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
% R6 D' O" N+ V, K; I" M7 ?! ^ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
6 }! Y- f/ ?+ Ostatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! n# N) v( X2 ~' E7 u$ V4 ?
- I4 ~( V+ ?6 m 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
4 [' d6 `# |2 g; V- _% { Y };
$ y' O% Q1 o% \( k+ w& P
# e( k0 v; c7 v( X9 A# E4 R/* F, G, H and I are basic MD5 functions.
*/
0 q: s& V$ q2 n#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 Z2 r" y( X2 R2 o #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, r) a; a$ s6 y; @, v. p#define H(x, y, z) ((x) ^ (y) ^ (z))
1 N* @0 x+ l4 M& V( G#define I(x, y, z) ((y) ^ ((x) | (~z)))
! l4 ^ R. H+ s5 \) Q0 g5 V* V
8 o3 H: W. \6 q( E7 {/* ROTATE_LEFT rotates x left n bits.
*/
9 n/ L. s5 u6 V5 X! @#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) b7 [' c+ g# S( }# ]7 u
1 z) p! }* O" r /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 j- T3 C3 ~( r- K, t$ R+ R, d Rotation is separate from addition to prevent recomputation.
*/
' R% N- u9 k9 u n #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 I$ M) G6 ], i1 G( c8 d) z: `#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ v0 l# U" C8 @" E2 f( h0 T" k #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& i1 G. A& N9 x7 z1 z1 j #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 i; t0 t+ G1 s" q+ S1 s7 p
" O4 l0 {# I0 v. {) x0 Z /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) O; H, B8 F9 E8 K }3 ]0 }$ N; u- Svoid MD5Init (context)
: }0 t8 p8 x& l1 ^ @MD5_CTX *context; /* context */
0 P3 v: Z/ \9 T/ { {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
! j5 P9 T/ v a) x, U3 R*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 g! X. H" V+ \0 L0 a, M0 s }
' y. ^* Z1 a1 X& s6 \( q
4 I) E& ~/ V, a, R( [% r* e* } /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
+ c- C& s' h' O3 W* s5 ?% Evoid MD5Update (context, input, inputLen)
. V, b- X& G# b2 D" }+ R MD5_CTX *context; /* context */
. v/ n* j! ~# [! q( I! Munsigned char *input; /* input block */
" w+ ]/ u- b' e; \unsigned int inputLen; /* length of input block */
, h% ]/ p0 Z8 z8 w- V {
unsigned int i, index, partLen;
& V" `, C: b7 k; K0 a$ d
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 ]7 z3 d* V2 ^/ j" K3 W
/* 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++程序


, {8 G* Q, l6 T9 M+ C1 N, I4 U
: r: J. @, \ N4 r1 P4 q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-3 17:58 , Processed in 0.078320 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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