中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ R5 e+ `2 ]; m9 L. C( b#include "md5.h"
9 D+ w' b' E+ w- z
/ C$ I) c: Q) m5 Z3 I/* Constants for MD5Transform routine.
*/
6 a4 f; `% U/ `0 i7 F7 u
' @& q2 H5 N7 k! @* V/ m8 H4 O1 O* N
3 i0 [7 {% c9 W0 R/ a3 d+ n#define S11 7
) i6 |; ?" O2 N" ?+ L% z! ~#define S12 12
# E; M' C! H( P' R#define S13 17
! t+ p, F# `% B# K6 H#define S14 22
. E# d+ z1 b" P% E- Y* z1 j' Y2 f8 N #define S21 5
) c/ s9 }1 n B; \$ c#define S22 9
' o9 V) {: i/ {; s #define S23 14
1 n3 ]# Y6 n& K#define S24 20
; y3 f$ y3 x0 [5 W1 G) p# z L #define S31 4
9 Z( N r6 @4 o7 @5 } #define S32 11
, |6 V5 a# ~0 `! S5 B: ?. t- x#define S33 16
- Z2 l. [, U( K7 o#define S34 23
! E6 c/ `, Z+ Q#define S41 6
5 C0 S$ B1 Y- M9 A, k1 P#define S42 10
0 F- k" v/ L8 Z7 p: Q) x #define S43 15
1 G( }; n4 S: [* h) u #define S44 21
1 P+ s4 _5 y: e2 w% @
+ f, L: ~. N3 U) D+ e) Z7 Jstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
# x! t+ U3 S0 @5 B/ Ustatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
( u* ^9 z7 E y. W f: B" L* Sstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
+ j }' n! X' S# istatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 a- C7 }7 [) O: ~6 {8 Estatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
3 n) G/ {% u) O2 E N5 O. O
5 h |# J0 A, y; q. z5 ostatic 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
9 |3 r) w1 C; l' Q* U};
0 r m; L; t7 S% Z# I. ~
; G L4 V; ~* }9 ^" {% L* n) _/* F, G, H and I are basic MD5 functions.
*/
% }: d5 I I. x! }4 E#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
) y6 z6 X6 |6 B5 k8 r0 t5 ~#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 Y4 C4 d. e- ~1 ?#define H(x, y, z) ((x) ^ (y) ^ (z))
4 C0 W, u( @6 K# A4 [#define I(x, y, z) ((y) ^ ((x) | (~z)))
+ Z3 z j& ^6 E
/ X! i6 t" C c; e T /* ROTATE_LEFT rotates x left n bits.
*/
3 w" D1 }8 J+ l6 s+ h' q/ Y#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" q+ ~$ ]/ k2 r" N* \- G
$ M# ]% ^' Y5 V, {) S; g% O! v- N. e3 `/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
, _. r* ?+ G! G+ C3 K4 Y; wRotation is separate from addition to prevent recomputation.
*/
, h. n2 Q1 F9 Q* J #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ Z- ^4 B h3 |7 A" D% j1 a, d0 l#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. w. \- J# r& f. {, v0 Y/ t3 O: a- d#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 w7 s9 y0 Q: z# ~7 l6 o; q #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& g9 U' F3 V8 f, X# O
0 ]% S1 X8 {& F+ s/ c/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) ^4 P+ s1 z$ R( f4 |6 Q7 Dvoid MD5Init (context)
4 p. i( J+ q7 Q3 o v. h! r1 J: OMD5_CTX *context; /* context */
7 {2 G; F8 ?/ R0 D! m; d7 V{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
3 I: Y) x1 y, \, e1 x+ ?7 e */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
: ]2 {; I6 h/ Y+ O" S r }
) d* N3 t$ j( s: e8 t
% ^" W5 w* N* t, P/ N) K T/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* x, g) c( ~: cvoid MD5Update (context, input, inputLen)
. ?; X/ y+ M6 Y$ U/ xMD5_CTX *context; /* context */
G5 r2 m# h. [& d2 Y( ]4 Eunsigned char *input; /* input block */
& A0 t/ {3 e' R& \3 h& tunsigned int inputLen; /* length of input block */
& v. y; f7 q! [7 e, Z{
unsigned int i, index, partLen;
8 h U' | G0 d! u: n u% K$ X
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, H5 U i5 e) y' W
/* 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 z, B# t* s+ [2 p/ |
. E) _5 ~" l! k* {+ H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-24 23:46 , Processed in 0.261902 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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