中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 {8 J( c& F4 C #include "md5.h"
/ j [ W+ j O) q# l$ d6 B0 [
$ O% j5 A% D! l# S /* Constants for MD5Transform routine.
*/
, [4 z% L0 ~' [
7 j2 Y3 e0 X" Q: Q3 ~
2 D1 N8 E/ s4 s9 B. ]. q% m1 s#define S11 7
9 D7 \2 L( H( K+ G #define S12 12
& C8 a$ J1 d/ F5 \. S6 M5 w#define S13 17
' G/ e. h5 j8 v, C2 C: ?9 g) f- S+ \ #define S14 22
5 y" \2 a2 X V9 [6 ~6 H #define S21 5
+ q& @ v+ W, P( ? #define S22 9
: z1 k, B$ P$ y, o3 q$ I #define S23 14
+ V( T5 V( F" k#define S24 20
- m! j, k$ Z6 u2 H( A+ w9 M g#define S31 4
9 f" F6 d7 m! c" S; l" j#define S32 11
* z5 W _6 K7 f2 r#define S33 16
2 ?' a6 j& [: x( N, x k5 w #define S34 23
5 d0 N$ q* Y4 j- P* ] #define S41 6
8 m% u0 w3 \/ ^# X, q- w #define S42 10
7 D0 ]/ |* ]% L( Y #define S43 15
. r/ p& O; s. Z5 I#define S44 21
) D9 s6 k- F6 b! n$ p' J0 J( x
0 r% D, u' P; @. {3 m/ H; a! [ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ o3 f! x9 n1 k7 x# q! A. O" o static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 Z3 g r% C$ B( n+ astatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
; E, B: s& O" o0 n' I static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# B1 u$ r$ r# K+ u" _% [% w static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, a# Y- t! T9 | r2 h. Q
& ?0 P+ r" s. Astatic 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 q1 T# P7 u. ?- e) ?% c$ M% x};
- U! g% T% S) d j
- x) c1 a. G1 {! B& q% n/* F, G, H and I are basic MD5 functions.
*/
: y+ P7 \$ S2 c/ a#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# a4 ~* R3 O8 S! F2 j#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
9 F, S4 s% Q' p7 n! | #define H(x, y, z) ((x) ^ (y) ^ (z))
; y% E# Q1 D6 B5 |) q; K#define I(x, y, z) ((y) ^ ((x) | (~z)))
' B; j& k' v4 R; D* X: \4 y* q5 y
! K G3 {0 e- l9 I/ \; t8 n /* ROTATE_LEFT rotates x left n bits.
*/
6 A) P& z3 Y6 V2 o! B #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. ]1 v2 U$ ^8 G `
; g/ L; [* ?1 w% d/ \9 q" o' E/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; y8 s0 N! }# Z) \0 [ K( J ORotation is separate from addition to prevent recomputation.
*/
0 n5 Q& w* A+ q #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& F5 a2 {) o1 ~, h1 D b1 R#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 z: f% @. V9 q) ?#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% B6 @9 l& m0 s, f #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- u4 b6 k: p: g+ ^- X
3 R- }6 X: h. x% Y+ U6 { /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
8 [% T4 x5 n0 W7 u m5 Z$ g* @ void MD5Init (context)
( f, n: B- K i3 KMD5_CTX *context; /* context */
1 m6 q- `5 m: f' E{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 N, i8 i6 ]! B */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
3 B' i) k& k. t6 z" q; L}
, P; M4 s/ H) I7 A3 ~ s [
: ]6 M( b' c3 Y! r3 X/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
5 l2 s6 C, x# Z+ `. p" t" _4 evoid MD5Update (context, input, inputLen)
4 f- ]* F4 ?6 L0 SMD5_CTX *context; /* context */
: i# c+ G- I& U0 Z: j unsigned char *input; /* input block */
( M4 N y% _, f% [unsigned int inputLen; /* length of input block */
$ L/ Y. P0 x% M( s4 Z {
unsigned int i, index, partLen;
; b, F% V t' T
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 X8 q$ U" k; {
/* 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++程序


3 M0 L) C6 ]* b/ B3 z3 @9 k
9 z# j- P! u8 w2 d( d: V
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-22 19:11 , Processed in 0.059279 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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