中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ T" a4 Q+ y! a0 x! K3 _. m* f#include "md5.h"
& X" n1 N# t; D* m! t3 K
9 z: t7 H; {6 F. Z# `/* Constants for MD5Transform routine.
*/
5 U, L0 @/ r j( {% i5 K
" u7 s1 k$ o, Y; e8 c2 }' G
! A+ w% x4 b. e( s! N #define S11 7
# K3 |/ h w. {$ t( g$ Q #define S12 12
. _ @1 J: n/ |& ~$ n3 Y #define S13 17
4 F, x! m' j; ?5 x4 M& k #define S14 22
' c7 N" w! S+ W- P1 B: z#define S21 5
8 [0 w b& A- i8 R) W& Q9 e& x#define S22 9
: s |* _: c& r3 ~* C2 g/ F/ F/ s' e #define S23 14
2 y+ ?4 \+ T$ U' U1 M#define S24 20
+ Y, c/ z/ _) t3 I3 \) r o. Z9 D#define S31 4
$ J U$ k' h3 D* S" L #define S32 11
* M& _5 T6 R3 a#define S33 16
& M# \" v5 g- B #define S34 23
$ T3 n- d" f: {; _6 R#define S41 6
6 i) o( _+ d# h( O#define S42 10
. b8 E" S! j b) _) u5 v; q' {#define S43 15
) {0 f a7 b$ N+ W' \$ W6 w9 m#define S44 21
( f% R! ]- x, g7 {! k7 K
0 K0 g+ X, C ]; n! pstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
" c; q2 S, S+ s+ u static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 v. [6 i' l7 _1 Cstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: W6 e9 V8 \: U0 l/ o" |. b" @static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! l3 Q+ O+ w- v; a3 qstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# m7 [' b7 ^. {; H' d! Z
1 I' @2 ` b3 W6 k# j2 W4 r1 a; k$ `, dstatic 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
# R& A) m: r0 B% D4 H) x6 B2 c) O};
; _$ ^4 c! [" j- Y# H/ s! L9 D
6 @! _, |3 W9 r! j, d8 j /* F, G, H and I are basic MD5 functions.
*/
8 ^6 ^ x5 M6 i# s$ `+ e/ E0 o #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 W6 L C: n3 j, @1 S3 s* z; ^#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
8 R; c* w3 _! ~1 r" Q; H#define H(x, y, z) ((x) ^ (y) ^ (z))
/ z( i/ G6 [8 |1 k- @#define I(x, y, z) ((y) ^ ((x) | (~z)))
) @% \3 |% Q9 B% N4 Z8 ^$ E- K. Q
- `3 u. }6 g6 V6 h/ Q* L/* ROTATE_LEFT rotates x left n bits.
*/
4 }9 P/ l; [5 i1 U7 t) v8 x; a #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
5 b* a, I3 M6 d) {6 e) K
1 k( a* C- ]6 G( s4 X, h /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* V. {9 F7 e9 }& \" | Rotation is separate from addition to prevent recomputation.
*/
, L* I1 ]9 C# 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); \
}
" D5 g, G; h" r" u- [3 p, l E#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* ^* J$ L3 a5 V7 @: ?#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
H2 Z$ `7 `) S6 `2 C; N3 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); \
}
" E; j; j, Y6 \ R" P, g, E* {1 @/ T
/ ?9 G, U% M8 P/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
% `) V9 b9 A jvoid MD5Init (context)
6 i( h2 V/ ?# ^' N& NMD5_CTX *context; /* context */
8 d5 s) @& c. H3 W' e9 I{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
* A2 ?" ~7 p5 G */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! v6 ~6 `: g* j}
$ }! s6 U {: I2 x, m/ X3 q1 K$ V
7 h' ~) H8 u( Z. M/ u/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, x5 c3 h6 c0 o3 ^ void MD5Update (context, input, inputLen)
E8 M% \. x+ F8 r ` MD5_CTX *context; /* context */
4 v3 {; B8 f2 l1 ~; H; U unsigned char *input; /* input block */
/ t/ N0 O0 B6 I3 O! Yunsigned int inputLen; /* length of input block */
% |# X# d' _. _3 q: } {
unsigned int i, index, partLen;
- d3 h8 I- y/ I: p; ?( d
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
) [9 g2 X$ I. u. h. t |9 R- a
/* 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++程序


2 | l; h; \( G$ A
1 G* U( i- [1 X/ n# Y8 I
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-23 21:55 , Processed in 0.060956 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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