中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
6 _( e+ m3 Z- C& [6 e% C#include "md5.h"
* g/ ?% c- T7 A3 @1 L5 J: V
( b* R4 k d$ f7 U6 T1 m /* Constants for MD5Transform routine.
*/
. Q* d; W2 [' M& i
$ z; h1 q3 R) Y9 [
# `& \4 d& t% [6 f9 X! ` #define S11 7
# q: v, ~6 ^! M2 S( e#define S12 12
0 P# R* a+ W( t$ U6 u#define S13 17
! l+ _. Z+ v( g#define S14 22
, ] e# G/ r6 X% r#define S21 5
4 `2 z9 t2 X& ~/ i l% I#define S22 9
0 u. }1 y5 [% t) w1 ~; d2 a& W; x #define S23 14
3 o& ? G, ~6 T6 i+ f0 [" F: f#define S24 20
5 l" v) V6 g p#define S31 4
% V+ ^/ S4 }5 o5 m) O! p #define S32 11
9 e% J ~9 q( z! M ~4 F* q1 R #define S33 16
0 V+ y- z1 W/ v/ E5 v #define S34 23
1 B. Z0 ?$ `- q9 O#define S41 6
: x7 ~ i5 M9 o5 J0 N #define S42 10
. G" ?: @8 Q. Q #define S43 15
2 ~; c4 t/ R' P/ w/ N#define S44 21
% K: |. K! w( v
: m% B- V# {1 ^! V8 `; ^ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
( r; k$ }0 F* c$ p3 f8 l static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
q: \: e: p4 q. {/ Y- ^static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* F- |" `0 a1 w. T: Nstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
3 |' ~: P5 W6 Rstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
$ L" g3 [4 J' i) E- I! {' B( p
- }! C/ U# y" }; g3 T3 G 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 }& D4 ~: p3 _8 i) K& [4 ]9 g1 W};
. B6 {4 \* A* G/ g
' E8 c) U S+ r Y# A [4 q/* F, G, H and I are basic MD5 functions.
*/
y3 d9 z# V) Y! x #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
" y+ t' w9 @) Q1 P. B. K D- {3 n4 B #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 L5 W0 q3 C3 G3 H #define H(x, y, z) ((x) ^ (y) ^ (z))
8 e4 G6 w" C& }& w6 f* d* D #define I(x, y, z) ((y) ^ ((x) | (~z)))
F9 A0 b5 |0 S+ _/ o5 g
( D ]# p# Z. }( D8 S$ g/* ROTATE_LEFT rotates x left n bits.
*/
# C# l/ q, p( g. \ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- k5 l- y; @% J% S/ `# h1 l
1 g9 B$ T' O: X4 u/ h6 y/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( q- |; }% O; z5 M* D8 w Rotation is separate from addition to prevent recomputation.
*/
, O8 ^4 t/ `0 U #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% v& G/ C3 A% x) E#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% K" ?6 g, Z+ F; J" 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); \
}
7 G; U6 Y" |3 u+ V4 I #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# K2 r* H: R5 I6 W- c5 g
8 |8 z# I" {7 x5 [8 L /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
& ^( C; Y/ z/ B! Z" tvoid MD5Init (context)
T: v4 v" i) ^: FMD5_CTX *context; /* context */
- a. y* L% u' \7 e [ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 N/ l( J% t& f+ _*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ ^/ h6 G# z! f4 \) n: B1 {7 L }
# c" h i" p6 s) v
0 k* _) V3 G, T- r/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 j$ @. N" F2 G& a: M$ Svoid MD5Update (context, input, inputLen)
& o3 m& B5 @/ N MMD5_CTX *context; /* context */
( ?9 E# [% w6 s ounsigned char *input; /* input block */
+ a4 D( U# P- O& Y) f* p# | unsigned int inputLen; /* length of input block */
2 @3 [3 z+ l4 C{
unsigned int i, index, partLen;
" z- `7 E) x5 ]* S% D0 }; Z c1 Z4 Q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
; K5 b: I( U. [+ b: v- V1 i& V
/* 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++程序


( J, w n/ P/ B5 o3 K
$ ?* r( E% c/ e+ y
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-11 00:48 , Processed in 0.079890 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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