中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& ?! i/ I& a; h- D( j' E) Y" W0 ^+ \$ j#include "md5.h"
* z& }8 X' ?( T! S0 l% A
3 u0 s9 o& f9 M. V7 H. Q /* Constants for MD5Transform routine.
*/
j* v3 k+ D! k
, \( G+ Q4 l; l6 V- }( W
; h: l% x) ]: G% S- a #define S11 7
2 E) n8 ^" E5 [0 h#define S12 12
" ]: Y, ?! Z8 w* j, j7 D7 { #define S13 17
- W7 l( d: x0 G7 H k& r#define S14 22
~; o0 A$ f5 K- K# I6 G4 o #define S21 5
; K" O$ ^4 d9 F #define S22 9
/ }7 X& S) j- K4 n- J7 O8 H #define S23 14
' A- @7 a8 [5 f #define S24 20
. Q( A+ W- ^6 [1 m$ s$ I #define S31 4
: H! w2 Y. ^0 L9 \) E1 ~, S #define S32 11
- w s5 m) U) n' s#define S33 16
$ E' f2 }, ]/ h4 ~& J; T0 F- Q, ]#define S34 23
2 x* P: A6 d1 Z# k1 O6 U) n4 l. y#define S41 6
- a4 U4 l7 f# c+ p+ m. g! B+ ` #define S42 10
2 E: c1 v) [, g+ {3 {) F) }8 T #define S43 15
* `% J1 O1 P5 W J1 P3 J$ D #define S44 21
: B5 {, m7 a4 n
! |! E* _3 \7 p( l. F static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
b4 b+ Z% x4 u- X0 J+ h static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 w1 |) ~) T: E& S+ f) x& v static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 X5 }; Q0 s7 q: T+ w8 X4 p static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 a5 C5 c9 {3 K4 f- tstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 U: Q: R' P. S! ~
) U4 P3 F G: D' F 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
2 C* N3 ^: U8 x% ?) [ };
6 T5 b5 w5 f- M0 w0 V1 C
# y& s, j5 p2 I" H; k7 h/* F, G, H and I are basic MD5 functions.
*/
$ g* u8 Q# |/ q( O: |! A0 K #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% |6 ~1 @1 h. X* ]& g- m #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% l' B$ P o4 \! s2 }#define H(x, y, z) ((x) ^ (y) ^ (z))
$ R g& i5 V5 f1 Q2 X5 E #define I(x, y, z) ((y) ^ ((x) | (~z)))
% c9 Y* t5 [1 r% B
3 ^0 a) T' T- T+ h, L/* ROTATE_LEFT rotates x left n bits.
*/
2 `8 b0 C% u- S* b+ @1 k& N; e#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. h `+ j8 o1 L1 X7 E5 D
, J' c/ ?8 e* _4 T, Y /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; a% `. I* s; m& G) H$ M Rotation is separate from addition to prevent recomputation.
*/
3 X% N* I0 Q! x& n# N) O; 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); \
}
( ~* c; j% T4 @, V H% g! R5 F#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- T h" f: p/ N#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. r/ w! ^/ q5 V5 K* m, Q* ? #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) y6 T; X; |7 U/ \+ ^6 w3 X
, f9 ~2 e1 p# H; ]/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
]8 O# l5 L: q void MD5Init (context)
) u. }" f+ d8 `/ ~4 I( rMD5_CTX *context; /* context */
2 a% A2 l$ u3 A: U{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, H6 g" _$ r# H6 ?. B0 N*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) r, s( ]( K& V2 X1 Z}
7 T* {4 g1 Z( C& c& U# o
$ p# t$ u Z7 j/ q( o' q$ x/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* t& n8 Q p2 P8 s; B. } c void MD5Update (context, input, inputLen)
) C. u6 t) \$ H# ]5 MMD5_CTX *context; /* context */
9 R u: }0 R" a$ N7 z7 P9 uunsigned char *input; /* input block */
; ^+ v# d. t+ [- V4 R7 E1 N, `) X unsigned int inputLen; /* length of input block */
- @% g2 L2 r' @( U9 L {
unsigned int i, index, partLen;
# U) P) | |% M" S* \3 U* t2 m
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
* j, ~: U6 [6 c) @) K2 V7 e
/* 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++程序


/ r& b. U$ ~1 \7 e: x5 c+ j# L
6 P" B+ R, Z' d0 d0 K
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-29 05:50 , Processed in 0.061224 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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