中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. }! u6 h+ R3 _; s0 E2 S #include "md5.h"
: D, H- ] i% n9 V
% i# `& {' `% C- a7 d /* Constants for MD5Transform routine.
*/
" k) [# R6 R1 L; R& K
+ r6 t' F) Y! \5 `' J
% t) [' B1 q% p6 T#define S11 7
9 N# j# P, O2 D( c. ]# w #define S12 12
# p' I7 H0 u: |; p5 }#define S13 17
! `) t0 I! e: `" ^; q#define S14 22
& a5 m* n: q7 F( u1 W, P4 T#define S21 5
" q( S0 ]/ Q# {9 P3 @#define S22 9
) D% `! w9 R+ R/ M( z3 ] #define S23 14
0 l+ y% a1 f9 N8 E4 S& ]#define S24 20
7 Q# K$ ~* j" o7 F$ b#define S31 4
. r) A# ^( X& J0 B; E #define S32 11
+ y2 b: _0 b. A7 T #define S33 16
: _* }0 f5 K$ x, f5 ~0 P7 D #define S34 23
% }5 y8 ~! M5 Y) ^2 [/ o3 P #define S41 6
6 }" T! q% w6 U7 P5 N #define S42 10
6 w- b( f$ [7 O5 ^- k% p #define S43 15
, w% j2 N5 P& M #define S44 21
: m1 y6 ]1 L4 v9 b1 J- L {
. F" t( R7 z# ?5 J2 j: Vstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
, z `' t! O* h7 P Y static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
# p6 n2 M% P& o- j static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
4 W7 }, {2 h% T! a0 z. @, k static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" f6 ^' [+ O+ ^+ c/ N& b2 w) b static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
- c+ l9 l+ P" U5 r7 j6 _
. s2 @. U$ I# u* B+ P5 [$ I1 Z/ @5 P static 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
- B0 i0 A5 j6 D4 g3 b5 x) J$ o};
; {$ r- x8 c7 }
' h7 b/ v* ]/ n, c" s0 |/* F, G, H and I are basic MD5 functions.
*/
+ ]3 g9 g& u: G2 j7 c+ ?, `7 q #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; m. R/ b# Q, r0 H% G #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
- j! y& H' _# ~! `1 w- d #define H(x, y, z) ((x) ^ (y) ^ (z))
3 K5 U6 y6 M' ~! J5 W0 Z9 u! }% g #define I(x, y, z) ((y) ^ ((x) | (~z)))
( t5 j1 Y j; G
4 x( }" J+ O p/* ROTATE_LEFT rotates x left n bits.
*/
& h% U5 Z" P3 R& U# N9 i#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
% `" G/ j' b |$ j
( q4 I- H: _( W& _+ X: N+ Q, h/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 @% N5 ^" g3 ^2 S' s$ p4 T3 i4 S7 MRotation is separate from addition to prevent recomputation.
*/
/ O4 \2 m0 S, z8 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); \
}
0 ~, q, N4 M; Y5 M #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' K* i" d0 k; j, i/ f #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 z& U2 L2 i- F1 m#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 ]6 r1 R% T3 S# R, y' P
! @( K& ?7 \4 j/ L# l: u/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
7 P) R4 X5 U3 J+ E void MD5Init (context)
" v: d% L% Y8 d! {0 e. ^ MD5_CTX *context; /* context */
; ?, ?! X' `% g, o6 D- W3 C# V8 j {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
/ U- T' L) J; b$ U( t# T*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 U' r" ]: I6 w. b" W}
* v2 \* J) U* i1 X0 a
: [* M7 k% { O3 `6 c; o8 A /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" M3 u" f. w' I8 q0 H, {( ovoid MD5Update (context, input, inputLen)
2 ^+ H5 H/ X7 | MD5_CTX *context; /* context */
1 {- V; t; R6 b unsigned char *input; /* input block */
1 G9 }; c( j. t6 P0 z unsigned int inputLen; /* length of input block */
7 X# f# z k$ @1 m8 v- G$ c {
unsigned int i, index, partLen;
& i3 l3 y6 u& P+ }
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
. s) q2 m& K$ I/ X
/* 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 ^9 G" m7 j3 b
0 J& F" s: s. B, W; q3 K; q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-21 05:36 , Processed in 0.070168 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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