中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
( n2 [$ N5 Q' z/ j; ]5 \ #include "md5.h"
: n0 i/ P7 @3 u& k, d* U
m/ ~. }* {# d) ]$ M* y /* Constants for MD5Transform routine.
*/
]: [, P: W+ {- p
; S4 h1 m& h' n3 O" T
( F( [+ G/ `9 s3 k2 V6 o8 g) f. k% @#define S11 7
. r& q3 q2 X# q. M% u#define S12 12
1 H6 s! j5 J9 c8 {* q* e( L; T#define S13 17
/ N) t! c2 }* z4 w& S1 z+ J1 w W #define S14 22
" `/ M/ |6 U5 G6 o$ [: ^; G#define S21 5
& q8 [0 _+ G. ~' c3 V+ p#define S22 9
% t) t, o: |' V6 F: [- x8 @. E0 w #define S23 14
# |* M1 D! d% O, N/ f8 N- ]#define S24 20
3 {9 F3 H) | n#define S31 4
4 v6 _0 i/ _; v6 s& } #define S32 11
6 B! n4 g z2 j#define S33 16
9 k s2 h' U' v3 A #define S34 23
, ?+ B. I& J& i: m6 a7 L- @% _/ O: w#define S41 6
4 S6 B+ s/ V) [/ M% T/ t#define S42 10
! M3 X4 m6 N% k( N5 T" H: Z5 p9 {#define S43 15
: \ s9 z! S8 b8 G8 a1 Q#define S44 21
* s# j- k1 y% Y1 T4 |8 i: P
' v" q3 t, R, [, u) H' l static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
0 n! P, Q' G+ {8 S% }; R0 n( Q9 r, n* ~8 M static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
- B4 m- N/ g6 \$ q) C! ] H; m3 q static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 z4 n2 x" a# Q5 E6 N% I$ Z! G static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
) |1 z x7 }; \6 ?8 X& T static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
6 B4 C/ k$ d4 h) `$ i% ]
3 C1 x& q* b+ X0 P6 g2 T, u 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
8 n6 w e- B2 K. }: s};
# A7 M3 N, T( ?. z. P* s' j$ u% J4 v
. ^9 j. b9 }: N/* F, G, H and I are basic MD5 functions.
*/
6 m' e- h' @. S$ l#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+ n1 e4 u9 F6 o1 f, R/ D4 [' e#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: n4 T. N7 L& L5 v( ?6 e5 F#define H(x, y, z) ((x) ^ (y) ^ (z))
7 J" u' _/ ~3 D) `" h3 Q# a #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 R4 L0 M6 Y4 B& Z5 m+ |
0 k/ l/ j6 V7 ]; [; {; t/* ROTATE_LEFT rotates x left n bits.
*/
4 C7 P8 _+ s* N0 ]& v4 d" _4 O! {#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
$ ?% M0 |; C- r" K$ g; ^3 ~
. G# |' x* \9 Q% a; \7 J; t /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- e9 O, K' ?8 `$ b4 {+ Z* B Rotation is separate from addition to prevent recomputation.
*/
^% q( \) Q& |7 W #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! h; Z, @; E0 q3 H0 x4 V/ z" Y& j #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ w. I- S4 E7 _$ R#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; @+ K7 \3 [. z #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) U, b8 e& Y$ }
, u+ B$ s1 p( a# O1 g3 E3 ]; \ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, W: b) W/ O( [. ]* K# ] void MD5Init (context)
# m$ K' y/ }" b1 [! FMD5_CTX *context; /* context */
7 O# y6 j$ W) D. |8 t2 G+ G( h{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
/ f% M m; D. ]/ E */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
a5 t- H, `. E# f- ~}
& C1 Z+ {7 Y2 I# ?& ]/ C# T; A
+ n* _4 Z/ b0 D9 d/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, D" k0 n1 I1 _5 j9 _ void MD5Update (context, input, inputLen)
2 H2 o) W; S, O) {7 f" Z% l; Q MD5_CTX *context; /* context */
% h6 g/ M7 F0 K9 u O! w# K; A unsigned char *input; /* input block */
- j* s$ {6 i; J unsigned int inputLen; /* length of input block */
7 J. j6 }! R5 O& ^1 ` F. Y{
unsigned int i, index, partLen;
; [ `' X( y% ?- v9 U2 L
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, J5 @/ m. n/ @/ j/ f7 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++程序


/ S. b, c/ e. W% p+ ?' n
% g8 ^1 D h4 v7 h# r
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-15 11:13 , Processed in 0.060516 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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