中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; U3 b4 H9 _2 l/ a( H) C- J3 ^ #include "md5.h"
: \& @1 d3 w2 x) m w
& E W1 ~! ~$ k, J/* Constants for MD5Transform routine.
*/
. w+ h& k, D' T: u
7 z9 L! g- X& t& I- K9 K0 R
+ |; l! m, x9 E#define S11 7
( O* l% B( `+ K0 K/ ]- ^4 t5 ]#define S12 12
- I7 \3 s( [0 z #define S13 17
' y6 ?" P/ P, `#define S14 22
9 q9 N1 I1 F2 m- }0 d: {) W7 i#define S21 5
- Q" a. d" q$ q* G: ]- {9 y#define S22 9
2 o9 |2 E# U5 q# d #define S23 14
7 ]4 D/ J. s# F4 G$ T: w#define S24 20
. U8 ` X, y3 M/ Q# I- }7 z #define S31 4
5 ~1 r* z+ \. H/ o9 @1 j#define S32 11
$ L: w" b [% s- X1 i! t #define S33 16
( X! e" [; N9 n; f2 x6 G #define S34 23
7 n' g5 c8 B, q$ e* R2 O4 r$ b: L #define S41 6
: p0 N1 _& v, E1 \( g( Z/ i#define S42 10
8 f9 U1 c4 e# ]+ `9 A, g#define S43 15
( Q0 F8 q' A6 T. i4 Q4 x8 l0 z2 D( [#define S44 21
3 a0 V7 e; ^, n" q
0 f! N0 I/ M& q9 ]# Qstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ U2 J f0 ~5 a0 M0 L1 ustatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 i: h9 D' V" ^3 j% h: G static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 {1 Q1 a* P/ P4 H$ lstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 \! m% U+ g$ t# U+ J$ F0 Tstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
- h* E* P9 u2 J5 B' `" ~( f
5 B, `2 u" P, ~: h/ qstatic 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
' d; d$ o- V, @ };
7 C, W, f9 S) s! S
1 ]. }7 p5 _& ?* Z. D B /* F, G, H and I are basic MD5 functions.
*/
) }" }+ N. D; L; c! }8 B% G3 f8 N- W, } #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
) B$ ]/ [# E/ ?, Q, g8 w#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ s# |/ h2 \5 |4 _/ O#define H(x, y, z) ((x) ^ (y) ^ (z))
- b) Q% L! g- A #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 |# G0 C3 |$ Y% Q" N4 i+ _$ K
; o: U; e) c( C/* ROTATE_LEFT rotates x left n bits.
*/
. r1 t. M( L( ~$ y' W& D #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! ~. w( @- l: c/ V/ `2 h
$ Y# B9 q; u; Y: O& h /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: h6 r- m9 M1 p; X) v2 iRotation is separate from addition to prevent recomputation.
*/
1 w3 T5 p0 h9 n #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; O/ Q$ G. m5 c; u6 z( p#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) ~( I3 `; n1 N #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 {6 z/ u' `: x0 C% Z4 g6 j #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ I# w. e6 z; m! k
: L* g/ Z# y0 q/ J/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
- P! v2 n! d; J- t' m) N void MD5Init (context)
- t4 z+ w6 W, x! Q1 L) A MD5_CTX *context; /* context */
c7 m1 |' {9 ]$ {- p- A& }{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) O3 y3 Y) c# w) E) f) B*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
' [4 T/ Z4 s% G) r" V }
* M5 }$ k5 T3 f
) W* t) K s `: k+ D3 z$ Z/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 P7 Q# i3 Z. hvoid MD5Update (context, input, inputLen)
- N) J: S4 V3 n% D MD5_CTX *context; /* context */
+ Q- O8 ~7 J( y8 c. w unsigned char *input; /* input block */
0 J3 R/ a2 L+ i unsigned int inputLen; /* length of input block */
3 K6 {5 ]/ A" v) V& r" H {
unsigned int i, index, partLen;
1 ^% _* N; x/ t3 M1 j, ?; }
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/ a& N" f3 Q7 ]3 `; d+ R) y
/* 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++程序


- R! W- m$ J( s9 U; |
; k/ e( O' s% _0 u0 ]9 g
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-30 02:35 , Processed in 0.190278 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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