中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
8 C/ S7 [9 p$ i' i& K+ w #include "md5.h"
/ }8 W4 w1 o ]& V: G& [9 D7 h. H
- | m# J. r6 O( S/* Constants for MD5Transform routine.
*/
4 Q0 |$ C/ ?% a0 @% d
! c& r8 ]9 b7 X# b0 X/ \
5 `' w$ V }8 W #define S11 7
; u( i( F$ M) ^# k; x! e #define S12 12
V# Z' T0 r2 B; r |; N3 i+ Q#define S13 17
/ Z3 b5 N( z7 x& ~3 B#define S14 22
p5 \, I1 {, {; }, D0 K #define S21 5
5 X' x* S, F$ c- K) @& p #define S22 9
3 ^4 I; @: ]2 O+ J) c: ?/ f#define S23 14
1 ^7 [* q% Q2 m' ]; V; \ #define S24 20
: R6 o9 A7 R3 Q #define S31 4
3 {, X- H0 j+ Q( h9 z% s- {+ ] #define S32 11
1 v2 N( c& R6 S5 r. Q1 _#define S33 16
# h0 B0 e. `9 H0 M2 K- z9 C! q #define S34 23
! o" j, D: {1 \2 g9 t. t% y$ }" g! u #define S41 6
) e$ N0 O9 G: n$ u1 c#define S42 10
5 x% P) u; ~$ k8 B }. m #define S43 15
: }' P( B" q* t6 ^4 ]2 E #define S44 21
& B; u4 F0 f3 k
: v" W0 w8 W/ R: M, ystatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 ]0 i9 t9 W5 B: j0 G static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
0 n4 E( w6 r+ k! N% z6 D& z s static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ [* P# C+ p7 f) J7 Jstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
. j. l0 I$ m( \' V. u& @. |static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
1 `3 k$ [% ]* d/ F
& C- _3 Q8 n1 i1 y0 R' G8 C 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
+ |/ U1 g/ e2 T* x. h };
& D: v$ k- z; U# C& r. ]( B
5 Y6 S" U' T6 Y4 C. \8 F5 i/* F, G, H and I are basic MD5 functions.
*/
3 c. e3 b; ]" f7 W: x #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
% y2 a1 t% T) Q! I7 @% q z O7 W#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( v9 ?0 }" p- q#define H(x, y, z) ((x) ^ (y) ^ (z))
/ z# T1 V( K$ W* y" u5 p#define I(x, y, z) ((y) ^ ((x) | (~z)))
& }# k) v$ O. f& [8 o j) g
+ \% n* d7 r) E/ ~" s8 h- J- e/* ROTATE_LEFT rotates x left n bits.
*/
4 J$ y6 H. g6 P( c$ m9 p#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
' z! l" z) Y% ?: w3 a
3 D, V( }2 e, ]$ I2 \7 ~+ O /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
% S6 L3 u( a7 q# qRotation is separate from addition to prevent recomputation.
*/
' \) f# U. l7 W#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 N% B% s/ C4 w c" w9 \ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 w O, o1 {0 ], u#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 s1 s% F5 d- R- K0 w#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 V% K( V, C7 f0 z
& S! }4 \: P ]2 E7 L* v) x/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
7 k0 a) R$ M* R void MD5Init (context)
9 O; G& @ p/ M3 q% ]% xMD5_CTX *context; /* context */
' R1 Q3 K: @8 A) f {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
: L% a3 U2 k6 J6 S% Y7 @ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 b8 o5 U, D' @0 x s+ o- K}
7 J$ I+ S" z( e$ K5 i) n% ~2 Z
4 m6 L# l& N& K, s# I- y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
6 y4 G; I" l T M8 h# P void MD5Update (context, input, inputLen)
4 H5 W; G/ f+ E MD5_CTX *context; /* context */
6 v9 Q% W% J9 V) m/ L U) B7 R unsigned char *input; /* input block */
) G+ ]" ~6 ^- z8 c* y5 E unsigned int inputLen; /* length of input block */
+ F+ z2 J/ g" z: h# M5 Z3 H+ R{
unsigned int i, index, partLen;
1 |" r( U% F) F O: | d9 B8 s
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- K. z* Y, z2 g6 A9 X
/* 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++程序


$ X, U7 z h/ E
4 w* C. i' Y& ~% y# W
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-21 16:08 , Processed in 0.060606 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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