中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; v; P# o# s8 K; U7 n& | #include "md5.h"
4 P5 L) k8 s7 o0 M9 ]% w; O6 U9 g
! a7 q: e J- u& L) W$ I) V) q /* Constants for MD5Transform routine.
*/
- ^" K9 b, d9 c
( q b% j6 X' }9 L
4 F U4 z8 {0 _# J- N5 V4 e* l #define S11 7
0 m9 W; Q5 X9 P8 R& y #define S12 12
3 k9 s% V& G* K) `9 a+ H#define S13 17
' F: M2 r: c) ?3 F; x#define S14 22
- A& @4 C; N1 l* } #define S21 5
" h4 y: J2 r# ?- [+ }; w #define S22 9
: ]$ q/ ?9 X' V `7 O( ~ #define S23 14
0 r. u& v$ |" Y% G# U #define S24 20
" u! T& h" Z- K: T$ z#define S31 4
7 H& l* a& ^) T0 t6 \ #define S32 11
) P6 w% B: ~0 p4 t#define S33 16
! h7 y/ ], q3 e: @; E' M) i" t5 ~#define S34 23
/ n1 \, q9 n/ o% i) V/ X- I6 q9 ? #define S41 6
/ T$ _+ C6 C+ m) [, b/ S) i2 ~#define S42 10
$ a# g4 a% ?/ n# b: Q& x#define S43 15
4 G+ c; H" J$ Z#define S44 21
K/ ?& B. }, r
+ x/ Q- h( q) T+ F9 R( P6 ?8 astatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 L- n$ [2 G" s% |static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
: M/ y/ o- |/ \6 x! {static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 q$ b8 n( m" l" @ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
) M: W- @( P* R: a1 H3 cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% j4 N5 P8 u. q7 N) H! c1 `
/ [$ l9 N1 K& H. Z0 ?- Wstatic 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
1 t# S- ~( e8 b( U' z3 H1 b& G+ P};
; f; b( \: q f, r3 j) Z
# L- a6 ?+ J6 e7 s# S0 l4 r /* F, G, H and I are basic MD5 functions.
*/
+ [5 }2 y/ l6 c; n/ f#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# R! U. ?7 }+ M: i #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
8 ]' v) c Y8 O- \! u. }$ d #define H(x, y, z) ((x) ^ (y) ^ (z))
. ]2 X, a. x7 |2 E/ H #define I(x, y, z) ((y) ^ ((x) | (~z)))
+ t. L" @5 ], y1 V9 j# F
1 H; Q/ z4 i8 d4 x* r" s/* ROTATE_LEFT rotates x left n bits.
*/
7 Q. |, c) h6 q$ `. {; f#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) m- h; h0 y" p6 K0 B
8 g) E/ g. M8 Z/ t" Z; G /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
5 d- C/ u9 l$ N# z& w( E% k( O Rotation is separate from addition to prevent recomputation.
*/
3 R. W0 d) G7 K% p O, c #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 `7 A7 M5 T$ {/ 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); \
}
3 r/ f" R" y' A0 b #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 y2 B' v/ O8 @$ g$ t- K) g, y' r #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! T7 X& {1 h. p; H
9 @) l1 n" i! X4 c( }- c /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
+ ~9 B v0 A! Q* a0 Evoid MD5Init (context)
0 I$ O9 R) J% h3 @* V; r! s" B* W% N MD5_CTX *context; /* context */
* ~7 U: S: ^0 W {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
6 ]+ c1 a/ n$ F C) e) }5 z# V* @ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
, `5 I) D2 ~0 J. F# c% G}
! j9 N! F. }+ J' Y' y- P- F/ m
0 k9 l: ^4 t1 J W6 V2 W/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
- q, P' X" c6 N3 E7 Dvoid MD5Update (context, input, inputLen)
# n) L% e: I f% H* ]- W. b; L0 ]% uMD5_CTX *context; /* context */
2 F. q, U! L* y$ ? unsigned char *input; /* input block */
( g3 a5 |- ^* k+ h7 W+ e, B unsigned int inputLen; /* length of input block */
7 O0 |0 _8 G! o% @- A {
unsigned int i, index, partLen;
' _* V0 m2 s5 v' \* W
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( ?* V/ @0 ?) z: b9 k3 h8 E
/* 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++程序


5 \% x5 s" m2 j
$ ?9 k" n) [6 t1 E
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-4 08:12 , Processed in 0.141834 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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