中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
4 L+ A4 v8 j4 S7 L% Y #include "md5.h"
- d7 i% q+ z5 c" u, S$ y% |
% K* g3 C- v D9 |" m/* Constants for MD5Transform routine.
*/
7 C/ C/ E3 X0 J. r% ]- y
. D2 d& `/ T$ b, t& D, E. ?
- k- N+ }2 G0 H1 b0 {#define S11 7
# x! \2 P, g4 m #define S12 12
6 p6 q2 l* H& j2 X- [: I1 e#define S13 17
. f5 t/ H# f' p L8 ]/ n #define S14 22
8 J: _1 v$ _8 O e#define S21 5
- Z5 v. I$ z+ Q- S& F #define S22 9
- I& H; E. p8 x! b' ~8 g3 w #define S23 14
; {/ j, w5 u2 ?' `& [& P* D5 } #define S24 20
- Y5 {9 _, H4 c* t8 @( n2 A#define S31 4
3 D# O/ ^8 I0 P: u7 z #define S32 11
0 w' e; n$ a$ U2 v! O#define S33 16
9 }- m% ?3 r2 A' K& f9 H$ P#define S34 23
6 J9 M1 w, N! n5 \#define S41 6
& l( Y7 C0 ?2 @. Z6 N/ G1 t( g. ]- X- A8 u#define S42 10
" |# p6 I) C7 k#define S43 15
/ B" X2 W/ H9 c' E#define S44 21
: s% _; @; |( p) h6 {0 A7 l
, K+ H$ m, e2 o. l. O' h3 `% V& P static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
3 i6 R- J Q9 n3 x T: Xstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
6 j! l5 o+ S0 o static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
+ Y4 g. s4 b+ R3 V& w" f5 Z7 O static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
3 _' F" X( s8 Jstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% z5 g4 M, ` E4 \3 y. o+ U
2 A* w* E3 g n1 ~3 B6 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
: W: {) n0 ?! F9 A. V! j5 G2 \: ~ };
+ U9 `) V- |+ _% S9 b
( D- g0 N7 B3 @5 p6 p/* F, G, H and I are basic MD5 functions.
*/
* P3 z6 Y% Q% @4 O #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# |$ o) A4 Y h9 @% y- `& A#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
. @7 o2 O7 }- [+ {#define H(x, y, z) ((x) ^ (y) ^ (z))
& b9 N) B. W6 { z7 I! @/ Q! w#define I(x, y, z) ((y) ^ ((x) | (~z)))
% A$ @0 f* U) S. `- v/ R; [
/ T( ~ A4 \3 ] x /* ROTATE_LEFT rotates x left n bits.
*/
/ s4 _* n3 [/ n/ c#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. \% @4 E8 J ^$ t9 ^8 e# U+ F7 J
( u$ i/ I: Z! l5 _* t /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 M- Z5 u2 f; _. s Rotation is separate from addition to prevent recomputation.
*/
" b# Z3 w6 P+ |# j: {7 ~& n' N3 X#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& I2 U3 |0 F#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
# d; |; w% J$ B#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ k2 M R e9 F#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 r8 o, \/ O' k! V t
4 y% _5 Z: q2 ]' R/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 s/ W4 d5 z! q void MD5Init (context)
9 O. C( s( K/ D3 R5 X" r1 ^ }+ k4 P% M MD5_CTX *context; /* context */
, _1 F! Z+ b% ^' s {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 w# \( p8 a9 m( n' ]- _' _ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 t5 G8 F8 @0 a9 v# r$ ~6 e6 v% R }
, l9 X( }- j! A' L* Q* U6 I* d3 T1 }
3 ]2 J8 U4 h* @- k/ Z* s3 [- h/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 Y5 f- _$ S- H" P/ kvoid MD5Update (context, input, inputLen)
, j7 |* m- u8 c/ A4 j. X# e) V# q MD5_CTX *context; /* context */
2 O8 m7 D& M0 ? unsigned char *input; /* input block */
) G3 x& D5 _5 m/ G/ Cunsigned int inputLen; /* length of input block */
, i+ P* p. N1 a# Z* K+ [/ {$ Y{
unsigned int i, index, partLen;
* R2 g. H; ]7 i6 ?
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
$ J* d8 ^. C! b
/* 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++程序


& P1 p" X/ J; u! I6 @4 ~1 e% Q
' Z9 [9 M- B$ U! B& b0 Y) u, I
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-12 12:27 , Processed in 0.056787 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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