中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# x9 W; [3 z/ Q# J0 l#include "md5.h"
9 J. ?8 m& P6 q5 |4 E: X$ J# C
q5 y! S: u6 P& f* i9 { /* Constants for MD5Transform routine.
*/
7 S l0 _7 S6 ~$ o3 |% B) \
t. x9 h( ?( n9 o# s1 P4 q: q& w
3 V2 H! s" J' g3 i2 i1 }' ? #define S11 7
* n! Z* N7 ^5 G% L #define S12 12
1 U5 b" {! e4 d) I0 F #define S13 17
) y8 o& V; Z. O- ]9 X4 ? U #define S14 22
* a( a; B4 F( Z7 }* e' C#define S21 5
0 n+ y. W5 W; h; g #define S22 9
) J" ?! R; M3 W8 G#define S23 14
/ G0 d& ]8 x9 y, `. Z4 V! v#define S24 20
; j' R) ?! a" G# t- i #define S31 4
+ l0 C& e. o8 B5 s#define S32 11
, t3 O+ b# k& O4 ]0 L; C4 @ #define S33 16
. P9 t& Z. k7 H% j#define S34 23
0 c6 X7 `8 K( l, ? #define S41 6
$ X) y x- B; H4 {6 g# Q/ b1 Y#define S42 10
- |4 t' ?2 b$ \2 W+ e: D$ _ #define S43 15
9 W& ]2 U2 g7 P; |9 ]$ J0 u#define S44 21
6 B% f- T1 K2 I
% M7 R/ v( @- w! m; A0 F static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
: }$ X" |% n" s static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; ^( W/ O% l/ v% C4 `6 r+ E# z static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* V' Z, r) g' {" {static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
' b2 K+ T2 g7 `/ j6 A. istatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
4 e1 d3 U/ p6 j j6 w, J
8 x; c5 I! x* J7 K" h: ~$ | 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
; `6 X2 Z7 l3 n9 | };
3 u7 E- j; R+ _$ T
: P; \8 V, P. j) c( ]5 T* b. R: ~ /* F, G, H and I are basic MD5 functions.
*/
6 t" ^% f4 D$ d: Z7 e2 Z#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! j/ ` H2 f! n/ `: Z' o% ~- R* i #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
/ g! ^7 U; R# l2 S* Z #define H(x, y, z) ((x) ^ (y) ^ (z))
# d' D. u8 E2 c" O2 }6 T( ?9 x/ |#define I(x, y, z) ((y) ^ ((x) | (~z)))
9 m5 R: M& r& W) o9 U
0 E; P$ a* G9 I/ |/* ROTATE_LEFT rotates x left n bits.
*/
2 _$ o! w( Y2 g' D3 J& [5 V6 i1 R#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
# O6 {* C3 b, y" t. y
5 j W; [0 `% \- V( U /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
' e7 E i" M3 D Rotation is separate from addition to prevent recomputation.
*/
5 ~7 J: _! v: y4 S/ J; L" U3 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); \
}
+ S! I8 |+ c+ M7 u6 t$ W #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 ~* _1 j7 \/ f% a#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* }* z* X/ V9 L7 [#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 ? r+ [ Y6 @9 o) U3 [
& `! T: b" B* J* E/ `" e /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
: ^+ {. F# L8 G4 [void MD5Init (context)
0 n2 x' V. @+ e7 X9 D, B MD5_CTX *context; /* context */
6 A [9 h O# Z/ Z( e {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
8 i N+ ^2 ]8 x1 v3 E! U8 `*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
5 h; [5 T" W2 D}
' S9 f2 T+ z" g- p
$ O* c, ~' i( u8 h' l/ D /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
$ ^/ H, I3 n1 Tvoid MD5Update (context, input, inputLen)
6 f/ `' w% F* `! R" d+ @MD5_CTX *context; /* context */
( |5 D( P' V# q' h8 Zunsigned char *input; /* input block */
& e- t- m4 O+ ]% F) b% |+ yunsigned int inputLen; /* length of input block */
- f `) ~$ I0 c{
unsigned int i, index, partLen;
' P( V1 Y3 c/ l+ b4 a8 ^9 h
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( z8 a. J0 a u+ U$ y
/* 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++程序


2 p: }& I" [/ f a; i i
N5 W5 R0 F' w2 d3 M
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-8 11:41 , Processed in 0.085114 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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