中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
8 W" s/ a7 r: p c+ e v#include "md5.h"
2 r2 c6 Q* L9 e
" `, y' V4 E' H$ G; Q/ p9 q6 L" z2 M/* Constants for MD5Transform routine.
*/
% s7 h! |7 A* J3 C/ | Q
; ^; R/ t: s4 k
( t$ k" e u+ E* e' Z#define S11 7
6 C. Y. s& t9 G8 v #define S12 12
2 Y$ g B5 {8 c9 Q& }#define S13 17
, p% ?) W# |1 F( p #define S14 22
. j& R% c: x, e- h4 h1 A1 L, ?#define S21 5
/ \! u6 _8 e( |2 N2 m8 T#define S22 9
5 x. O, b' E9 V& `( G' h* T3 F #define S23 14
" \2 O8 @; |6 a: S% h1 H' r #define S24 20
v/ P. i+ _3 I8 p#define S31 4
) |, w. J/ m- j$ Z, q; N #define S32 11
* [& y8 X5 v5 G8 V* f3 r. G#define S33 16
; `9 \ Q9 i+ b7 R f( n- `#define S34 23
( D' A8 g/ E3 R& D1 B#define S41 6
" C* J5 X( l* S5 r#define S42 10
* a! P* R$ Y/ j" Q& J3 Z$ x0 a #define S43 15
0 F7 l! q7 U& G7 O0 o5 g #define S44 21
- R$ W1 I1 T/ o' m+ c% ^4 i2 o1 s
- w) F1 T) O7 z1 C9 ^! v static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 `" |0 L8 U1 V, Y" q0 P& ~! ?% i static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; {7 Y8 i0 N' B7 `! W; vstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! D H# \5 s- n+ m, A' V! [ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! j; [9 O+ Z" `' i, gstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! P D( f' i% u% L. H+ B; ~) K& r" @
3 y8 G0 {5 u. w3 \" Z 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
6 ]6 b O& X5 ` };
& h6 D4 j% g9 M8 |2 D9 L
1 h- H; B( W- `% R- m, t3 @6 U7 U /* F, G, H and I are basic MD5 functions.
*/
9 {& y( ~# l" L+ ^" _$ Y* I1 w#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
) s/ S. x; i' h/ I9 @6 z #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! `+ F: V$ E' h, x. `# p3 K #define H(x, y, z) ((x) ^ (y) ^ (z))
0 t& A2 L- l* W( ]#define I(x, y, z) ((y) ^ ((x) | (~z)))
9 b6 M) S* j% l
; D: I" i0 H% p t: K: g /* ROTATE_LEFT rotates x left n bits.
*/
2 j2 M6 D. ]. p! d#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) L2 J% `2 J3 t8 J Y. b
' ^% I8 d- T2 S' ^/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
# p/ H$ r6 m1 K Rotation is separate from addition to prevent recomputation.
*/
/ L) L4 x- L. L' ^ #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) {. B/ l6 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); \
}
; T1 E& t8 ?/ r4 s7 n; h- Q. {#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
f+ j- k: r$ D8 }1 R2 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); \
}
% h" b' S* G( w c4 E' j
t- j2 s! x% b, z- y" O8 \% V; t/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
! D: I8 ^- x+ b6 P9 p) @void MD5Init (context)
& v9 G1 ~6 Q O8 V4 G ]MD5_CTX *context; /* context */
4 y3 ?6 |3 E; l0 N{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
; @" Q5 D* n% R4 N, d$ n) p*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! I F( a2 M9 r5 Z# _5 o2 {& a}
E5 a& h9 D# m) I1 z, Z8 Y9 f% ]
+ [& P1 e/ Y+ o$ G1 l5 N# H, z6 [ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. j9 R9 y# h2 m* Fvoid MD5Update (context, input, inputLen)
3 h3 J$ k% ]" w* @- i MD5_CTX *context; /* context */
" E" @0 C# T( ~9 G; j |- U; ~ unsigned char *input; /* input block */
! I% a6 x8 I$ Q4 X3 x unsigned int inputLen; /* length of input block */
, _1 \) D" R& |# Y. F {
unsigned int i, index, partLen;
- S3 } Z: t* V7 z2 O7 Y" [; c) o
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
' t" X# C {& n' z9 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++程序


8 R3 ~& l$ N' ]" N3 W8 [! L7 x
3 x o& ?2 P/ V f9 y( g8 v0 E! {
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-5 21:02 , Processed in 0.056844 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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