中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
1 c2 H6 k. \! x8 H% `+ s i#include "md5.h"
( M+ p$ j, x% L, d6 k; [6 K
* W3 l+ |* q2 r2 W- W+ E8 ~5 `7 k/* Constants for MD5Transform routine.
*/
% c- p; y- P; E
0 z2 Z+ C u4 q5 i6 f7 y5 X r
3 Y9 b f% R" ]1 s #define S11 7
$ F) \3 t/ b2 v #define S12 12
# N# v* R; r+ P#define S13 17
+ |0 B5 G" w2 Q$ d5 Y& ?6 r3 D8 ~+ d#define S14 22
- _7 N" T! Q2 l7 t#define S21 5
& ~0 N8 I' {0 h& c# z#define S22 9
5 o% p0 P w8 G4 X5 p2 L/ f3 X #define S23 14
0 }; z' F, V2 w6 p8 M#define S24 20
* i( Y& L( V- t5 n! _: A" F#define S31 4
7 P# ?* P+ m0 u0 T) h #define S32 11
2 Y3 _/ B, d8 j3 e3 w; r#define S33 16
3 [; X+ K" ?; E #define S34 23
7 o* T; L" h P, s7 u/ Z$ a#define S41 6
- q2 s: l$ J2 j! y) L% |$ |: u) }#define S42 10
9 O7 F9 G& A1 o$ ?9 M #define S43 15
! t' C$ N+ ?! y9 Y5 J#define S44 21
. a! J. z3 s4 y
+ h6 Y3 g; w* `- p& E4 R) m static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
6 S! Z& z+ Q1 U+ W) s3 o0 {static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 u( r" [' l) v. g* B8 {8 hstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! x" @' g c6 L( | static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
7 T! M. `! ~6 G+ _, m8 m- dstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. V _" ?5 b3 a
3 b+ [( L c1 M 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
! V( l3 p4 v q) x6 Y4 |2 N! L };
, C) \# V: E: E: P3 g
8 T, D! J- e2 D2 ~ /* F, G, H and I are basic MD5 functions.
*/
: @% ?2 S, N7 m W9 Z#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
8 y2 g4 l+ [$ T# }. I #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% L' a* P8 d7 B6 ~6 Z+ P#define H(x, y, z) ((x) ^ (y) ^ (z))
* H; F$ u2 o' \) X1 Q #define I(x, y, z) ((y) ^ ((x) | (~z)))
5 d# M4 Y/ I6 E! O' Q& }
" F/ z& x" I. Y2 ^, ?! @ /* ROTATE_LEFT rotates x left n bits.
*/
! A7 G) d. H0 b) `6 c, O#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) \% D9 K, f G }# c
9 [* n& H/ X2 s /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
% V, s$ }+ o; ^7 v* ^& ~4 e; p Rotation is separate from addition to prevent recomputation.
*/
$ g) }4 I9 D2 t( E #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ U. ]$ D, u' y/ e- a #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) g2 C1 Z! |: z: E" b! H#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 i: m/ ~, P; x0 D/ S# N) A& d& ~ #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
w4 M( @- p, A
6 O$ w/ o0 r0 J- J5 }/ `) X0 |9 a8 j& c /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
+ \! Q# V/ b" I$ G$ O0 E: V void MD5Init (context)
1 m/ k/ D! L7 J vMD5_CTX *context; /* context */
0 S* d- c# u# }6 [, q; k' _5 n- x {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
, q/ c m6 e7 E& a! A */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 r1 [& S& q. h* b5 @}
p/ v4 g/ R4 \0 E% L# ^" |
( u" F! ?# U# @ _! |8 I$ _( \' E8 G8 g/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
4 c0 n" D. B; S! X# _( o% bvoid MD5Update (context, input, inputLen)
! s' O- \* B8 t( vMD5_CTX *context; /* context */
! v1 @6 u k* I" d3 E: Z unsigned char *input; /* input block */
/ m# G. d+ X$ P* {( }unsigned int inputLen; /* length of input block */
6 t+ G) ?, X8 ~7 y, N3 J; n6 ]{
unsigned int i, index, partLen;
; N* N* |% V7 L& }3 _4 H
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% e. H* ~ X2 X/ z
/* 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++程序


7 t0 K1 v; j t9 A3 S) d
" l5 \6 |0 S- y) c7 H
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-21 12:06 , Processed in 0.078214 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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