中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 S: n* z8 r) |9 l0 n$ ~: @, b" \#include "md5.h"
; [, Z, X' N: v! S2 Z$ t
( X4 f) u1 I& u; p# s6 R8 C /* Constants for MD5Transform routine.
*/
# f+ v2 Q5 v1 z8 Y+ r d2 R
8 \8 V W$ q" w/ Y. [+ G6 m
! S) B. I# J1 g" h$ E/ G #define S11 7
+ w8 ?3 }1 Z/ _8 w6 r #define S12 12
2 q3 h+ L2 ^8 E M4 X1 c; J#define S13 17
1 s! s6 U2 f5 \" g$ V4 \#define S14 22
) `* B' O! [! y. H- j" _1 t3 `3 N# T #define S21 5
( v$ x6 Q' ^* \/ G6 [$ F5 X#define S22 9
2 U0 w7 ?" ^- s; U# i; Z3 D/ B. I# f #define S23 14
/ B* J; { P, c& v' s8 D: O# h #define S24 20
" _2 E5 i8 ^4 i+ @) ^3 t: C; @3 G#define S31 4
+ ^4 q- j; T- J) B: p2 i #define S32 11
2 ?" r) H% T7 Y) o9 {9 P# |$ K#define S33 16
) a' R. [& g7 C# K #define S34 23
4 b1 m* l3 J* }$ M0 \ H$ C #define S41 6
; v7 A$ m0 d7 R; ~#define S42 10
6 `% ^6 g* q; _, |5 q T: b& ^#define S43 15
/ V6 Q5 a. Q2 x2 w" ]6 i; o; e3 e3 [#define S44 21
, V7 h0 b: Q" |- F: B3 I* U% c
- b, J* y3 h* ?' `, G8 bstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 q" [; u3 e& g+ W% d" ]% \ static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) f4 B$ m- @$ v" o% S) M- @ static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
8 V# p' z2 _& a. H1 t9 B6 R0 g$ N9 Wstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# b8 i* O: `7 @) y" j static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
1 Y& V6 v* t+ n4 a
" ?% x9 M0 y4 m* n: O8 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
0 O, ?& z/ \, l };
* J$ G1 K% q+ f) J% R- L
- h* `: n2 j+ z7 q @/* F, G, H and I are basic MD5 functions.
*/
4 x/ D3 j' [6 x% W$ O& \ #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, j+ B" ~5 f/ H' S1 b$ @#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
# ~) L9 Y- n( F. v#define H(x, y, z) ((x) ^ (y) ^ (z))
, x+ E j0 j% H: L+ W1 f#define I(x, y, z) ((y) ^ ((x) | (~z)))
4 s- B( ?; Y4 \3 I0 W6 `! y
: V' v/ j) {, }( n! l& U8 A+ y4 @/* ROTATE_LEFT rotates x left n bits.
*/
" l( q! A9 O; V( G7 e4 T( G9 Y#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
$ S) j4 m8 Q: w# B
7 c' N8 |7 Q- P1 B. I. Z /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- l3 K7 y z9 ?% T1 T8 b8 f" U* ?Rotation is separate from addition to prevent recomputation.
*/
/ T/ r! P- Z6 E8 E1 c#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. H$ t, W% ?) e7 i0 s #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 ~* P' h' n" t; O6 Y- v #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& [. b9 E; |- M& r #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ b: y& q/ q" F3 K9 Q4 q7 m8 H
! E1 F) U$ d. o5 X% a/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
& }* o% s$ |1 U* Xvoid MD5Init (context)
& l- ]! g% [( Z, e" A2 X MD5_CTX *context; /* context */
* c+ i0 T2 `; Z0 F) k* e1 q, ^ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# I. E9 k0 k2 G3 N*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 x0 p I5 p0 G }
. p4 M- |2 x6 m+ V2 S
# W2 ~( |; Z3 j# y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
0 l6 T, O& @: T! Y9 e; ?) S void MD5Update (context, input, inputLen)
8 B; V2 |$ @' S. k3 nMD5_CTX *context; /* context */
1 i: `, F* B0 L5 S$ s/ y unsigned char *input; /* input block */
, J4 ?0 I9 \& h/ K unsigned int inputLen; /* length of input block */
M" q1 U- Z" a5 r1 K' g {
unsigned int i, index, partLen;
+ `' U7 v9 L. \
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- Q4 o: |) r2 ^ B) |, A8 n- p
/* 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++程序


5 w, J L1 o- a2 H4 B% \' Z
E5 J/ w C' c7 I' y
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-20 05:32 , Processed in 0.057096 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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