中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
: M% v. E- D: |2 U1 o5 f#include "md5.h"
" V }5 O" H4 |. C0 z4 I
' h2 T$ N% e" k+ U' H& [- P0 U# l3 H/* Constants for MD5Transform routine.
*/
4 N- ]9 q' d% [, U, Q
2 L/ U/ ]0 g, a. F. U
7 a! v: [* X% T. `7 Y) ^ #define S11 7
& E- p6 T' k: m. g$ _#define S12 12
0 S% [- S5 `# r V; M. e, _#define S13 17
. U: P k9 J7 f/ b#define S14 22
; O- V# S" Z3 W3 O! o- L#define S21 5
. F. \8 y0 g8 }8 \7 b5 c#define S22 9
: v% X2 }+ h7 q1 W# x( b( s# w #define S23 14
" U4 ]8 ?% R9 K) O! Z+ l#define S24 20
) \' @) t# s# D) ^) M#define S31 4
+ t) h. q( C% o" I( P. X, X* J#define S32 11
. p* F" v7 c8 `$ W: C! U7 F#define S33 16
1 W$ G0 ~( ^* `. }7 y #define S34 23
. z9 R: t& @) Y( O" R) I1 f #define S41 6
; ~( U+ }' A' t$ `) _#define S42 10
$ X9 G( I" D; I, U #define S43 15
1 `1 N5 s' e; G; d #define S44 21
1 J1 L6 [2 @2 `- i
6 x# D9 ~3 U, `; Z static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 z, ~, t2 @+ }static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
P6 u5 r( b4 H2 y. H* B' H+ w# Y; U static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, s3 t: W$ C1 [1 w, h static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 f7 o8 K, b: Z static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
7 g3 B' e6 A) m+ x5 C3 w8 N9 ?" g
! B; V, y: B& k- R& U- Ostatic 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
5 V8 X" k3 Y* s- |};
+ n, `# ]- ]) `! y, O) ~' T
4 W; E0 M1 \7 l+ d- ?% f5 k/* F, G, H and I are basic MD5 functions.
*/
9 {# X9 v' t3 _, K#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 N+ {( n" y; a#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
' R9 Z4 P& W: s0 {. A8 y3 x C& z#define H(x, y, z) ((x) ^ (y) ^ (z))
/ n' x' f8 g0 ?/ Y7 O' w#define I(x, y, z) ((y) ^ ((x) | (~z)))
% L* D# R3 W! i, m5 f. \
( |! g8 d1 ~* ~$ E" e9 w/* ROTATE_LEFT rotates x left n bits.
*/
2 R y2 V) C& I2 @#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+ \6 J' y5 A' L* C p2 t; M
( V5 U2 U3 `6 [! ?/ y7 _# P /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
/ m: r5 r/ o0 ~+ ^: Q4 i Rotation is separate from addition to prevent recomputation.
*/
5 C4 Z, }3 ~+ [# v8 e/ e; P1 g #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 L8 i2 w& A: a! o #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 U9 i) n, P+ O+ C #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, g/ q( T/ H1 f* G+ N#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% @' I" d# o6 _$ D
g1 E4 h& b0 o+ c. R8 H /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) ~& q/ f* w- _7 J1 pvoid MD5Init (context)
& y/ M/ G7 t+ J9 r6 Y7 x MD5_CTX *context; /* context */
G+ s" n5 w2 V3 L- N5 i5 D X {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 I5 Y( o3 X4 `*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
% p( r& J- |2 I6 W7 \ }
3 a" s2 u5 [$ ~8 x Y( [3 y( T1 M
+ T2 e1 w d5 H3 c7 L' `$ K6 p/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 ~! x% R: \4 s- Q6 b! U& r( E5 wvoid MD5Update (context, input, inputLen)
" k! ]( w. G9 `, H) c MD5_CTX *context; /* context */
4 w9 o3 a$ D7 B2 S7 i f0 r6 K: z# z unsigned char *input; /* input block */
3 T) V1 f" P2 ` unsigned int inputLen; /* length of input block */
: s7 j( q/ @$ W! l; u# b6 l. x7 E {
unsigned int i, index, partLen;
0 |! l6 O! a# c3 N& G J: p
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% m; f) e/ f y, x; `' `8 u2 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++程序


+ `0 _, R. @+ Z- d( }
7 [$ a( _5 m/ {1 ]. ? y/ O
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-19 11:02 , Processed in 0.209580 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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