中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
/ L3 E$ Z) K$ I% E8 R5 {8 R$ k* n#include "md5.h"
/ C* o1 {! b9 O+ E0 O9 l: l) c
8 I9 | q' B' H* G, N: o8 [/* Constants for MD5Transform routine.
*/
. J9 {7 e7 K6 O3 ?+ {5 i6 u$ O
; `7 Q; F+ v8 [4 V" I; A( s/ T
/ V* o4 R1 b' u( V6 ]8 W/ v% y: b$ [/ N #define S11 7
# U1 O, Y, O4 K7 A: e, I#define S12 12
! N) Q, r4 h2 M A1 s#define S13 17
3 Q1 D& T; F3 Z1 v8 z& k# W! i$ f9 K #define S14 22
7 [+ t6 [# A: Y/ h# s6 T1 H #define S21 5
( F& f) E7 g( x; @ #define S22 9
% ~+ @+ R7 o- P#define S23 14
& z: H7 l6 Z# J* S5 [5 t: `* @3 H#define S24 20
# U) A2 i; B& l( E#define S31 4
& h$ K& v: {8 }- j #define S32 11
: U7 k0 F# E) T2 n6 @6 G#define S33 16
8 K" g( \* N4 ?2 _ D' r #define S34 23
7 `! o5 y1 ^8 }8 @' G9 H#define S41 6
" N" ~: ?8 N6 I# L: \( L( ?# w #define S42 10
% {4 S1 P6 N% v( `! ^+ n' @ #define S43 15
5 x" t: q" H2 H9 g#define S44 21
$ K: ]/ R+ ]! k1 h
9 H" D; J/ f2 s4 Y) s static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
" R# Y x1 S6 A6 v6 c& k8 _, X* r static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
K u% \! f$ F) V7 d6 K4 R, Hstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
/ h& N+ M; V- P/ Ostatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
, S2 Z7 M0 p2 {0 c static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! n6 x; h+ R3 ~( i: W
% ?# v3 x$ r. S 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
2 u. Q, r9 @ T) i. B. m5 m. ] z};
% h$ o# w8 |6 e/ ?4 }
/ b( t8 j) k7 S( Q+ a9 f0 v, u /* F, G, H and I are basic MD5 functions.
*/
4 L. p6 d; f$ W9 O: p$ M0 ]#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
6 |! R$ y7 W1 V) t& f0 v' s: O#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
& q! y+ k* F7 U! Z) W1 A #define H(x, y, z) ((x) ^ (y) ^ (z))
/ {0 D" a1 D9 R. H6 ~* s #define I(x, y, z) ((y) ^ ((x) | (~z)))
5 J" F4 ^8 _. E- M
h! E5 y# T. X8 V3 G/* ROTATE_LEFT rotates x left n bits.
*/
. h, j" H, o3 L& r3 d #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) e! \; m! u0 b" N' p
; i \- g' M- W) G. E6 x /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- z) W+ W6 @# J/ z2 X4 l% N- ]5 ~5 ` Rotation is separate from addition to prevent recomputation.
*/
9 E7 k/ M( o( l4 C- x4 v #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! v0 Y1 Z8 K+ m/ Q#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, @/ \0 K" M" ~5 _% t- a4 @9 @ D #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( Q9 A9 h4 |4 Z- o3 T9 e #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
n% U. V, P6 [3 }0 k
- h/ `& s; ^. \0 O4 }* [2 w/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
+ T6 ^9 k3 ?; A7 `( ?: ` void MD5Init (context)
' O, }3 [- J- b. y! DMD5_CTX *context; /* context */
, e# d1 y: t5 a' M{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 R Z! H4 M! a! N. i9 p* a$ ? */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ C& }. S/ i. _$ p& O. L5 o }
& X# ]. q" f0 ~: w- }) {$ |
( q* w8 B* [- ?0 E4 d/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
f4 y2 ]/ d' j- ~+ g7 U! _7 ?' G void MD5Update (context, input, inputLen)
- B/ |9 }6 ]; u* w: P$ N( i MD5_CTX *context; /* context */
7 n5 W$ q K( g, U unsigned char *input; /* input block */
8 E5 H k5 t3 k- I" ~$ Y3 | unsigned int inputLen; /* length of input block */
% z& E/ J( X k8 D" q! l {
unsigned int i, index, partLen;
: c- |4 Y8 _- k# G3 v$ l
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ g- H3 k2 g+ {+ l) a
/* 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++程序


& O% ?0 T* }1 r' F" \0 Y
6 U3 g( }$ f$ b/ H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-21 11:38 , Processed in 0.061545 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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