中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
2 Q+ g1 N4 s8 m6 `) [0 p1 e* S #include "md5.h"
( m( Q! R4 p9 l/ _5 I
# T! a+ n+ C- P' C /* Constants for MD5Transform routine.
*/
9 r& z, r0 D. n9 H) _
; s. d5 ^( L$ l: ~/ {
! s% }1 V# O4 @* z. F! Q% l#define S11 7
- H, h5 y0 Q$ a- e#define S12 12
1 R( b. x3 i4 K, x+ M; S #define S13 17
, n. w. i$ P# u% k' } #define S14 22
( t4 h9 d: T; E; n#define S21 5
0 F3 |. t+ Q, S. R% t% J5 P5 a #define S22 9
9 X! v0 }8 q, _2 k #define S23 14
" y4 j3 \+ d0 O4 q6 p #define S24 20
1 _! Q# p4 o$ E' k8 J#define S31 4
0 \( L6 h$ g/ C) i# N% A: a7 k$ V3 M#define S32 11
% \9 x* @) [2 ?1 ?) F4 z #define S33 16
0 t* J# }. W+ k3 @* V8 n& ]; A#define S34 23
* a4 W& i* B6 Z2 @; g#define S41 6
5 i# @$ Y, @4 w" a4 h #define S42 10
/ U2 W2 t) `5 k #define S43 15
4 L% Q3 y& j6 ]8 A4 G' t% k#define S44 21
* k6 }. G3 i" M2 {- E
2 }, x% N, D& t0 Istatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
+ @! ~5 V# Y/ `' G* z static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! F6 ]& m( b6 ]7 T. D: W9 I, @static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
5 Y2 O+ Y- C% J/ H static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
# z$ Z% m+ ~- B static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
+ x- d! S6 o% y+ \1 ?
d7 `1 G. x5 W1 v! y 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
2 ~1 l0 r4 e. ~7 | };
/ W$ n0 D% G& n! {5 {
* o& I7 F/ j& V1 q7 H /* F, G, H and I are basic MD5 functions.
*/
" S# M8 N; I% P7 w) }! v #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& x+ n1 g l/ j, g1 n( P2 x2 k #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
! H; U; @/ Y2 Z. g7 d! q #define H(x, y, z) ((x) ^ (y) ^ (z))
. L5 [" U4 r0 d) g2 c6 T9 f: ~6 s- n#define I(x, y, z) ((y) ^ ((x) | (~z)))
7 C( E8 P; ^/ ]% Q
: z/ R% F4 z1 r6 d) P/* ROTATE_LEFT rotates x left n bits.
*/
^( S3 E7 X9 r3 f #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" @$ C4 E. l: A2 q( L: g6 Y
/ I! `/ ]/ Q% D0 n3 A$ p /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
0 m! _; D) S; r' S# Z1 p& p" @( T Rotation is separate from addition to prevent recomputation.
*/
% R3 \7 M( J/ R( o/ v #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 d8 x* N2 \2 x7 L. ~3 ]" i7 ?1 _#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 R l! b1 r3 ^1 X4 c! Y! l#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* Q1 @ e4 Z z0 j, G#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ `3 G O9 X' A0 ^/ q5 T
& @4 X0 Z& r }% n2 E0 l, ^ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
7 I6 K8 \1 g1 c9 `5 q: ~ void MD5Init (context)
, C: @ t; X% T1 G7 M MD5_CTX *context; /* context */
8 w. a! v$ Z2 N! S! L8 w& @{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
+ `* m) n+ h. e% O+ ]8 I7 ?*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ {# Q G: u! r6 l6 e( n: c# h1 i }
% A2 Q' n5 ]0 l+ I1 s
. J, I* B3 g' k' N% c4 z, @/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
- t0 c; a7 c# X; r% U' Dvoid MD5Update (context, input, inputLen)
4 u0 N3 w1 |5 a MD5_CTX *context; /* context */
2 x* t+ ?: D8 ?( H8 M% |% s unsigned char *input; /* input block */
& X( q" G2 {* P3 t( j* r9 }# h unsigned int inputLen; /* length of input block */
; R9 e6 K& A7 n. }: R+ j' z# z {
unsigned int i, index, partLen;
" u: y7 n( W2 \) ]' R0 \6 a. w
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
2 G' `- i" _: h* t( G
/* 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++程序


6 Y8 h1 h* Q% w7 j. }
( v7 M- s* P& ~3 B
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-9 00:28 , Processed in 0.332412 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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