中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
! F7 i2 m; r, y) D#include "md5.h"
8 V$ E7 O" x! `) M5 r
5 ?6 `9 |5 i, B9 B" N+ M /* Constants for MD5Transform routine.
*/
9 G) Q, k) h5 u& N% r
& o u3 E0 q7 B0 R' g7 S
3 ?( v6 x% L) |- V& x1 r" l0 W#define S11 7
6 U/ Z9 t6 u3 d2 o, w+ V7 k$ ]#define S12 12
* Z" ? r* P3 b2 ^" ?% k$ J) j& r #define S13 17
; P* D \* f2 m* i& g' p# q0 T #define S14 22
9 l0 t1 h9 I* h. l; H: ?% r#define S21 5
) U) ]3 _" e) g#define S22 9
. p5 H2 x- m7 Y s% Q) u# R#define S23 14
' u5 ^ N: B U$ r H#define S24 20
) D8 Z6 Y9 c' n+ I% f. o#define S31 4
( p, t: K+ ^) L) Z8 [% _, { #define S32 11
* K' _# c$ t' n+ m- @ #define S33 16
* t6 a0 ~! v: d: z: F#define S34 23
6 Q9 t! x( J6 O8 v #define S41 6
) b( @3 X6 m! D2 x$ p#define S42 10
! e+ Z4 C4 o) T$ Y4 ]" v, Y#define S43 15
, [5 s0 d) m) F: P5 ~* f #define S44 21
7 g% i) }6 E" O% N/ E
4 H5 H9 z% j* t9 x$ `# X; @* f7 b9 f/ l' Nstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 N7 |, ~ |- U; R- P# w9 c sstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
8 d& m1 u) N* z( H* p. z8 pstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
1 C8 \& `+ _8 r. [" r6 F7 [static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
3 }+ S3 @" L" k/ l9 k static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
C! z: m6 y8 z, u( {8 Q3 N
8 L% [# [) Y5 a/ j4 S: L% Vstatic 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
# H% A8 Y' J- d};
1 x* \) [$ I, H* n, U8 ? B/ H
/ f! I# x5 C: S' L! u /* F, G, H and I are basic MD5 functions.
*/
7 _' }8 g. n6 m: G; A #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 i/ G+ [7 p% U$ N9 S3 O( z#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 s8 p3 d- F8 Z {#define H(x, y, z) ((x) ^ (y) ^ (z))
6 x" r6 b% `- _ #define I(x, y, z) ((y) ^ ((x) | (~z)))
& H% t% x% g2 D, g! g
7 n2 s1 V7 f3 j# P2 J/* ROTATE_LEFT rotates x left n bits.
*/
! T: d' s \7 ]% l#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! z8 U3 m6 P9 \$ R! D
) t4 N/ Q' U+ \) r9 i* M2 A; L /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
$ a0 q% W' y3 K( m$ y Rotation is separate from addition to prevent recomputation.
*/
- b J8 y) l3 Y/ d #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 o+ o8 ~: T C* 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); \
}
, ?7 }& o) O) _$ Z3 ]% ` #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ o4 d5 e' G) n3 B( K6 l#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) h# A, U) n8 `7 B; `
3 t. N/ Z( f& K& J/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
, N' {+ I7 D8 W4 F+ z: [# A void MD5Init (context)
5 q, J- K. j# s- X$ ^. xMD5_CTX *context; /* context */
! q% L3 ~% [, K% T' V+ N1 p {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, S3 m. b+ g' r' n5 Y ^0 z# L*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
5 V& W* q/ d0 m% W }
9 }" j0 K, p8 f/ B* @
1 [( o& G7 L5 @+ O0 q/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. ]/ {& T6 s2 |% M' d+ o void MD5Update (context, input, inputLen)
+ ~' N$ |" k2 t0 \+ S9 EMD5_CTX *context; /* context */
7 `5 W, `" ~) Y3 ]6 ~ unsigned char *input; /* input block */
: ^! q# P) @4 a) q7 ]$ G( M* |1 T unsigned int inputLen; /* length of input block */
: A( _! I8 b' p' m! w* j6 R{
unsigned int i, index, partLen;
2 i4 w. n1 F0 Z; f" _9 B" [. x
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ `7 R6 l3 W) o# J
/* 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++程序


( |; Q2 W. r5 I. \0 B: T2 [5 V
: f; c$ [9 Q" t; F
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-22 07:08 , Processed in 0.077641 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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