中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! M$ W! D$ l$ C# a$ f0 D. l#include "md5.h"
$ R5 ~1 o* ]2 T1 ?' S8 [
- B D1 s9 _! q /* Constants for MD5Transform routine.
*/
9 l* K" C2 s6 f
7 a: G% Q7 E+ Y y: z+ D
9 O( e9 |. L C# M; D#define S11 7
7 j# i3 k# j$ e5 g #define S12 12
- B, _. n0 l6 S# K #define S13 17
6 g' r0 y }4 }, @#define S14 22
6 P+ }3 ?) z0 r7 W% t, s1 W #define S21 5
, s$ l9 w1 ~. |8 l! Y7 @ #define S22 9
, @& Q* N! Z) B1 p3 @: K/ a #define S23 14
& S3 P1 f; Y) g l5 R#define S24 20
/ C" \1 s; @3 V" x H #define S31 4
9 [- s9 V N2 [/ J* f #define S32 11
+ c, G- x0 Q/ f! | #define S33 16
& T! w* V0 M- A- z! T% e. R. _: r" E#define S34 23
8 d3 I" N: [0 v+ m* ?& w #define S41 6
# [" e0 Q/ h r& F; [' |5 ~6 ^- i4 e#define S42 10
5 t; y& R) j1 u. Y: C #define S43 15
1 j1 [. b" _4 x- }. s: C- I' N4 ?! W" z#define S44 21
2 {: V; X9 }6 i
. l9 h0 S' S0 c/ U static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
; h3 F* S' J$ ^+ d9 a8 h, G8 jstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
: a8 c! F8 O% L6 |. Estatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 x( ?8 J5 |* D' G+ fstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ M* {8 A! ?% j8 A static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
$ W% `$ t: s: [
" o3 q- q' n6 R/ ]' C+ Q 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
- M4 S& P; f! X/ a0 N };
; ]5 ^% o# P# j" y) n
: x" V @; r4 Q d6 \/* F, G, H and I are basic MD5 functions.
*/
) H$ h) F1 `5 _7 E/ b4 i' L#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
: |2 f! |6 ~) t* O& N# {$ k; |, @ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+ v4 v2 p% w% y/ Y* X. Z4 @ #define H(x, y, z) ((x) ^ (y) ^ (z))
& O7 q: d# {9 m2 ^! d" C#define I(x, y, z) ((y) ^ ((x) | (~z)))
8 m- v* W# n9 n( s" v7 I
. s2 b* A! v: J$ P4 z8 Q/* ROTATE_LEFT rotates x left n bits.
*/
/ ]! ?' b/ K- k. z7 s: h. h9 y #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
q2 c# c1 p- ~. ] W
! y. r! F& l& J6 x/ [/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. c: X$ v% p' ~" m! fRotation is separate from addition to prevent recomputation.
*/
+ ~: t2 t7 ]8 `# q: x! H. l #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# z2 H' E6 D& ` #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! Q; s1 i' T, J #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 _: k L: ]% P#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ z" \' M% m9 d$ G9 z( e
* e% C1 ?+ Z" f' b! w/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
2 ~% _- m- T+ C- mvoid MD5Init (context)
: L4 P+ X6 Q# x; k7 j1 {" m MD5_CTX *context; /* context */
! K% u/ q# h7 m {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 `' v" H6 }" `, ~9 ~ D! f: J: e */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
- j- O! H" t8 q2 M* Y }
, J4 D0 O2 J( g5 O
0 Z$ {6 L }$ ?" p* j3 b/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
2 Z0 h9 I! S1 N3 u9 h7 C1 u: { void MD5Update (context, input, inputLen)
, ]/ q5 N: F& a1 e, {8 z, r) YMD5_CTX *context; /* context */
( e9 K; C' Q# f3 J! r. F unsigned char *input; /* input block */
X1 g$ q/ E/ | unsigned int inputLen; /* length of input block */
5 u* d6 v# V3 f# C( a; U {
unsigned int i, index, partLen;
) I+ ? o" S* g. [/ V6 @
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
" e+ |) c' K& r' p
/* 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++程序


: b) G" ^0 ?* r% J
3 F% \/ W( Y7 o8 ]" E3 o
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-24 01:46 , Processed in 0.055763 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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