中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' h. Z: X3 `" m1 ~ #include "md5.h"
+ R5 [3 e4 |2 \3 |2 k' g
' k) X! V) [7 m) R3 _. X% u /* Constants for MD5Transform routine.
*/
6 R& r3 [, g8 K) g, Y Z8 \; j
" O Q3 R2 @6 w [$ o Z) Y
7 e; s( H6 E' }( h! f/ } #define S11 7
9 \6 K O' K E9 V$ P#define S12 12
8 \- {& I r( q- B ~3 m/ \#define S13 17
" n" e* l& y1 }( R" D* M/ Z+ g. |8 q #define S14 22
& Z( u$ x5 O' V; r #define S21 5
. O1 p) @- }% t" l* H1 q #define S22 9
& {3 q: }: u9 _* L #define S23 14
' r4 ^) E/ d# i+ n#define S24 20
9 ^ H' w1 \, Z+ T8 N* T6 ^ #define S31 4
/ y* T1 d D7 h8 G' Z+ D* h#define S32 11
" `7 C- L' p. G- ~3 j5 p1 {#define S33 16
* H: {- g$ m O% j+ `3 {; h#define S34 23
7 x- G( Y( E- ?$ G- z8 X6 l#define S41 6
* g* _& q; N; `, c#define S42 10
2 ]- t6 q+ {" Q9 R9 N- b8 A7 w4 }5 d8 ~#define S43 15
4 c5 D7 J& Z* u( G#define S44 21
6 a5 s9 p; k- O6 G
; `2 z2 Q. D z* w* M3 ?static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
* s* B; N1 ^. U, c/ Vstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
% T% C6 D; U* H$ `, x/ r9 e. E static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
% h; S0 l5 @5 @; e" ustatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
+ C, Z1 U/ ~0 `' {) w6 O" J( E4 ] static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# P3 o5 ~8 p6 @8 M/ M# R
6 U7 q2 ^9 E8 K9 Y- z8 @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
$ w' B5 @' B) h, N: W$ w};
. F. S' w7 V* b+ Q
& c5 a4 _3 L& S. [2 b, i /* F, G, H and I are basic MD5 functions.
*/
& N! O# h" Y% D8 }. f$ b! \#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. ^7 r9 s' R/ Z( n #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
6 b. G) X6 w: {: Y7 b- V#define H(x, y, z) ((x) ^ (y) ^ (z))
% B# @# `4 }; l. p9 B+ l#define I(x, y, z) ((y) ^ ((x) | (~z)))
: E, p+ M7 L2 x& T7 H
% R4 J5 h1 R+ w/ y /* ROTATE_LEFT rotates x left n bits.
*/
, R6 u1 v3 e3 [4 m #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! M: j& V: d: n
( t* ~; x: E" w6 S% I0 N( J" z /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
( m9 s$ {2 v/ l4 C7 X: NRotation is separate from addition to prevent recomputation.
*/
# T3 }: X0 {0 E% R! n! [( }#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; F$ x; c( s3 t: @#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
j- X0 e* P, k/ d: @ #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ P: ]! R( \2 ] #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" j& L# v$ U! B2 v# [) P
) E) s( h+ I# y- E /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
# g: e0 {# d% b9 v4 ?' G, x# _" x void MD5Init (context)
% Z" @" r) `0 e: aMD5_CTX *context; /* context */
; r. ~: @8 ?* K/ g{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& M' }4 |$ B& _8 i# s( g- J# f*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ B( e4 i6 {0 Y: n}
i: Z0 e1 z# D% b
) y6 U, B6 S' c4 @% y6 d/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
7 a% X, \4 r' H; e' ivoid MD5Update (context, input, inputLen)
4 Q5 N! R$ w7 a6 L9 e$ AMD5_CTX *context; /* context */
. H4 R& ~0 {! B, yunsigned char *input; /* input block */
, R1 o- E* C2 j4 L7 \unsigned int inputLen; /* length of input block */
0 i1 }7 p' v2 _. e8 v* \; P' H0 ~* q {
unsigned int i, index, partLen;
* e+ j( h: n. T7 N: q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
2 S/ f: |8 G) x) E- C+ W
/* 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++程序


+ v) f) G" n7 z
: B& W6 L# V+ q- G
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-30 05:45 , Processed in 0.215412 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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