中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
E" N q3 |( Q6 o! h#include "md5.h"
$ T8 d6 O4 a [* K/ [- V
3 K( T& y& q% b3 w) x4 `* M7 } /* Constants for MD5Transform routine.
*/
. }8 s' P2 r6 L2 v
z7 M0 J4 G6 l/ m
( Y+ \2 B# W6 c% d; p' F" n#define S11 7
% ]" Q; a8 n ~% J7 [7 f#define S12 12
, Q, ]0 ]4 N+ a/ K+ R( x, T7 H$ C( l #define S13 17
3 r$ P& @% i2 |4 ] @, u- J #define S14 22
2 A. A4 G$ X5 g- w2 D5 G #define S21 5
/ Y7 S- _* A5 F( p#define S22 9
8 E+ g0 F3 f6 Q% k2 e2 U' ` #define S23 14
, Y- u3 T4 ~ j1 [, \1 w#define S24 20
9 ^' l0 A J$ u( Z#define S31 4
~; f8 T" m2 R/ ~ v8 {#define S32 11
5 L8 G# @. M+ G* z" A/ Z #define S33 16
: `. }! a: j0 d9 { U, T# C/ ]+ j, V #define S34 23
+ F' C) y, [& ^, Z0 B |" u7 p#define S41 6
: ~- q. Y o( g #define S42 10
# `& ^8 P( b" W# ` ~#define S43 15
0 Y* `" U: A1 @3 D: }#define S44 21
, |/ W5 X0 ?& L, @8 t8 x/ V% N
5 `$ L$ T" J& }: G0 Q* N0 D static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
& |* K3 C5 D. x, E/ V3 H4 n* ^static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
, Z0 [. ]" @1 L, K) r$ Y# nstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
z7 I, h1 p& U q7 n" ^ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! ?5 |0 c7 m& T9 a1 q- A8 r static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) H0 d. D! q: _
{7 w! d) N% [+ O! d( i 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
4 Z# X6 r8 \5 E9 x* T9 G};
Z2 ]/ m8 ` ^$ d: M" e7 B5 G2 o
" D* ]3 z0 m, D. n5 l( F1 C/ r/* F, G, H and I are basic MD5 functions.
*/
* J. B$ k E5 I/ O& m #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& L- N" b- k# J/ n6 G#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
9 y+ e& H6 P" t. J #define H(x, y, z) ((x) ^ (y) ^ (z))
6 _2 O7 ^2 s( v1 q, v. X- S#define I(x, y, z) ((y) ^ ((x) | (~z)))
3 C+ e) t5 }- r
1 |! w% A6 T1 A+ `7 q4 P$ W6 d! u/* ROTATE_LEFT rotates x left n bits.
*/
% v! P- I# a ? #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
8 ]: S4 p9 U* S
* O, M% A3 ?: l8 E I% Q- Z/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 u+ l* Q. X' L5 D. pRotation is separate from addition to prevent recomputation.
*/
B% R, l2 t F! i1 r6 w#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, e& ~% k' L; S8 w( o$ \! \#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 d5 u m$ j) H5 F #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 _) L$ e# V: Z+ o) e5 x3 v0 U% 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); \
}
/ v) q& k7 t$ u7 d0 E) f7 q/ M5 k* q
* ]4 b. Y% I- _2 h1 l /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
7 l c. S7 r# w' l U& k. ]7 u* Qvoid MD5Init (context)
) G: ?8 `4 O$ l# g6 TMD5_CTX *context; /* context */
0 n% W2 G: g7 y {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# m" ]0 h' G" j. f& M*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; ?/ W1 o' S& B" C7 e) x! a}
* a+ o& C; m4 W3 {/ P& a( J. q
& d1 r) y/ b( w /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
! k* V' L* `0 v( c void MD5Update (context, input, inputLen)
& s7 N- A& X* NMD5_CTX *context; /* context */
$ o0 S9 L2 d4 A' {* Punsigned char *input; /* input block */
/ w5 y5 ^; Q* u1 U) G. ^unsigned int inputLen; /* length of input block */
9 |( q! k$ F K+ n4 g {
unsigned int i, index, partLen;
5 o6 p/ [$ }' c3 O; w
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
3 C4 U- A' p; D7 k2 Y+ F
/* 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++程序


6 {* W4 b5 [6 w4 w; q+ ~1 o
! j4 ]$ f2 B6 |! c% s* Z ]- h1 }
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-21 03:15 , Processed in 0.055811 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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