中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
% {( w! p, D$ q! k9 ^( \#include "md5.h"
! Z Q' ]+ k3 c% B- U/ t) x, @. L
5 r) W3 {+ ^9 ^# j5 N /* Constants for MD5Transform routine.
*/
2 p0 ]2 q" L7 U# }% @( ]7 b. w
$ D, e8 Z/ e7 [8 v! r3 }
- \! b* S2 Z, M: }$ E#define S11 7
) w4 k! q% ?6 A#define S12 12
( m, c- v. Y+ ~7 N* ~% `#define S13 17
/ r7 w( o* }, l7 _9 m0 o( R#define S14 22
8 p2 V' \" w5 k+ Q#define S21 5
+ p5 Y9 f2 s: b1 j0 |! x #define S22 9
5 b- f- r/ b" ^/ S" z" ~, g#define S23 14
; T9 P6 q& U( D( Y4 y$ [$ O#define S24 20
" H% K5 O K! }- I$ R#define S31 4
# r( r; K5 d" p5 q x% l #define S32 11
+ B. I0 o6 o2 |4 K3 K #define S33 16
* i$ \* k1 j7 M#define S34 23
% N" y/ f! L5 {. T$ J: ], u #define S41 6
) e# A1 E" U; R- N( i #define S42 10
0 ^$ o0 j+ s/ _! h #define S43 15
# u' F+ `2 l4 Q; j4 _$ y #define S44 21
/ Y9 D# V& d$ B' e9 J8 @. k
4 U5 f% ^, Z4 n: |5 w2 ~1 ostatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 s$ m0 w0 I2 dstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& _+ R7 R s) g5 f6 vstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, y, Z) Z2 n# W4 W5 Q( K9 w- n istatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* `; _; [* _4 X static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) |6 h$ ~2 z9 c7 j8 F$ E
8 d4 s+ H$ r& M2 b2 U4 b: x H 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
, D0 K/ z4 G6 P' h0 j5 p9 [ };
" j8 |) V. m: ?1 I1 k
3 z( T; F1 D/ ]6 K/* F, G, H and I are basic MD5 functions.
*/
+ m2 S5 q G, v7 r#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ y. g# i: q6 s) s$ W; I J#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; b6 a' m; G: m8 L2 O#define H(x, y, z) ((x) ^ (y) ^ (z))
9 Y, R# u. L7 A- I# e #define I(x, y, z) ((y) ^ ((x) | (~z)))
) b9 q M; Z7 ?' ~9 u J
# {! Z% F5 t4 O; [, \+ @/* ROTATE_LEFT rotates x left n bits.
*/
& i( Q. X* {6 l9 g#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" F, Z4 {6 @) a8 a: g6 ~/ S
5 p7 w' N4 b# {' g8 k9 m /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ Y3 b) J9 g" k% Y7 A3 E- D Rotation is separate from addition to prevent recomputation.
*/
$ m* U6 J0 f) q* ^#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) v* N7 X8 h; f# K \ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# t- V7 G$ @/ @7 Q/ W2 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); \
}
0 {- A3 p6 G' J+ X( U2 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); \
}
* I( W, D; `; l
' `0 b0 P- I# s# a /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
+ m( J/ s4 \" d0 A4 W& evoid MD5Init (context)
7 C6 \4 z5 b" C2 M; g* lMD5_CTX *context; /* context */
( F) E8 N0 b# j* o: ^ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& N, L! f5 W' Z8 Y8 d8 T */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( t2 d% D% _1 _9 v' @2 D- |}
" `5 ^; ^% i0 L# y q G5 W' A
) t0 v `! r5 O# k2 K8 G/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. X3 Y* H+ X% \# Zvoid MD5Update (context, input, inputLen)
/ |9 H: V* K" |% b9 V0 Y MD5_CTX *context; /* context */
' M5 q& e. W% f( G7 A. k7 y unsigned char *input; /* input block */
+ w4 [& a) H5 [( Z8 c8 F# `; V unsigned int inputLen; /* length of input block */
# H9 `/ |. O9 A{
unsigned int i, index, partLen;
& y) @- u8 p5 G. i" Q, E: P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
9 L6 S- N1 i- u' S/ G: k
/* 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++程序


$ d; |0 {+ W! U3 a
" s: ~- K0 W" c$ S6 r0 M( ?
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-9 18:02 , Processed in 0.057795 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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