中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 J- z( l$ N: b1 X s# B; z' ^ #include "md5.h"
1 \& T: x. _# K( k0 \' T3 j
! R$ ]/ {2 S) Z6 h7 h/* Constants for MD5Transform routine.
*/
9 v) W; L# m; {) }6 c) L
1 J- h0 F- i$ X* t
+ t, k9 p6 O& d4 F; x#define S11 7
- y) R* ?3 H' F7 L. i$ h% a0 _ #define S12 12
: S. N& k4 b1 E+ K5 I& I #define S13 17
; I! z5 g( J. F- {: f; o5 q8 B #define S14 22
, I# x2 s. W+ w/ R#define S21 5
, ^; G* S ?3 V2 ]8 ^ #define S22 9
; o9 u+ F3 G" s' q e4 k #define S23 14
' X7 j2 k% X" V2 c' }. K #define S24 20
7 J2 X5 W5 P; h( B#define S31 4
4 R4 P3 m/ U8 F6 n; O#define S32 11
$ H$ ] J6 e% ]+ p! G #define S33 16
$ L+ e+ q0 D, c$ j #define S34 23
$ s, k4 Q! P: h. R/ I( k6 e #define S41 6
4 }. E5 F) f: w, |, ?& m, [ #define S42 10
6 V& U% d; s M: L* k0 x#define S43 15
: u( U1 m: f6 @ #define S44 21
Y$ `2 h) H3 b0 m# S7 ?6 ~! T
, s( w/ s8 w4 T static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- e3 ]! l! R7 k0 C6 n static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 y1 R" _ x U3 W; ]. Mstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, ^- F. _& A4 w' I) C- `; tstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 ?) b% m! S! _$ Qstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 ?$ F, ~' {: X4 L- X$ O2 h5 b
+ k- ]* l3 G) j 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
1 g8 l, ]5 \1 X- I# M6 b };
# N& \8 \* I# X6 H! S5 {- N
2 x1 ^! ~" G$ W; O /* F, G, H and I are basic MD5 functions.
*/
8 v8 b! u. }- L& {5 G5 I0 S#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; K0 {9 p" A# V* d' B #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; f- Q: c, i, B2 z#define H(x, y, z) ((x) ^ (y) ^ (z))
1 A1 r x9 I! \, C- t/ t #define I(x, y, z) ((y) ^ ((x) | (~z)))
$ p2 b8 I$ F7 H1 _; ~9 ]
) {! \6 @. k I0 Q) {% B- `& d9 r/* ROTATE_LEFT rotates x left n bits.
*/
4 X5 M F! O' V) V: Y* ] #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
1 A- {. b& G, g- f
! X; p3 k& c+ U( |% h/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: T" o" h: G5 O, YRotation is separate from addition to prevent recomputation.
*/
, ~: ~2 {6 O4 r6 `7 N; ~! y8 G#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 X2 B0 j. _, n4 U#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ a/ o3 Q7 @$ A! h#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( r8 y4 t! w% s- ~( G#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' Y! g' ?% N! w+ d& I
. F2 t. ]' _2 O3 y2 `9 Z' N/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, S9 \ ^4 o, B. o void MD5Init (context)
. [7 `1 f6 E( H! o/ [( iMD5_CTX *context; /* context */
+ g% F4 E$ |7 L! m d3 ^ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 b. c6 k# f* Q, z3 K+ J*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
9 x5 T( u. a _( E3 |( E8 W9 L& y }
$ H9 Q2 W+ E, v0 h& k5 _
& N8 V% \; z7 ]% [% H/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 a7 n4 H! K* Y1 d7 ^- f void MD5Update (context, input, inputLen)
# T K$ q& N% {. W$ h2 d. ]" AMD5_CTX *context; /* context */
, t1 k3 Y) q- ]- o) I unsigned char *input; /* input block */
# u$ |1 q0 ?/ _2 v9 G& G5 I3 `3 \7 C$ F unsigned int inputLen; /* length of input block */
3 E3 @) H. I, k{
unsigned int i, index, partLen;
4 I- n4 O0 o4 V" d/ F* W
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
G7 x; B5 a5 f0 E* J0 O* S
/* 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++程序


6 ]% S! O$ D/ p. K W
# G+ B$ `/ C( R( y0 p N
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-25 09:09 , Processed in 0.060796 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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