中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
- v$ l' C2 p# R2 ]) B# C6 d* x5 r#include "md5.h"
2 G8 D: ?/ ?# l4 n9 Y0 h
" c+ O! O d; A; D. q: B7 ?( c" d0 L% E /* Constants for MD5Transform routine.
*/
# G, M$ P4 t* M& B! B
, F. u8 r. W. M( U5 I
" l4 x: H4 s, l4 j4 Z% \#define S11 7
, U8 J; o; X2 T0 m( P' b- x# n#define S12 12
# k4 J6 Q: b p- c0 x- A6 V6 L+ h$ ? #define S13 17
9 |( q* K8 @6 \. Z# x #define S14 22
8 f6 F9 ?- p1 b7 T) k" K$ ^+ |#define S21 5
- m" @. G/ F1 Y9 s' ?/ ] #define S22 9
b; W! S+ o) [ w5 ^/ Z; {: g#define S23 14
) E9 I. h5 c: A* O5 s5 N# Z#define S24 20
* B4 S9 n( P! p# ~: U* c m #define S31 4
. Z, k. N* v( a: i #define S32 11
- v% d% _; T9 @; W& B* I#define S33 16
* Q2 h) {* \; @#define S34 23
2 E ~! {2 v! J+ b$ j4 o #define S41 6
/ A. ]1 C3 Q3 ~2 P) |+ {#define S42 10
9 c4 E/ ?* b% B; B#define S43 15
" ?3 L0 I0 S6 E# J #define S44 21
' @* B: T" r* Y, r
2 H4 V7 @: }' ]; B9 } static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
; W$ g" G! @! w: } static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- c: D3 ]2 Z7 jstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
0 B( |2 a7 l F3 i7 n static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" o% e. c1 r& P) n; M- dstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
" C$ z+ m% c/ w1 ~/ E( J
8 t% l: y/ X) P/ K9 _ 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
: N5 u% }9 t3 G0 Q};
8 N' \% Y6 b3 t2 y
9 S& l8 d4 e5 D /* F, G, H and I are basic MD5 functions.
*/
% u; ?" o# o- u#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! H) i) V' A4 @; A- T& _. {3 W #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
3 |6 y! G' f5 a% L8 x#define H(x, y, z) ((x) ^ (y) ^ (z))
; p! }9 ^$ _4 z6 o4 D #define I(x, y, z) ((y) ^ ((x) | (~z)))
- k7 ?* v9 [& p9 R D8 @ I
* z3 m8 L3 Z, |& e6 Z /* ROTATE_LEFT rotates x left n bits.
*/
: F ?5 b) d# h. p% i) d# b #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 c! t) [- D4 v- ~
. I; `' s# }# B. b9 U/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
X' m5 h& {" |( u; x3 M, pRotation is separate from addition to prevent recomputation.
*/
! d/ X( X$ e1 I" w! {7 U. Y2 Y#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 f/ J- a3 j5 C7 I" V/ _1 _% E#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! g* b* O( b/ w) ?" D! ^+ L( X! _#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 n. o2 k" L/ S#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 A1 _ `5 e2 b; v
) U( M7 g7 O: o1 e" v0 L) k5 A* y/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
9 O; {# ~8 T6 l9 jvoid MD5Init (context)
/ K3 ?' f0 r8 |# d* V* k" ? MD5_CTX *context; /* context */
6 i# N6 I5 p8 `) Z5 s+ m" V% I6 W; d{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
8 K; n5 H3 w; B% M* r */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 x) P5 g1 }/ k, r}
+ {" g: I# g+ t) t
* w' Y6 f X4 ?: p/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. v* [7 {3 Q- u( o/ K$ P: kvoid MD5Update (context, input, inputLen)
% r3 v! q2 m' H) _ MD5_CTX *context; /* context */
! ~) q5 ~; A$ N8 `unsigned char *input; /* input block */
( }5 Q9 x. d$ s* f8 ?7 q unsigned int inputLen; /* length of input block */
2 A4 x7 y1 N* ~) u' a{
unsigned int i, index, partLen;
' J b/ B! j. s! |0 l" P! @
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
) Q" q8 k3 T7 ?
/* 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++程序


_0 N9 G3 t2 a4 l, |- K9 c
( Z6 v4 l9 f! z8 G6 Y# Z! y
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-29 06:09 , Processed in 0.066564 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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