中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! B' m2 f8 V5 ^$ P" j3 d* r1 w q7 Y#include "md5.h"
! Y$ Z5 o& ?+ |
/ n# F* I- ~8 d1 M& V4 s( `1 a/* Constants for MD5Transform routine.
*/
8 N7 W$ ?( O% t9 e6 p
" I8 J6 a. A ~! R+ U( b6 s# U
# z% q7 ?8 X% o' q& Z#define S11 7
$ @3 z' }" X& `1 L#define S12 12
2 S% @/ k$ x0 A#define S13 17
X! D% ?; A- g! P4 u#define S14 22
+ a* a0 c( p( @2 U0 M( E( k #define S21 5
" Y! Z! m& i; t- Q' \6 I3 X#define S22 9
3 p( g! c" V# [. m" Q3 u3 Q#define S23 14
6 E3 X+ X- N5 J, R$ O% L) S #define S24 20
# Y; I% [2 z! u6 [ #define S31 4
# V- [( t q- o! U- s2 ] #define S32 11
/ p& ]9 b, Y5 K/ A" c" `' y#define S33 16
. m7 S) c0 ]/ O1 V #define S34 23
; B+ T& \. G- l #define S41 6
, N a0 c0 K: [* ]6 x! v$ T #define S42 10
4 I1 F( ?: E1 J7 n' @( S. W#define S43 15
' y, g) k6 W& y9 J* O' f#define S44 21
' l" c- m/ K' T' Z) o; Y* Y
) L/ o/ d6 U& s/ b% w) T estatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ W7 q9 e# Q: m/ c6 ]static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ S/ n+ X& H& F5 a _/ Q3 a static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
p) A: E4 H2 a+ i8 o7 A7 E static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 i& h* W8 V' l2 N5 ]# W static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# @/ e$ m: t9 B+ l
0 i, S$ q, s5 U7 ~; y5 S% U5 I; zstatic 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$ `* s( F" P; ?};
& `5 E0 A; n, V' s- ~
; g( ]! k1 W: `4 I1 _/* F, G, H and I are basic MD5 functions.
*/
+ f6 _* e- P5 K$ q4 C/ c% E% H#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! P) F' `7 X: E#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% k1 y* W1 q. L f. v#define H(x, y, z) ((x) ^ (y) ^ (z))
- ~: a, F$ ^6 { G#define I(x, y, z) ((y) ^ ((x) | (~z)))
- |. p( }- V% s; d6 L
) H7 n* K" ~# @7 u/* ROTATE_LEFT rotates x left n bits.
*/
, W/ c( W# F) h3 j" v/ f7 d. g#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- a, n* K7 C2 V; W9 [6 C8 F
5 o) p) Z- l( l/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
0 P$ B/ t$ q, ~) n9 H( e: tRotation is separate from addition to prevent recomputation.
*/
/ T* J# ]% 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); \
}
8 a# u/ E$ D- U+ b/ 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); \
}
2 \2 a1 G* S4 I& Y8 x6 [' 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); \
}
% J$ K. ^$ \* S0 \. e, G8 S0 { #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 v0 h' c0 o8 n! w1 x7 d0 @
0 z2 n; c- i% l" z5 K /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- {2 C2 Z/ c3 x( O void MD5Init (context)
: V' ]0 @/ ?" [3 [+ [MD5_CTX *context; /* context */
4 _& e/ t! a' ?& x, [ M {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
x- e( n, P5 |3 z" Q*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
, C8 Z, G- P: `- w}
$ \* R) q9 k: F# \5 P+ D; O
& E) @5 _0 a5 A, x) u/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
) {( ]/ p2 f: l4 x( z# \void MD5Update (context, input, inputLen)
! G! _5 p& O$ w% U5 X" [- a8 r- U& k MD5_CTX *context; /* context */
2 E9 C& h9 X3 |# a; X unsigned char *input; /* input block */
9 V# k8 s6 \2 X1 ]( Z/ y' I# f/ A. [unsigned int inputLen; /* length of input block */
7 f4 ^0 x$ q1 d$ U {
unsigned int i, index, partLen;
7 c) N. i7 c' |9 ~& B; O- f
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
3 ~; T2 |2 @( ~6 C: u3 ?- 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++程序


! i- X3 D2 q V7 F9 z$ V
1 V! _7 p' w, ?( `. U! F& H5 ?
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 05:58 , Processed in 0.056489 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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