中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) ~* f$ m: u3 J+ x#include "md5.h"
: b5 N2 g _2 F! ~/ M
8 K* B- g2 z6 A- X; b9 c/* Constants for MD5Transform routine.
*/
) s% V- X" g2 I& k9 i
, u7 B& q, I: E& {1 c( f6 A2 D7 k+ v
1 K& L3 v, u6 p7 g#define S11 7
0 ]+ C& H5 v3 ~7 N" G) g' I#define S12 12
5 X- S {; I0 l3 j #define S13 17
# t/ s% J% p r #define S14 22
- C6 t6 _2 P6 b5 X3 j( I1 W& | #define S21 5
6 y h: B3 a- i8 T2 X #define S22 9
8 Z8 l ?* V5 \#define S23 14
6 B2 ^6 N; j. ]8 x# W5 m& }4 \#define S24 20
, Z- m. Q9 B1 Q# j+ O$ k#define S31 4
8 U. }9 N- `* M1 o7 h#define S32 11
5 K2 z$ C( }4 H$ D4 u #define S33 16
3 K K. I; e7 J3 T: \#define S34 23
& L) x2 e) A% v% d3 E7 t#define S41 6
9 i( j+ g+ j& U) L#define S42 10
% E6 m: e. {2 {$ K- _6 o' l5 B #define S43 15
8 |) r1 J4 ^; m! P2 h #define S44 21
( e3 z+ C" _% J7 e4 T. s
2 p1 N, ^0 S% Y& ^, `; w- Z static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 e: t% l5 m0 R1 [* n, S static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
n5 o: I2 f4 t7 x& } static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# W+ e' e p" @& B( h5 Tstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
- C+ G* G; d1 m( K" G5 S% Q# s static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) @) i; i* c) X* f+ b3 u
( W) ]) s0 j" z1 `2 O6 ]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 c# P9 T" c5 I+ Z- S };
+ ?$ ~+ g& |& J8 T4 ]6 X, d7 s- Y
! z! ^5 i: P) P# M /* F, G, H and I are basic MD5 functions.
*/
6 ?2 a' l- c3 v2 j6 f& g: M; F#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
0 Y9 ]; X% o& P7 s- O" }0 h #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& }9 A% B7 C2 `4 J* a#define H(x, y, z) ((x) ^ (y) ^ (z))
& n7 i" H! T! [0 w* B#define I(x, y, z) ((y) ^ ((x) | (~z)))
6 R; U# U' n& V# ^) ]+ A
. @( {9 G1 O4 S r( R/* ROTATE_LEFT rotates x left n bits.
*/
, T/ {: v. ^4 J: j/ B#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" ~5 E8 @+ e0 Y4 Q
# j8 d" R8 D l2 t3 } /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
3 u1 b* Q/ ~' Y2 b* N( Y Rotation is separate from addition to prevent recomputation.
*/
9 O" `& Q/ b* \3 w: n6 S #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& U- ] `/ E! M* i, y0 W#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 G: e9 x5 d2 @- \' z#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* z8 t1 _; U% X+ } b #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* {9 @% E3 V N( C- [! ~
- A) I2 k/ }* M/ C /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. Q% ?2 e! i9 k9 |; N {void MD5Init (context)
+ N& w. ?8 [4 X6 F8 j8 _& F MD5_CTX *context; /* context */
# d+ E. p3 S+ j& C, z' p- C{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& s7 w8 M2 c/ }7 j, j */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
8 @' ]5 Y. s* X, p+ z}
& N# x0 k2 Q4 U0 t5 f/ I
0 {" z8 _+ @ H' z /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ _! N- [2 r& p. |! P: d void MD5Update (context, input, inputLen)
/ H) t( _ d7 }# w MD5_CTX *context; /* context */
( O7 s; }2 u5 Y: Funsigned char *input; /* input block */
. \1 j% C; k$ G+ K: t! F+ b3 Munsigned int inputLen; /* length of input block */
9 i0 g% g9 `$ s. {+ x( h{
unsigned int i, index, partLen;
% G& n) [( h- j! o. H# {( g% H
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
) U1 x2 ]1 E7 v* H% N' H2 l+ 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++程序


/ h, Z5 W6 q$ J& ^
; b* F: Q+ I1 q) p, f8 I9 w. ?
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-4 09:25 , Processed in 0.080817 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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