中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' f2 C6 [- w* f% E" X7 D#include "md5.h"
: g% R, }! Q4 b! {* f0 p7 P
/ ^7 m" `7 l; I8 b: c: M /* Constants for MD5Transform routine.
*/
6 p H! L# ]7 o* ]' u
A+ I" A* D6 Z d/ n9 |9 ~
3 J2 W) c% x3 q5 \# f8 r+ |#define S11 7
0 X6 H% A; {0 t& u3 X% x1 j#define S12 12
' L. K' ]9 M# j8 S) \#define S13 17
( [# n7 W7 P( v$ j Q8 @6 G4 q. _#define S14 22
- t) j* W5 X- O* L$ \ #define S21 5
G& u$ X& N" }: D$ H% ?7 M$ ^ w #define S22 9
5 q4 l$ i. O, s2 I; i' C0 Q: r: |#define S23 14
1 h& t0 A) h- B#define S24 20
- M* ^0 Y, P* H1 y7 O/ d#define S31 4
1 C( }! y4 Y% G+ R) w9 X#define S32 11
3 ]% w$ O1 A( I5 I' P8 f3 g#define S33 16
* Y9 K* a' \# L: M9 E$ G #define S34 23
4 }7 v* @. K7 f! h #define S41 6
4 H1 y: X! x2 J$ i N#define S42 10
) @9 O( F/ h4 h0 t( ~. }( D#define S43 15
/ T g! P; F+ r; o- d4 M$ v #define S44 21
$ J$ a3 Z% ?: r. S% w4 t8 e
9 E5 ^2 b- \2 G: A7 s. ~static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! K9 D5 u( e% B* hstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ U0 v: q/ Z! sstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
M2 F; `( }. y4 | static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
1 }$ K) t- }) U2 d/ q. lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
4 ~9 @0 O. a B2 O; S C" R6 Q! @
/ D7 }1 G2 y% v! \: Xstatic 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
8 I6 c |- b: T( L };
6 X$ Q2 W; |! R S1 `* I! _5 n
- Y& G2 R! w7 C& ]! }. s /* F, G, H and I are basic MD5 functions.
*/
2 }2 N% Y. ~9 |. k1 g% O #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 I" L6 o \7 z! c#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: |3 l% S; e/ Y: u #define H(x, y, z) ((x) ^ (y) ^ (z))
# k8 {; {9 y- _! u8 R' ?7 W6 h* n1 v #define I(x, y, z) ((y) ^ ((x) | (~z)))
3 k! ^) ^7 R8 e5 `6 d
3 M; p) ?2 v3 g& Q /* ROTATE_LEFT rotates x left n bits.
*/
" o f/ G" i% s' o: s$ ] #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) L( P7 T0 ^/ h
8 K' p3 A' X) y$ \1 g; ] /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: R6 H4 a4 F$ `$ t! j6 j Rotation is separate from addition to prevent recomputation.
*/
- r. M2 l6 c* @1 T' 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); \
}
$ p- F5 w2 Z7 V2 o6 S) \" n #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" `% G2 P, H$ \2 Z5 g#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 n4 c$ j# |. `! v @; C0 r5 b #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 t; O6 o G! ~! a8 {' v0 n
, A4 S, W/ L( b /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
4 B& y' r8 L2 ~ Z A void MD5Init (context)
% T# v) J1 L, t o' ^* b8 ~MD5_CTX *context; /* context */
+ ~1 s4 d/ i1 Y! R4 N{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& R3 |9 S- ~7 n: @8 h* ~ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
% x* ]. l9 ~& a2 p) G P% A }
$ M9 r4 v% H" ^% Z+ F! r# X
) b7 d; m# m) \' w7 S1 z /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
6 m* T! A3 j) B7 b8 j0 f; t. x void MD5Update (context, input, inputLen)
+ S" [" z% p4 PMD5_CTX *context; /* context */
$ [4 L k5 @. t0 R e; F4 @ unsigned char *input; /* input block */
8 X6 {- f# X# Y8 r& lunsigned int inputLen; /* length of input block */
- F1 D1 F, K9 x! M# d% A{
unsigned int i, index, partLen;
. H1 B& o) t! j9 L3 Y0 g
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 S- I+ E# x% 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++程序


* {9 U" d) Z# F, _0 d4 R& Y9 n' V. A
' z: U! _4 Y$ J9 O4 |
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-28 10:15 , Processed in 0.087557 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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