中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' N9 s2 s7 u7 `1 V+ U6 y/ u( S #include "md5.h"
1 F5 F' Z0 a5 U4 ^4 V( c
k& h. ]5 l$ ~" y, M7 I+ \ /* Constants for MD5Transform routine.
*/
) v, v+ b n0 v
) ]5 ?! x. t, N; y2 y3 _
4 K8 r0 W2 k% K6 [/ L$ F& l #define S11 7
" q- ?7 W+ d& [0 k- n0 ?# y: s#define S12 12
" o R! @+ z1 Z1 n1 t( N f$ m #define S13 17
" {/ j. w; d6 |. T$ y #define S14 22
- Z5 d/ a- Z/ \& ~2 | #define S21 5
: R# h% \$ |! t2 B5 N2 K# e' [ #define S22 9
5 x$ d7 [! [& a" r: X- V#define S23 14
; b- N" K, }, K) U #define S24 20
6 Q! v+ W2 a9 J# N#define S31 4
5 K! p8 @+ M$ C7 U# V$ W #define S32 11
% A1 k0 y( o3 S. }2 a: u( R #define S33 16
) T. _- M& ?2 p#define S34 23
, e+ Y$ \% q# z e& {" ~$ ` #define S41 6
6 D" N( h2 Z9 }2 g7 V7 t#define S42 10
7 K9 ?; B7 d8 ~. d. ^* G/ W) `: Y#define S43 15
9 S/ j @8 n$ |! `" d$ Q$ g, T#define S44 21
( m: [8 a' a3 U5 P: W) @
/ F, M+ }4 _ P; ?$ {' |static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 v4 j, R; S: o) Z5 S7 ^# cstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
: u( H6 p2 c- F% Nstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" ]& o! B; {5 U! F6 nstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
. q2 m2 o, r/ U2 b8 g$ xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 W0 r7 s7 O5 \3 X% l/ h; K- @
' F P# T1 L3 a# kstatic 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 K8 }' [6 m3 ]3 b. s};
: u/ {, v, ~& X& }( k; q
0 i8 j: Z& k- J /* F, G, H and I are basic MD5 functions.
*/
" [: g3 Y! @7 M9 f. A6 c" k#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" ~+ j+ z% S! ?2 V0 L; r, o#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
. A S- }3 b3 w x/ U1 N- ^#define H(x, y, z) ((x) ^ (y) ^ (z))
1 `1 N( E8 A* d* k+ { #define I(x, y, z) ((y) ^ ((x) | (~z)))
! s. S S- |3 N6 u
& G4 b3 J6 G' \8 l! `0 T! z) [/* ROTATE_LEFT rotates x left n bits.
*/
9 w4 ^, t" \9 L, i' g #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7 D7 W9 A7 e8 m# N6 l& X
; L" q9 |; |3 \# G+ [) Q$ F/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
) U9 } M3 T N) {* o9 d Rotation is separate from addition to prevent recomputation.
*/
& B8 B) ^1 i3 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); \
}
& n; R- a1 A7 X; D2 ^#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 r0 `5 H$ D/ ^' R) z5 B- @( I; 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); \
}
( g' V) x$ P2 a# b0 J2 G #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' S- i3 e( n1 L
; G+ i" p5 v0 a3 E; F; U+ g /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: t! B; r% o" f5 w9 F+ p void MD5Init (context)
( Z, z* l* z2 Q6 P7 g3 C$ G MD5_CTX *context; /* context */
8 F" l8 w) g m7 D* I5 I6 Z" ?{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
7 `) D2 {6 L4 ^8 x/ O/ W! m */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# O2 w/ V8 q5 ~0 _; o7 p- z}
$ @% w' r5 v# F: ]4 l% n
' S; D$ h0 b* ^# \7 f8 o /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# M p; T3 y. f& }* vvoid MD5Update (context, input, inputLen)
/ j$ [, p. |4 Y' a2 oMD5_CTX *context; /* context */
( |- b7 Q0 k5 n; Zunsigned char *input; /* input block */
8 t% S' I/ g& a unsigned int inputLen; /* length of input block */
- x. }0 h5 G6 b& z{
unsigned int i, index, partLen;
" |% R7 |5 ?/ j
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
" ~& l( T2 [2 ~0 X% [! n7 a% [
/* 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++程序


$ w* l) v9 z3 o$ [
8 w: ` ?8 h9 K% w/ v3 `- S+ c( ~
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-22 15:43 , Processed in 0.055574 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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