中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
5 Y/ t1 @, r* m" U#include "md5.h"
# a4 A& c5 u! [' V% ^% `3 I+ q3 r: q
|- k' H+ U" M1 U" m /* Constants for MD5Transform routine.
*/
, t0 S; m3 G4 a- E- |, O
1 }' h9 u" r1 E) x6 j9 ]; O7 @
8 [3 Y6 h5 _3 t" L #define S11 7
# q7 A, L3 j4 N# o# @. e" F, k #define S12 12
0 `2 c4 a9 N% S/ A- e #define S13 17
( c! i, B: b0 N" Q" \#define S14 22
7 ~! o7 d" i* u& X9 ^" M" ~#define S21 5
2 d: ]7 b% ?% O9 X2 W#define S22 9
3 Y5 S2 ~ h7 h, A #define S23 14
, Q& B! C2 y9 Y: O" |#define S24 20
% A1 G& E9 k% C. ~" v# M g #define S31 4
8 X& b- c: Y# O$ L4 ^0 U3 [5 v! E #define S32 11
5 w+ ?% N2 p7 ]- n/ L! u$ y3 Z #define S33 16
' W0 ]4 v: K7 c$ @ #define S34 23
0 c) e0 ]! T) F, X5 E#define S41 6
( E% r9 c8 O. b) \#define S42 10
$ ^, u: [( U m* X #define S43 15
5 X$ B' r2 u; n* g#define S44 21
9 R/ B( }1 o! k: B# b
/ t0 \$ r! h, C9 W$ }; O: I$ f8 ?) vstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ ]0 ^( L& M) A& \5 l6 C/ ~9 X, Z static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% U, A: C8 S7 x8 i1 f static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 F7 ]' u6 v. y, x# B( X& Ystatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, _) S% ]% A: l. ?5 _* ] static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& B5 g; v5 l3 Z8 x( ~ A/ U
. Q7 W' Y1 o" E4 u, F' Mstatic 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
! R: c) i/ {5 ]" i4 m9 v T };
* r4 b$ {2 d) G& a
( b9 C$ F+ b0 Y: t( i9 F; I' }0 a/* F, G, H and I are basic MD5 functions.
*/
4 r3 `' i1 ~7 m6 s #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 L: s4 C, \8 K% U #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; B4 b O0 i( q4 o#define H(x, y, z) ((x) ^ (y) ^ (z))
. ^2 z. y! h7 }$ B, e" \#define I(x, y, z) ((y) ^ ((x) | (~z)))
$ q1 C' Z! s' c$ j
7 a5 u5 m. G4 T9 k0 W, c/* ROTATE_LEFT rotates x left n bits.
*/
* |' @4 i" L% g: R. m, X4 Y#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, O3 Z! _9 R! Q- [7 b3 x+ o% X$ a4 K
9 t, w; E! o6 E. w. p! r' @: q/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ e0 l- _5 x& S5 L& r. cRotation is separate from addition to prevent recomputation.
*/
1 v# z& O/ Y( G, D) E1 m#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 }0 n1 Z$ W% G& Q/ b9 E2 T#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 x* y: ~2 G T1 P/ N8 H" a9 |; U$ S- s #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: d [( S9 {* Q; z K( O$ M #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ B: U2 J& i+ W% J
. K) K8 m" ^) r: t /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
6 F6 w& s0 @' y* nvoid MD5Init (context)
3 p* n u* e' i2 h/ F$ S2 }1 b3 I, u MD5_CTX *context; /* context */
* P. u* A7 G1 x {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. _. j" `/ O7 p7 D2 a) F, U6 W */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) @ v. r( p9 X0 p* B8 H}
! a% e" f7 i- x' R
7 e. x& S& m. n/ \ /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ n" ^+ ^0 z9 h" x3 g9 V6 Ovoid MD5Update (context, input, inputLen)
1 E9 _6 z) _5 F, TMD5_CTX *context; /* context */
7 J9 G3 x* W2 P g1 Z- ~5 D: \unsigned char *input; /* input block */
! I. }7 ^; ~6 U, N unsigned int inputLen; /* length of input block */
1 Y" q9 [0 K! U6 A2 e5 o% F {
unsigned int i, index, partLen;
- Z) g4 K( C* g2 d0 Z: j+ H/ }
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
2 P6 v: N: d" P9 q# A# y. o
/* 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++程序


# ~8 o8 V/ ]# X. k( V
/ d0 o# J5 d( w% u
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-30 07:27 , Processed in 0.068566 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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