中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
& t9 B$ P3 B4 B8 I' { #include "md5.h"
3 h+ \7 v( Q/ N& z4 n
6 W' w; m7 {, Z" h/ r /* Constants for MD5Transform routine.
*/
& u/ Q) P6 | N2 v- s
; l6 o# m1 ?: `0 w2 w7 @. }9 @$ K
$ i" m2 [* }5 F s1 n' y7 B #define S11 7
1 C) f5 Z" ]- f; v- l #define S12 12
7 ~) ~1 Q* l0 n. r #define S13 17
: j, s7 v: c4 L- \& T+ r: \/ d3 _ #define S14 22
4 { c9 k5 `$ Y& c* j* R9 N+ _ #define S21 5
7 W) v. p9 T$ Q: ]4 j6 Z* m3 ~#define S22 9
, h) w; F/ e* \ _, J* Z% g#define S23 14
3 M- k9 [5 @; v7 T2 Z# s) |" T#define S24 20
6 Z8 d: D k4 E! M5 A- S8 g #define S31 4
% W7 X' S' `: x- ~" s #define S32 11
4 g+ q6 ?5 V$ m. R7 e* t3 E #define S33 16
$ \$ P' z. @; _3 Q2 [ #define S34 23
6 [; h5 r% w M) ~8 R#define S41 6
% g% M9 H+ ?: H; P$ Q* Z( a. g#define S42 10
# H5 C% j( v/ z9 G. u#define S43 15
: f- X7 ]/ @. Z% ^' N: S #define S44 21
/ t8 {: e+ n3 N" R& E! G. I
- C2 |1 `) N% [7 i7 z$ X6 G static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 s/ S3 L- j9 V/ k/ x4 D: Rstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ d& w' d- R$ Fstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
+ \1 P% Q6 h7 H5 n O; C static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! Y) g2 @' u& estatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. M0 h# Q {0 D* p2 D `* P
' p/ x; J) u& c8 b+ P2 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
% q7 g9 x0 [. q$ T5 \& J" r};
D! F# Y i& R) v0 A3 w
" c& ~* y" g1 }. J R/* F, G, H and I are basic MD5 functions.
*/
3 }; v9 n$ E, ^6 x" o5 b #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
2 D2 }# Q0 S" ?0 X#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 L/ @* n1 F+ Y+ E#define H(x, y, z) ((x) ^ (y) ^ (z))
; b: x9 D6 F) k" {#define I(x, y, z) ((y) ^ ((x) | (~z)))
9 Y) K( E2 W3 N. c2 C" J
k/ K# O X+ n1 f: J/* ROTATE_LEFT rotates x left n bits.
*/
4 H( s. J, R- ?- u1 J% X#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. h8 B9 k2 S1 t# a0 z2 H7 f
! q0 X: u1 l. J8 g! y6 A) `/ n /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
! m- m8 {- T3 M4 P$ \Rotation is separate from addition to prevent recomputation.
*/
1 J+ o" R3 _6 P, P. 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); \
}
. f1 M3 s* R7 O+ m& C5 m" ?; @: g* _ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ P5 m" D6 M7 Z% J4 ~ _#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& n& [& d* s j4 K) j #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- [ c6 c/ M, S7 k& r# g
i3 X( V; C! I" U( @& [' F /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, ^ V% N2 G7 c1 E, p! K- `& C, ]& v, G void MD5Init (context)
* Y" D2 x9 \7 J1 E4 j! J MD5_CTX *context; /* context */
0 n1 n. u. @ W6 F! ~8 o- e {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
6 Z# _1 j3 _" X5 P- R( b4 p. g1 t */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) s% z1 H7 m% W' T4 N" ~8 y }
, m8 g2 S% X3 [; S
# D" K. ]1 ~: W4 V /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, i9 z9 D* l" f; \4 Rvoid MD5Update (context, input, inputLen)
3 y8 w y& O+ c7 }' Q MD5_CTX *context; /* context */
( _; e) t" O# J6 y unsigned char *input; /* input block */
7 e5 R: e1 G' M: y$ |* r" h4 bunsigned int inputLen; /* length of input block */
8 p% A E- o- u# ~- y6 u8 V0 B) t+ ?$ r {
unsigned int i, index, partLen;
5 P! i* D$ U$ T# J) m M5 ~
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ M' f8 T7 t' r6 [. A) d9 |5 R; K0 x
/* 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++程序


: [% @3 V; P& B7 ]+ q0 W
# \. X: \* L" P; c+ e* Q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-3 11:02 , Processed in 0.210300 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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