中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. Z7 d4 l0 f/ Z4 z7 n7 ` #include "md5.h"
" `. z3 R, Y$ U% ]8 o+ o: t; m
2 K! f [/ {) s* G /* Constants for MD5Transform routine.
*/
: k$ P1 i& [) Y6 j+ {7 ]
7 ?2 w" f1 j1 T* x! f G/ N: X% V
1 }6 V6 i) Q* x4 k# Z, i #define S11 7
& j. \' e' [1 W#define S12 12
# e" `+ c! H' Z! T #define S13 17
# P V7 W9 |2 Z4 E1 N#define S14 22
: r( {; O1 h6 u#define S21 5
+ `$ y; s6 }; @" ^8 P #define S22 9
* w/ p1 H7 Y/ E) F' q" Y1 T#define S23 14
* r. v$ `$ {/ b0 L#define S24 20
- z% n7 @/ _; R" M#define S31 4
: w6 N5 v/ b# H) A4 _8 L. @% @/ v q #define S32 11
7 E% U) i: E; f' _! y1 C+ S4 f( | #define S33 16
9 g+ R9 I# u1 `#define S34 23
! w+ x/ j, Z+ M7 E% c$ J! J #define S41 6
* s% A; @! j, m7 o$ ` #define S42 10
, {8 A: P1 P$ h4 ~" m( y# h, s7 i% k#define S43 15
/ Y7 c1 C# C; `! E& W# l #define S44 21
* k' J/ ^" Y0 r$ n+ {. ~6 n
# [% k7 x8 v/ t* s: J4 _static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
% g$ J! R9 z* \: T, e* `1 x static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 R8 _2 i: j% _- R8 Y! j static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 \& w. U. s: @; Y% C5 |5 @3 Istatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# q& O/ S& m: u1 s% q Q8 C; p static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
K. O* |# E7 J o2 Y F
) q0 W0 [/ t$ `8 }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
) P; S0 w; Y* x1 w };
/ a8 s' s; V/ l1 t; ^: ?
3 |* U' D9 c _1 u3 v /* F, G, H and I are basic MD5 functions.
*/
0 l4 n% ~3 V( w5 t #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ R6 F" m6 Q1 B( N7 w+ C' Y& R' D #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) U x. m( V- g4 X#define H(x, y, z) ((x) ^ (y) ^ (z))
; u. f7 t, M! C' E/ M( D( b #define I(x, y, z) ((y) ^ ((x) | (~z)))
. d* V! P9 d: J) A0 S
7 Q( d/ [# B1 ?- z3 `6 o2 N' V/* ROTATE_LEFT rotates x left n bits.
*/
8 b% e7 k. u3 w' H V- \#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
0 W+ r( T$ q- M6 G7 ^
; Z$ E8 {! \7 v; u /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 @* X& D2 q) o' u4 {. r- W0 WRotation is separate from addition to prevent recomputation.
*/
6 }9 `7 c7 k+ k- W& S( N" r$ f8 e$ f #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" X8 ?/ i3 Q. J# _. @' @& `% _#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ M$ \3 c* P" \. G" X#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 G5 ~" G4 h/ S: T* [$ T' H* r #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 S% t8 e" S" D2 x4 I* _' q/ z) Z5 z
7 R6 b, y1 }' m8 \: E G3 T; I /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
3 T8 j/ X% M: X' C8 N9 w void MD5Init (context)
; m6 Z3 ?% E9 i- Y: `MD5_CTX *context; /* context */
( k' T( A8 l# A{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
m1 n# x( a {+ Q! T*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; F$ a O5 X- R. Q) z}
1 ?3 `$ k; q3 o1 E$ v
! Z7 Z! s- X) h+ q% y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
1 b7 U0 }& r! q void MD5Update (context, input, inputLen)
+ C/ q: f" e( j9 wMD5_CTX *context; /* context */
8 S8 j$ W6 }8 g$ F9 `unsigned char *input; /* input block */
# }( t" |- q2 hunsigned int inputLen; /* length of input block */
" S2 d3 w( A4 x6 Q9 r{
unsigned int i, index, partLen;
$ `0 u% Z g* e: m# e/ q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% Q# \, k( w7 d5 b$ f
/* 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++程序


& w' i2 ~- E- o) ^
# U$ p5 l' z+ T4 L; E7 f
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-5 08:27 , Processed in 0.110988 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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