中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
0 w5 q# u+ u. `& p #include "md5.h"
6 m- [$ J% K0 r$ {2 a* z7 w
C* m7 i! \) _, C3 Q! ~ _/* Constants for MD5Transform routine.
*/
( K7 L' J: j' B' J
0 p4 K; D# m* B/ z" v0 H( T
: D$ {+ X6 @# O0 z#define S11 7
/ v5 m }$ E! V' H V #define S12 12
9 K; ^' m4 H9 c. }4 x6 L #define S13 17
$ l4 U+ j6 O3 r z#define S14 22
; m" H" H. R' o4 p #define S21 5
' O" P. i3 B* E5 w; N #define S22 9
5 T1 [. F% C& Z% ~7 e# `* A) M #define S23 14
& A$ y3 P! r! K- V2 o4 [0 p#define S24 20
B5 [5 {- r B0 r; K' A& i; ~#define S31 4
( c* i+ A: ?% z: v/ k, b' b#define S32 11
U1 U1 L5 _8 G" w f$ ?5 |7 f#define S33 16
' s/ w; e/ Y# p#define S34 23
7 {1 X( K* F" P5 n$ `! M6 O #define S41 6
- t: @+ o% P0 C& Q" A' j/ @, i #define S42 10
1 _9 S4 U; k `5 P( Z#define S43 15
7 I8 b: |. p) O- F9 M. W #define S44 21
. R Z8 x1 q% @+ l3 f3 j
3 ~( l: R0 l1 P! j$ ?1 ^ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- @$ M4 ` p5 [, y static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 t7 k' `* f# ?/ A+ U, Kstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. m7 d8 {+ I! L$ g static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& a. l' x3 A8 H static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# K1 d: p! `$ b, U( o) p
) I9 _, `( o$ k7 d( d 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
* G9 I2 D) i/ F, b, e4 M" B" { };
! [- N% f+ s" R6 G/ v6 J
! o' y P# y. }; i9 T. E, C/* F, G, H and I are basic MD5 functions.
*/
! g5 q4 F' Q2 c: j$ u+ C0 k #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; `3 r3 f7 k1 j! n0 N# s: k R) o#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! }0 j$ t0 l0 P3 E( z- r" U #define H(x, y, z) ((x) ^ (y) ^ (z))
+ D. A7 Q7 G3 [, m; f8 o* `" g S#define I(x, y, z) ((y) ^ ((x) | (~z)))
r% O e( t3 r' x7 X6 b' x
) N5 Q3 w- G: l/* ROTATE_LEFT rotates x left n bits.
*/
9 A! k" }. U5 v4 w2 i6 q" u#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 C7 v6 F5 b2 M" B8 P7 {: f
1 C& L% c% O3 h8 k /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
" s- Y. D9 Q: Q% Y1 {/ D Rotation is separate from addition to prevent recomputation.
*/
* n8 H2 V0 S, P2 V0 g1 `9 I#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 @/ e$ g+ n" A6 w) y #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' Y7 c8 ?" D- | @5 \; Z4 m#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
L# ~4 d8 b$ J, V #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* z6 N# {3 N, x
7 c$ z$ P1 c. x. R. v& z/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 e& D1 A; P/ M: a" V c# z. zvoid MD5Init (context)
7 D6 L) C1 h5 N @* a# L* k MD5_CTX *context; /* context */
5 \2 o W& Z3 h6 [0 d, y {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, T) t3 D3 V* M( v1 N' A */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
8 m; _, @3 I) a* T8 p# Q2 a9 f# [}
# o& z0 x: z3 a h5 ^, v# {; J
$ x/ D' E; u) F8 J$ G/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 ^8 s; B ~/ o* f' u8 Z void MD5Update (context, input, inputLen)
3 I, T& M4 U; t. ]" H MD5_CTX *context; /* context */
9 \7 t7 a2 z8 t: aunsigned char *input; /* input block */
1 N. H: i! B% O9 A3 c& k$ Yunsigned int inputLen; /* length of input block */
! n: O0 j5 c' G {
unsigned int i, index, partLen;
$ N+ S( y7 a! M* U# q$ M6 f) D5 b
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 u, p# t2 h* v$ L/ 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++程序


- V/ W, G! T5 [9 v* ]: d
' D; h( J2 t2 A/ T( A
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-13 10:27 , Processed in 0.068222 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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