中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, B+ r) q0 e9 f. m #include "md5.h"
- K' f' R* G+ K/ h/ M6 L9 M# Z
* ]: V: L% N# f4 m9 g+ V /* Constants for MD5Transform routine.
*/
; z- E( X5 s: n+ f; M
; `1 z# ?/ R. _
# C6 r3 z( S2 B8 n* e#define S11 7
- w; n h, o% N0 G9 N6 z. R#define S12 12
' N) A! @% l3 B) { #define S13 17
! e( u" ]$ k" g7 y#define S14 22
$ x+ _# Q G: Y1 S #define S21 5
+ W8 ^' c8 l @0 \ #define S22 9
; O& @ M* E$ i; q5 N$ S! | #define S23 14
% x K! t5 T& `2 Y#define S24 20
# l3 L, e8 ?3 X# r) K#define S31 4
s, o+ q2 n: R6 B& c; T; G#define S32 11
3 q# }- y n9 b5 c$ b #define S33 16
$ ^+ ~& f& ?5 ]9 _! ~2 S& v #define S34 23
( l- Z9 V# P. \: a% b; L#define S41 6
3 r& P3 U) z* j2 I% x#define S42 10
3 o% j3 ]* P" p+ l. ?#define S43 15
! O) ^% K! ]- n# ?" H9 i! r #define S44 21
@) d5 s3 Y- S- X- {7 }) d
+ i2 `0 N6 U; _ static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
; e" w; _' A; F static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
+ j0 m1 d0 ], V7 ^static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& f0 F# k9 \+ w6 Q ?static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
* G/ B, ^- H- t4 \9 r+ zstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
0 _. N3 I! }! p& |- g& ~, l8 m
, l, o& U4 l8 y9 L/ lstatic 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
' Y1 D* t, n2 W0 i; r };
( c4 t" h& R: m/ v& L- O
( A$ @/ d) z" \0 U, z, j6 z/* F, G, H and I are basic MD5 functions.
*/
' m; S1 y* o% I3 N1 Y#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; v* n) H2 D( `# r8 r; S#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7 Z5 w( h3 u, |: i#define H(x, y, z) ((x) ^ (y) ^ (z))
# |% E1 ^, ?! c( P" {& J7 V. s2 F! O#define I(x, y, z) ((y) ^ ((x) | (~z)))
( f" j2 c) X6 s, |0 S
7 p3 I/ ?% r8 n5 X* l, G/* ROTATE_LEFT rotates x left n bits.
*/
* T; _" O' p- o5 E#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/ Y! K, v# ]! Z# i5 y
/ c6 C* a N d8 Z! L+ C8 }# M/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
: }: ~' b0 F& y* ` Rotation is separate from addition to prevent recomputation.
*/
% f7 h% p2 _7 W R3 V2 P: b3 J#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& n, E8 C! e2 g7 U7 q #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. f% {( w5 h2 ]$ 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); \
}
# |1 B' a- U! B0 p#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ Q, R3 z* ], d7 z7 J, B( l
u" n) h+ Y$ x7 D/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
% r, k: i0 D2 _void MD5Init (context)
m q( v1 d4 B MD5_CTX *context; /* context */
4 ^& x6 G* j4 I{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 n, t Q9 K' C */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! T: i$ v# t, a" J) L }
: ?- y, @$ ?4 K/ j- }0 u! E+ }/ n
3 D+ \5 |' Q8 K5 S5 v6 u2 j /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
, Y) o6 z. j$ S* a5 Mvoid MD5Update (context, input, inputLen)
1 L/ e- d# F* {* VMD5_CTX *context; /* context */
7 W# N# i) i/ `5 h' J0 K9 t unsigned char *input; /* input block */
$ [# \' B; Q- ?6 G unsigned int inputLen; /* length of input block */
) N2 B: k; u. i) f2 N{
unsigned int i, index, partLen;
# \3 ^# o& O& Z# [. K" t
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
& r0 K5 e; K( h
/* 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++程序


5 @" p9 [- M+ p
- i' l, Z; ~' {, g) _% M. e. j
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-21 18:17 , Processed in 0.158912 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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