中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ p( M; S# G+ ]6 h3 ?) r, F #include "md5.h"
+ q9 v) [% R2 ~3 i6 j0 S. z
+ a; s0 j3 Z6 C# t. K0 L3 T' a5 V /* Constants for MD5Transform routine.
*/
5 R/ x( i2 O" e+ ?2 t3 D
2 D5 ?/ D& }4 v, Q4 L- b
. L v5 T5 i2 z+ J" a #define S11 7
# V6 [" a# G# |8 Y#define S12 12
8 \, Q2 L0 \( Q8 z#define S13 17
' m: E2 m1 D- S$ z* d #define S14 22
6 t: } ]9 F6 q$ | #define S21 5
0 @: F3 ~5 s/ C: d3 O$ d #define S22 9
$ s* r! G# ]# Z, ]0 l& H4 l#define S23 14
+ S# L9 X; t6 i3 @3 N; r* g #define S24 20
. X* f/ p+ T# T4 }7 r5 q4 E #define S31 4
- |$ M+ S! C- i; H#define S32 11
& o) A: _$ o4 s+ j/ L g% f. K( T#define S33 16
" N0 J5 s) X& o: j* B #define S34 23
! z1 F" n, r! \2 l; Y! g5 V5 m, y#define S41 6
" c4 w- ]3 y8 _3 W, B0 D#define S42 10
5 _/ I! d9 x, Z5 w. ~0 p #define S43 15
1 T& n1 R3 r& A5 ~. J6 a" }+ K#define S44 21
$ t& A- L# e1 j( J" R
& h) Y5 A) V H- V+ P static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
" r2 Q& s8 O+ E2 _- Lstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
1 a3 @% T7 n2 F0 v8 xstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 ~5 z: s) x/ v- r: M$ E5 ustatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 C0 c: h7 o5 r0 K8 q; D) |3 fstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, G M7 e, n+ u6 b; x6 [2 |% e
1 b2 A5 c$ | f+ `. X6 Z; C, M 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
3 R# e' ?5 M, @9 J0 P* K$ ~};
2 W9 L4 `! L l8 k" h
Q, f7 ^1 x4 d/* F, G, H and I are basic MD5 functions.
*/
: K" A0 {5 z# o( b" Z#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
' k$ f c2 L$ V' h8 n #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
. M, J) ~; \/ ? #define H(x, y, z) ((x) ^ (y) ^ (z))
& l" ]- k$ ~( n#define I(x, y, z) ((y) ^ ((x) | (~z)))
0 b% G: g% _! I, X( @, S
' `6 d& z- [/ e" D7 p% k/* ROTATE_LEFT rotates x left n bits.
*/
+ {6 {0 y5 C7 R+ l& o# @' w #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
6 b0 H8 Z7 }" o9 M' c1 Z
' }! r. A+ @% `9 G /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 n) U8 I3 y! M/ q3 b Rotation is separate from addition to prevent recomputation.
*/
. W! E$ w5 b3 o) \0 ~9 F #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 |1 w4 r# u, `/ Z#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 e0 |2 R9 N! ^ i' 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); \
}
: @+ ~" q4 x- C4 ^#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; K2 r( g, Y' R3 ~% }5 @
, d& Q6 d& g% v5 `6 |; e8 A+ T ~/ ~ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
0 L+ A: N* G7 G, X( [8 m* M" d; [( wvoid MD5Init (context)
/ E' i! I8 C5 Y% }MD5_CTX *context; /* context */
& L: ^4 J0 g B: J1 Q {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, h9 v. r) m" q */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
8 i( y9 J' F. o- z: \- e+ R) l }
" P: |/ N: G) p: N4 o1 g4 v5 r
& m! z& _3 M+ H% K/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
6 ]4 I8 T2 [% E. l* { B+ W: C void MD5Update (context, input, inputLen)
0 r) ^( N" l: @0 nMD5_CTX *context; /* context */
+ @- D1 k4 t! I' j, @* O1 a7 U* [* Gunsigned char *input; /* input block */
! t, T9 b2 S1 S- Z; i# P5 ~% E7 d& j unsigned int inputLen; /* length of input block */
, ]) x) E( N# h* N7 u) z{
unsigned int i, index, partLen;
7 |0 Y6 X0 i4 K5 D6 B% m6 j5 V/ E
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, r1 C" X; m4 R
/* 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++程序


+ ?. a, G1 U* d+ J. y
1 X7 Z9 x. k: M1 y, l( V, [2 u
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-6 15:34 , Processed in 0.059143 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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