中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
2 h) L3 h3 t: H #include "md5.h"
! d' s6 Z5 K; ~' z
- F& |# @0 i4 y" {+ W. a A /* Constants for MD5Transform routine.
*/
# ^; w0 G* Z; B- ]" R
0 J$ f3 o. o [+ P
2 n+ E* } o+ r4 C2 i2 l1 ~+ h#define S11 7
9 x' Z% P1 W9 D4 i# d#define S12 12
! w: e6 i0 F0 R. N #define S13 17
! l0 S$ V" A7 ~7 H" f5 `$ @ #define S14 22
2 ]. }5 T" ]% R2 b* k9 V9 i. l#define S21 5
, K# r+ s+ l# y #define S22 9
4 M% P9 u$ ~, @/ N0 Z* `#define S23 14
, J% k; s/ k* a ^ #define S24 20
- B/ R9 K/ C- E; {! J8 ~/ d #define S31 4
8 ~) q2 h; N6 a. ~2 g' a# P #define S32 11
+ J9 ?6 Y4 Z, {* n#define S33 16
4 i, f7 z/ B/ J% r3 ` #define S34 23
+ u4 }& @6 O) p2 r6 W3 I#define S41 6
- q) Q# q* F0 j( N( g, y #define S42 10
, h* \( D) c* @0 P% L1 u#define S43 15
|/ s3 m" v. x: |# I4 A' y#define S44 21
, d1 W% r& V1 o* v7 |, Y. u
6 h% `( k/ X, K6 estatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 l7 B2 C2 M' H0 S" x static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
5 R# \6 r3 P! Y6 [static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* \) {7 \& w4 ?1 E3 A% gstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
' c/ @5 ~0 y) e+ `static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
V: E9 `2 J6 ~( \# `
; s1 a! Z' k4 `/ @8 l% `5 o' B 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
4 F1 |7 J$ {, ^9 i};
' J, O7 E \2 f) a) z
) M, {* ]1 u; H$ m, | /* F, G, H and I are basic MD5 functions.
*/
% @ h8 S7 o6 u#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ V& M! ]( `! U& Q. z8 L/ A5 T, R#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+ k h, n7 o) _1 ~: L2 u1 V#define H(x, y, z) ((x) ^ (y) ^ (z))
1 K) u' H( t7 m; F3 [ #define I(x, y, z) ((y) ^ ((x) | (~z)))
" n: B9 b5 y5 y
( z( n* [- ^5 H% \5 d/* ROTATE_LEFT rotates x left n bits.
*/
4 W- A! {& n4 ~1 c#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 P7 c1 h; G2 Y: D8 T, @7 O
, ]/ U/ Z$ P' T4 o/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* j& c; K1 b# x# S2 M }Rotation is separate from addition to prevent recomputation.
*/
$ `9 i/ X6 r: I% Z#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 B `8 }) i! ~: n) e1 r#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& D( O; u+ D$ a& W* m/ u5 R#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ N( S. r! |; z: 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); \
}
/ l" d9 e6 y7 G: j# `
' X4 p0 G3 v8 _. c O /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" v J9 g: l9 M/ T# `+ z4 D3 i+ Avoid MD5Init (context)
' f7 O P- M; _; J0 t4 e; `: U0 jMD5_CTX *context; /* context */
3 n0 {' h9 F3 Y3 m {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
5 R+ J2 J7 W2 J9 K) }7 j*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
4 D0 v6 _: n8 M }
8 \' s7 C, q, n' E2 t( [* |# [
6 L# C! _6 }$ u8 K1 f* ?/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' C" F% ]$ S i$ R9 X6 l void MD5Update (context, input, inputLen)
' `, Y5 d/ e: E+ \8 q' bMD5_CTX *context; /* context */
* j0 s( L, `1 z4 z! A7 }unsigned char *input; /* input block */
0 T5 }4 |, D, J unsigned int inputLen; /* length of input block */
T/ B# T1 [( U9 B3 G& I {
unsigned int i, index, partLen;
* H% I$ T6 }1 v& f% [7 r
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
\/ D2 i4 A$ E% S' z" k- _- b3 s$ q; 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++程序


, ?" d1 l6 P9 j; }$ c0 H; M( q4 y
5 j2 r p* D( u; R2 |
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-20 04:24 , Processed in 0.333221 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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