中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
+ v7 j6 v) ^& g, [0 _, S3 d% B- Y#include "md5.h"
8 X* i+ o' u9 H/ B+ ]' H9 i
% I+ {' D/ P& N& S, ?/* Constants for MD5Transform routine.
*/
" _5 A4 E) D. Y* C/ A9 y* C
( |0 }) Z+ W/ d' Z7 V+ \0 L/ z# A
T0 y6 Q1 U, b6 s; ~) n #define S11 7
! e- A9 K+ W8 g2 j6 t+ r9 W: \#define S12 12
: f; @1 O- z( P5 m, L! a* J$ u #define S13 17
- ]3 G" G1 l9 v. { #define S14 22
6 [5 N9 x" s ^1 ~: i, g: B#define S21 5
$ M; ]- z% t0 b #define S22 9
: j: ~" n7 z% W#define S23 14
8 M- Z7 a6 K/ l3 g% A#define S24 20
' y- c0 M# r2 k" q1 ]5 o7 h: z#define S31 4
( Q3 N9 V' m* o* p% M" S! ~& N' A #define S32 11
4 `+ a" L* Q Q#define S33 16
4 W# ~' S1 G" G# v$ `9 `8 r #define S34 23
; u' }5 {; s" |6 Q1 U4 i8 Y0 E #define S41 6
+ Y4 g8 G6 ]! ?. b) Z! e#define S42 10
1 @, p, w9 \5 T8 t+ d0 Z#define S43 15
. w% G3 D. y: j: p#define S44 21
7 v9 |6 h; b; x' ~) I2 `6 }
( g! [: f9 w2 s5 k( f! ~6 H, e6 Rstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, k6 F3 L4 E G! Z& } static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ c; _. E/ E+ f6 Vstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 c" J! i& A7 q static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
w- M( ^3 K6 K2 G# {3 O% ?; sstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
3 ]; E' G+ q/ N' d7 ^
5 u4 s7 e' s' r2 S- Q6 u6 hstatic 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
9 H \# s$ W% I9 \ };
5 f J' b& d2 e* r1 ^* g
9 w+ n0 J7 o* N+ [ /* F, G, H and I are basic MD5 functions.
*/
$ z6 d$ f0 }$ r! X" N' A#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
0 r% q) p' ~" x4 U( c' O2 ? #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) \7 g& B& f% i2 \/ \1 }; q #define H(x, y, z) ((x) ^ (y) ^ (z))
* _ v$ }6 _7 L% n7 n' D#define I(x, y, z) ((y) ^ ((x) | (~z)))
% D4 t$ C6 A/ p* G+ p
" d' k1 Y6 v/ N, ]/ u/ v4 F" B8 m /* ROTATE_LEFT rotates x left n bits.
*/
7 a J7 G( C/ @! @4 q* C; ^2 H#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
; D. D& A8 ^7 h% H9 B6 e, Y& [
$ {% x6 m+ x! ?! a6 T. P/ r4 I /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
2 D: T, B+ Q* v% v, m Rotation is separate from addition to prevent recomputation.
*/
+ B/ Z. z- q2 N0 E#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& |- r8 p, y$ B7 h0 i- A* S& G I#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 V! Z8 X& l- e3 ^4 |/ 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); \
}
/ A" a+ c, h! u# M2 j) B! 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); \
}
5 V' {" o b: v; o/ H* L5 a# x: G, m: q
I1 @ R9 O4 N$ r- O /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' @) c) D2 K G) S3 Z7 O9 C void MD5Init (context)
/ D! i) \! T, z2 J MD5_CTX *context; /* context */
# U1 H) i8 O3 Z/ {2 h, V+ }4 e{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" a' z% {2 W. B*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 K6 r2 z p8 T}
5 W/ |7 R* W3 C; x1 D4 U/ c% D% d
; m3 L5 U' {0 l; J% @; x; J /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 w5 l1 i( H, \- @ void MD5Update (context, input, inputLen)
: q2 P7 E8 z0 d# U3 g7 \MD5_CTX *context; /* context */
; S( Q" ^5 t o& \+ ^unsigned char *input; /* input block */
3 Z0 v3 F1 g+ hunsigned int inputLen; /* length of input block */
. ~: z/ X( N# r+ N! O+ T{
unsigned int i, index, partLen;
( z$ K3 ~9 e& E5 e9 [1 e' e
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
9 c: d& V1 O# ~6 X! S6 h3 |7 k5 M2 ~; t
/* 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) Q* O; \3 O! C
2 W1 X/ q2 t& |5 ^9 T
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-7 00:03 , Processed in 0.077007 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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