中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* V; Q; {' s9 w6 G9 s #include "md5.h"
* k8 E6 \9 O1 o% n; f& N8 F# R
& V8 Y# s5 S e Y /* Constants for MD5Transform routine.
*/
# Y! M0 Z+ X0 i t1 n+ t9 g
$ o; J" l/ _5 o3 g8 ^
* }. W2 m( n y2 D: B8 {#define S11 7
R1 H4 Y; g8 X) D8 S! a& E! J* [#define S12 12
6 ~ M2 u; b6 r #define S13 17
( [. O: w& m! D#define S14 22
5 V- }, t% L' e9 l# U4 h #define S21 5
& c) [* G1 g5 P0 u7 s7 E #define S22 9
/ \! o, V" c- r. i' O3 n/ t #define S23 14
$ q% O% v# ?* Q, }( D#define S24 20
. K$ b K$ T: z7 {1 T+ a6 m#define S31 4
, e( r ^) K. ^#define S32 11
( Z/ I9 S) P* r! d* V$ C) J#define S33 16
. I2 A& V+ p! d #define S34 23
( u7 u, o9 E2 U3 I7 e1 W2 C#define S41 6
! F* ]! n p ^1 G* ^' z* z4 y; s #define S42 10
' D0 w/ ]+ X3 X( U #define S43 15
) w/ U: m5 d0 ]0 ?( A! D, Q#define S44 21
- o) I, `. L& T* e( W
* {* R0 D* h# B static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 D8 B6 d8 k, @( j1 g5 C. X, bstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 {* b8 S$ x3 D+ Y/ M6 Z4 n( Y# nstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 V% L0 O+ j1 {9 L Rstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
. I) x; |% _. x static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
9 l7 n" k+ b3 j$ t3 \# [1 B: G8 W
+ I/ x+ {; s! O2 E 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
. B- W p. y! a};
4 \1 f6 x5 i6 P- {5 V, G) q/ X
, {' w7 Z" N0 ]. f2 x6 ~3 c) [" H/* F, G, H and I are basic MD5 functions.
*/
/ K6 y2 m+ v7 ~* [- C: v# X #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 Y: R4 B3 g3 Z! G; P5 h, X8 c#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ S- |6 [. W) @! n! Z4 v #define H(x, y, z) ((x) ^ (y) ^ (z))
1 [2 i' ]* M1 {3 p#define I(x, y, z) ((y) ^ ((x) | (~z)))
* q& T) g1 `2 e* o% ^4 g
+ }: X( v8 y. M( [ /* ROTATE_LEFT rotates x left n bits.
*/
5 R/ B( C7 O* m- d' {3 m2 v#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! P$ m+ x' U$ X6 \5 ^5 c9 T5 Z" X
* H& M9 E: Z3 m6 D* x5 k /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 T0 ~3 \1 q+ k' ?! Q8 { Rotation is separate from addition to prevent recomputation.
*/
. p7 t3 K% H3 E% M2 N+ ?#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ m5 u$ P" U0 f* 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); \
}
( U% n7 I" u4 ]0 B l, d+ D, k$ _#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 s. Z2 N- p; m/ _2 t: G #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ D5 ^0 L4 i! V- B( _
8 w! n4 m7 d1 z+ m* T, s/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 _0 H6 ~- y4 h- h) @void MD5Init (context)
& l1 s0 q* {( s3 P: u% n) S) ?6 yMD5_CTX *context; /* context */
3 p& ?1 G* z6 u& I, } {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, L/ L. c1 H' J7 K+ j( H5 i1 C. }2 M$ S*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 ~" x- N6 _/ @6 p: d6 O}
0 M4 o7 ?6 f6 ^% M
$ j" [9 E- S } /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# X4 ^8 W! ^1 Y9 X4 jvoid MD5Update (context, input, inputLen)
, ~' q* Y7 k: ]% x MD5_CTX *context; /* context */
# k4 G5 L3 H; O% f9 k# x0 N unsigned char *input; /* input block */
, T) X- r! u2 U" j- G unsigned int inputLen; /* length of input block */
! }9 i5 l% S% T- h2 A. O9 _0 h \& H{
unsigned int i, index, partLen;
& M6 y! s7 \; ?0 c$ e
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ t# p$ j3 L: G4 [" j' C. Q0 K
/* 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++程序


# @# f( K2 ^3 @3 \! k# W9 g" p }
0 I3 z# d- m% G
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-11 11:26 , Processed in 0.073853 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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