中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; N N3 Y; y; A# F, ^, A #include "md5.h"
) U& L8 B; H1 c7 o A, i
- |" F4 v; l a- { /* Constants for MD5Transform routine.
*/
7 _5 K" E) m4 @6 b: u3 p! o
: C1 t( T _9 m- ^4 l/ D8 W' r+ [
9 F. N" ]8 D, D' L% ^#define S11 7
9 o: C: u% M( V0 w7 l- s8 b6 ^ #define S12 12
; X% A3 t; ?' O; a#define S13 17
; h8 `& H; T; X' { #define S14 22
! e6 @/ W, _! y, P2 U4 e#define S21 5
4 \& A v" w$ y$ R& e) s#define S22 9
6 `" Y6 i' c1 l2 B& M( I* g #define S23 14
9 }9 W+ F3 F# y, l0 P- V4 T#define S24 20
0 D# l; I1 g' j#define S31 4
6 t6 a( Q$ c+ ^#define S32 11
1 Y4 I j3 D5 j7 u#define S33 16
* u2 N. A9 h, ~, e% Q #define S34 23
% J9 Q1 F. F) {: y; R6 K#define S41 6
& e) a' ^ {2 w; ^4 r/ y #define S42 10
5 r6 U5 D: c5 z$ M% s% U #define S43 15
! ]. a9 f* |8 T$ T' E#define S44 21
( K! M! u |; G" l8 u7 I" v- l. x
3 i& Z( |8 p7 L& l+ {: Hstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ A5 `8 K& R1 j6 S R" G+ o static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
* B: A# e h# ]$ _static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# \: k3 V0 Q- \" y& Y- E& q$ fstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
' e! u- R; y( w& g- d static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
$ g$ O3 k7 K- |
. y. v& m) G0 A6 }) k$ nstatic 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 ^0 ^# |4 }" L};
) b6 k; C6 ?$ K7 [+ h) {
$ `7 b+ s" i N' L% Z( x/* F, G, H and I are basic MD5 functions.
*/
% c3 C4 {3 t. G$ H E#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& r* s1 H" {% s3 c& k2 G#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
4 H4 H5 H( o0 @5 R4 n4 e #define H(x, y, z) ((x) ^ (y) ^ (z))
V8 e( Z9 `2 N! Y" g) F) j/ J #define I(x, y, z) ((y) ^ ((x) | (~z)))
% \9 H3 |3 P' X; N s; I( C# `5 [
5 Q, C- F; g9 K" u# Q$ q) m/* ROTATE_LEFT rotates x left n bits.
*/
& h: F2 V0 e5 C- P4 G2 f! { #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! \% y0 H0 G' N8 P% F+ f
" ]1 B X. ]( l& b/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1 U5 m9 E. D$ m [! h; o+ oRotation is separate from addition to prevent recomputation.
*/
) b R3 u0 a6 k# y7 h#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: L; v+ n/ ^* a* o- s#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& Y% U9 i$ }9 W: I #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* P9 Z$ C& t: m) [! ]* 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); \
}
" m# u2 h2 j+ Q r
6 H W* p- d) _4 c /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. M# E* k* y3 V& u% n- J void MD5Init (context)
p' \. C' [/ W6 g+ { MD5_CTX *context; /* context */
2 v+ M0 L4 L: C: x$ H{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
5 E/ P5 `) t& Q \" P. d/ T */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
4 O1 I, j" \/ `9 d* g4 s- s& F }
) J' G- e1 ]: H& e, R& b
9 @; q- H% K7 E/ M& G% U/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
2 G! Q$ z' {# u4 B% P' ivoid MD5Update (context, input, inputLen)
9 D: T) S* Q8 R* L# E+ e1 e/ o: DMD5_CTX *context; /* context */
+ z2 [% u9 e- c; t+ eunsigned char *input; /* input block */
% t; W+ x; _- vunsigned int inputLen; /* length of input block */
8 w/ Y1 A% H4 i/ M; _ _{
unsigned int i, index, partLen;
: x5 C0 \1 T! E5 \$ g7 w
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
* q- a% ?! j) B; m/ {
/* 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++程序


b( c3 ?; {2 `, ?" Q; D+ L
) D; k: s$ ^2 U: t' Y- T. q/ A
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-13 01:27 , Processed in 0.059519 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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