中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) ~, M# ^1 d9 I( P Q0 N# X# Z #include "md5.h"
* r! o5 k0 f$ I4 C
- G3 Y G6 R( Q/* Constants for MD5Transform routine.
*/
5 X5 `2 o- [9 W' g& [4 [1 P; u9 c
9 O! S6 `3 u) y# P
" z2 p9 y( s* l/ j' T6 s, g #define S11 7
' z( V7 b' w5 z$ ]% K: C#define S12 12
! ^, E6 x" D. G+ B#define S13 17
1 W/ B! v r6 M/ f#define S14 22
- b; U: l- @1 p" J( t2 g, A! a#define S21 5
9 I; }, P5 O- V8 Y#define S22 9
1 Q; l4 y! }' y# v7 h) _ #define S23 14
- L) g' u& d |* J; E: V#define S24 20
3 Q2 e: [9 o2 l' l3 y" M #define S31 4
* J4 L" y5 v3 \. S5 I) F6 c#define S32 11
# m* {1 b( \1 E) J/ Z #define S33 16
# `+ [3 c8 _+ B* k0 x #define S34 23
! A+ C( B# \5 Q; N/ z2 ~#define S41 6
/ j4 T/ H) K% ~9 O1 t#define S42 10
& |! N' n" d( n1 b% a#define S43 15
7 ~6 ^. o: I$ |( [$ v, C; e #define S44 21
9 D; f4 e: U& i5 D0 K
; N: k- j; q9 u/ f$ {6 P static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
. V5 x' b( V# i6 S% `; K static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ `( K J" H- e4 u0 v static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 [9 N9 k) b$ K static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
1 z, f( m K) |& Y/ t l& @* F9 R' X static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
; C) i9 R/ M% H, s: A r& I/ P
5 f9 [0 I2 L7 g6 Y% i* s* ~& qstatic 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
! y' B7 B7 X, k1 L; Y$ F0 y5 m};
& C4 H; _9 d0 p( M
1 j/ K6 X! Y% D) i% k /* F, G, H and I are basic MD5 functions.
*/
/ d' ?& B+ E! P3 I/ m#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
0 {! w: `3 B. C#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: r! }4 H6 W, g. {- m/ X #define H(x, y, z) ((x) ^ (y) ^ (z))
2 h6 i) y5 j a5 u #define I(x, y, z) ((y) ^ ((x) | (~z)))
, {( u j! h6 _. z- f: a
) s6 x0 I, G# T( y0 W! ^3 W; @/* ROTATE_LEFT rotates x left n bits.
*/
' h p: W3 `0 r& }#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( E/ B6 K8 ]. q/ j0 Q
2 V" e/ F% d0 ~6 t/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
0 c: c* r. L8 C" G3 t4 i Rotation is separate from addition to prevent recomputation.
*/
) z8 c' l: j/ Y7 r) O#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, M4 [* f* F1 k3 P/ g/ x#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 X, D5 ~8 U: I+ \$ z. {9 u1 t #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* L A6 s4 A2 V% G) w" }: y#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& v- t3 T5 g! r) y8 X* G; Z
" X# j1 O% P3 p9 g2 b# L& z1 i /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
2 _. _& J N8 y n; d0 z wvoid MD5Init (context)
" d/ B4 ~- m% t2 A MD5_CTX *context; /* context */
8 O+ D# u4 u8 a7 g3 E. ]+ {{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" M9 I* Q2 ]: v1 m*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! O6 u+ \" s- [6 P }
+ i% K/ x# C5 v) t0 q
* p5 `5 ]; T8 _7 f5 p/ {8 @. D! r+ b/ Y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 k: M6 b$ |3 e. ` z& M# Q# P2 Mvoid MD5Update (context, input, inputLen)
# I! W& G" y5 C& H9 h MD5_CTX *context; /* context */
9 u+ H* c0 w+ F B, yunsigned char *input; /* input block */
9 I7 {' B, X+ C8 z unsigned int inputLen; /* length of input block */
) v" F; x5 ]) b% d4 Z7 O- K; H, F{
unsigned int i, index, partLen;
9 X# B6 ^: j- U3 L7 H
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
3 N3 K# D6 H% Y# g6 j
/* 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++程序


- W# z8 x8 n# \( U0 T
; N) a# Y1 F$ H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-17 12:35 , Processed in 0.056563 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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