中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
a! A& x. g$ |: T* u, w3 m #include "md5.h"
# f f% |/ \. G( ]5 \5 x+ g
g/ z3 K2 _6 {. E& j /* Constants for MD5Transform routine.
*/
; X2 ~1 K/ x4 U$ z' H# I
# h: O7 L# b5 y7 v! {; b
6 J; W# ?; |$ R0 X1 I7 a#define S11 7
4 c. N+ S6 ?% Q# i5 [3 K #define S12 12
, }, a( y* Z! Z+ _+ c7 x #define S13 17
0 h3 c3 D( L! Z! A" n#define S14 22
* q. B; b/ W9 B1 @#define S21 5
# T; q0 |5 B8 F #define S22 9
5 t2 x0 ?+ Q" d; F0 J8 ^5 Z0 i #define S23 14
% S, P2 @. o# B. n #define S24 20
# W" ?" F3 ]( e' t& M0 E( s #define S31 4
" X" O) H6 s8 s& ]4 L#define S32 11
: z" L( V) S& u% T. {; f#define S33 16
; o- L: U" s7 d# c7 Z& j#define S34 23
3 Z6 l6 ^' Q) I4 ~# r% j #define S41 6
& f/ d9 [! h: ^! O6 ~6 n3 t# n#define S42 10
9 b" k' X& _. k#define S43 15
7 C$ O4 B( E: t#define S44 21
i0 v4 K( b5 I* k6 u
: j9 D8 w# }3 i% N( P7 [3 F2 mstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
3 w+ f! t( V! W- J3 Mstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
p6 M, T) N# d3 w static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. F* I' n# n* q8 T static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& _, ]# M, S4 N x1 v6 b4 cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
5 [% m: k' B7 B8 m6 ?% q! Y
( G2 A! M4 A' X1 y5 [1 W! Pstatic 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
' k& @) W6 c$ t8 B};
7 d& z% v( w$ a/ ~* H# U: P9 G
% g/ X* |* j# \, @: O/* F, G, H and I are basic MD5 functions.
*/
/ y& ^! w! {3 k! L. Q4 D #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# h% p, X- e) N0 r#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" i! ^# Q! D4 w7 R#define H(x, y, z) ((x) ^ (y) ^ (z))
, K, u8 ?$ V9 q( k5 Q* J#define I(x, y, z) ((y) ^ ((x) | (~z)))
1 e. r F5 j2 g
/ ~7 z$ M: {9 m8 J /* ROTATE_LEFT rotates x left n bits.
*/
2 `3 p/ Q* G q9 v#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% N9 B' o( {; U8 q/ B
) w8 k2 w& m" t: T6 s/ H3 D /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ `4 C. t/ W+ `% g6 L' w9 PRotation is separate from addition to prevent recomputation.
*/
6 |) k0 c* W9 ~. O3 D* 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); \
}
3 h$ A+ j" H, {' W; b #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% l7 I4 U2 X9 Q0 A$ ? #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 n- x6 t7 F9 d/ Y#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# T( s, F8 N/ Y5 n, `0 G; q
4 L3 t% A9 F5 k0 h% Q: _$ }/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
* l K n4 T+ E! M1 Y void MD5Init (context)
s5 K: a# m( ?- H- XMD5_CTX *context; /* context */
& b+ k8 r/ p0 ^: Z- J3 S$ {{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
[2 c4 x3 A( \9 p" c/ _1 \ T*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
- i3 }3 D/ \+ h+ F" g m: [ }
% M4 q0 K7 l3 d! U) u. }
* Y; B, L6 P9 A: n. P" I# _! B- F/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
$ f' b* c8 l1 U1 ?1 l: @void MD5Update (context, input, inputLen)
2 ?4 l% M; t4 B& ]/ j( d+ t# ? MD5_CTX *context; /* context */
+ W& z7 R e0 A8 d3 ? unsigned char *input; /* input block */
4 F6 G/ e$ b- xunsigned int inputLen; /* length of input block */
% u$ o5 o/ T( }! c0 g' z: y: b4 Y6 B {
unsigned int i, index, partLen;
2 P' B8 s( d5 g5 q$ V2 h
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
# j) c; {( g; ]6 d1 \( H1 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++程序


/ ^& w/ K: [* Y5 y+ J! f0 B+ [
0 k$ l# _/ |: J# U: _
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-20 06:46 , Processed in 0.057480 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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