中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) I" n( K$ I1 P2 N% ~) `# l! `#include "md5.h"
8 z4 N9 Z5 \: s3 F
9 h) D" c0 o& N/ R8 V/* Constants for MD5Transform routine.
*/
; O2 x7 K5 m! ~! O: z3 F: d
& U9 p6 y! t% L! U* |
& L+ V# }& ]; M, ^0 a K$ L#define S11 7
- c2 x, U$ L- Z4 O8 a4 s #define S12 12
/ X1 Z4 x: f7 \; ~#define S13 17
8 X+ o! c* @# h! f6 D+ J #define S14 22
" F8 {- @6 {) N+ B& I#define S21 5
' n$ y9 p8 e" ?) C9 \ #define S22 9
% Y/ _ ? {8 f* k! o" B$ _% I#define S23 14
+ c9 e8 B# u2 P# f0 {#define S24 20
6 S8 b$ @& H8 T' C9 w" H+ |5 K3 z #define S31 4
) j" N* s3 B# C! ]$ [7 l' a #define S32 11
- f3 L2 o5 J) q' z6 S #define S33 16
- G( H5 u1 ^, G( v, ~. p #define S34 23
& Z! \% \' x/ p5 U#define S41 6
6 ^' K1 s& T u9 ?4 E #define S42 10
% R2 ~) |% U4 K" O #define S43 15
" c' v* k- S0 U: \. j6 T" Q6 y/ H #define S44 21
& F% o# V7 X+ o$ L1 n8 G
% z! C9 C5 ?; J% @! wstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
3 [0 N w+ O6 N' @. E! hstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ E( I" p4 t, {) I& O4 o: o- w: K; Xstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
# w# C# u3 `& \! |static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& S) A1 H7 m. h h: j* @$ Estatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
" S) D3 F H7 j- X& X
: w6 z' D$ I1 k* M3 E6 C* N9 B: g. L 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
# G2 [0 O- W, ^ F0 O( M};
) b% W' U3 m7 A$ w. I
; s( c. o( Z+ t /* F, G, H and I are basic MD5 functions.
*/
5 y7 q1 L) _1 U4 }2 Z3 u1 M9 e6 h #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 }3 U' @: O4 Z6 r, p9 d#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; Z2 L1 F% m: S) B#define H(x, y, z) ((x) ^ (y) ^ (z))
* H; M2 W8 w, J& k2 j% Z7 \: Y#define I(x, y, z) ((y) ^ ((x) | (~z)))
0 n/ m1 j7 C+ o( F% @
8 @$ R2 @3 h' J) `& l- W/* ROTATE_LEFT rotates x left n bits.
*/
0 D: t% \0 @( D6 m% z #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/ O4 p7 M9 l9 T% }2 j
3 K x" y' L: B5 u /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
# x( U# K; ?/ h" |/ M Rotation is separate from addition to prevent recomputation.
*/
, d; e) {' y4 A1 m3 n- n/ s6 e #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, T( R, }7 h! g4 |# e #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 X1 T4 d$ f$ d4 ?5 O3 Z- H E @ #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% c* F; O. `* h) 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); \
}
6 o, w, u6 _% `/ A
' v: {9 K$ A$ F /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
% Q& V, u4 c% zvoid MD5Init (context)
1 y1 y, G# [6 J# M3 e' P MD5_CTX *context; /* context */
2 H% F+ G+ @; ` {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 B8 {) f2 s1 S: ]& b7 r4 \ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
4 c; y& P2 t& {6 J( V7 @/ Z, {: G }
4 Z9 W1 T m0 t5 X5 p
( {- g* \9 t0 u& P G2 i+ r8 T, R/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; w3 G" I# D; z5 U1 ~void MD5Update (context, input, inputLen)
3 d4 B$ O. F) O9 D: q) JMD5_CTX *context; /* context */
# f( C+ l# @8 C4 S2 w( h# ] unsigned char *input; /* input block */
- Y: z2 l% G4 B n1 d F6 a4 q I1 Hunsigned int inputLen; /* length of input block */
9 d: }' j. I$ I8 V/ E{
unsigned int i, index, partLen;
% h# o) z4 E: V9 M7 Z7 L' j ]
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% O0 |" f7 _' m+ \" H _
/* 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++程序


5 ~6 E& _" A0 @
$ \# E/ l' d& L* ^: V2 i3 E
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-24 06:28 , Processed in 0.081671 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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