中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ X% [ J4 A+ }* ]8 j #include "md5.h"
4 Q" A. g X: Z3 ]- {$ l: Y$ i p
" v5 ?1 g5 {; f" }; W/* Constants for MD5Transform routine.
*/
. N' T4 T1 p B. g; ?8 a
2 G5 J' b1 V6 N3 C
' W' G u" K4 f- J#define S11 7
! \' e, C0 G& L0 D$ h( f' m8 ] #define S12 12
3 E% e2 k2 q5 q #define S13 17
, | b; w* m# k; m* b* C#define S14 22
5 |3 @1 l+ ?: O. M7 I9 j0 m#define S21 5
. U, W- x' c! L4 ?#define S22 9
) s% O3 X' J% N0 {; y/ m. U7 r+ r #define S23 14
0 Z F' N5 Z6 k+ V0 d: F' d #define S24 20
' o+ y' K/ E) J- J/ E+ M #define S31 4
0 W$ R+ Z1 K5 X2 X. L#define S32 11
q8 J: j' s& R- H0 h #define S33 16
7 C& d' p& j+ t" | #define S34 23
3 P+ g: S4 u8 r" U7 q m#define S41 6
) w- x1 I [ k* L5 h9 o; W) | #define S42 10
0 g5 r: y, {! m0 ] #define S43 15
4 y/ f4 R2 y8 S5 S6 r- e# G# ~1 A8 Q#define S44 21
1 z# R' |- x9 i7 _* C( ~& _
: w% {3 Z, B1 _( M; ~ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
" L% k: t0 E4 X2 Y/ G' C; H, h) Bstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
9 j) W' t( q+ T- t5 W' c# B static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
/ n: }' Z# P# q; _7 r hstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ O$ O" e" Q3 ~/ T1 s0 F static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
/ ^' n3 F7 j/ K$ ]$ U
+ e4 W' {* l8 g. `+ ?$ ~1 f 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
. `3 ^. o7 ]% h5 l8 G8 S8 m* { };
1 Q. }" E' g( @ a. {
9 K* Y" M: B9 }1 m, J2 ? /* F, G, H and I are basic MD5 functions.
*/
! D1 X* a1 o' s: ?# J! [% G#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
4 ?' |) J5 N4 V) |; g e #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: E* H& v. ]& A1 g" ]- l1 ~. @#define H(x, y, z) ((x) ^ (y) ^ (z))
) W6 y# u% Q6 A#define I(x, y, z) ((y) ^ ((x) | (~z)))
2 z, A T' A" V- Z2 ]( y) n4 f3 X
+ [ n0 F8 e5 b; d( M6 y/* ROTATE_LEFT rotates x left n bits.
*/
! `; {7 k8 I u3 W' J#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
& V9 f3 c# {# I# i: p
( Y6 p0 r0 b$ p% v/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 E! o; c: G1 S1 p: {( C7 ~Rotation is separate from addition to prevent recomputation.
*/
, I# Q& E. P2 @; n1 F #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 [0 F* F6 |& G0 P B- d1 M#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
L3 q g r. d" K0 p( { #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 l7 f6 j+ i! |& c2 ^ #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ e3 V9 ]! O1 m2 v2 }
I$ L& D7 r0 L) S. |* N( h L /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
( A, S2 l, k$ ~5 W! p0 o' G1 Mvoid MD5Init (context)
" S) _+ k3 p! V4 d Z) l: @* L' x MD5_CTX *context; /* context */
2 d6 h- T1 M; q2 r# v3 j% @# {{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& T2 o5 B u; z/ y1 N0 F! W */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
0 } k4 C& z4 w" |}
- T g! Z% e2 j% \& n
; d# F* x4 D* d. y/ t$ u' { /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ T) V5 f5 ]. O; Q. R8 }void MD5Update (context, input, inputLen)
! S% f: N; M& S( i& |MD5_CTX *context; /* context */
) w. ~: E7 k: g8 H9 G9 W" I unsigned char *input; /* input block */
, [' D {+ ^. T( f' V% x2 ] unsigned int inputLen; /* length of input block */
2 @, j& D0 `( Y* @* M% {9 V {
unsigned int i, index, partLen;
" U! f( H) v1 s" _
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, q# r& J( f' l" \+ `
/* 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 |& n/ s& a9 ]
& S8 L" l- c! E' p- I# a( W
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-10 00:58 , Processed in 0.175139 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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