中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
8 X8 L7 }" a2 G' H# p) C#include "md5.h"
) q! a/ N" u& p
% X( o, O8 x. h* p6 Z/* Constants for MD5Transform routine.
*/
H" V# w; c- x i! ~# D
+ V5 H- k9 H7 F$ r% v5 D$ p
+ I: Y2 X6 t0 v( e#define S11 7
( S m- [: h: w: F1 m m#define S12 12
6 |3 d0 @4 Y9 M' e#define S13 17
5 w, h0 u5 p+ T" k#define S14 22
; G! N/ | S) \7 d# ?# P, Z #define S21 5
/ q( O9 y1 P. a1 k#define S22 9
; |" P2 W i" Y$ X) u8 {4 d4 l #define S23 14
8 ]. |* ~- `8 A7 U #define S24 20
7 |. k1 V& R# X7 {" F: I$ k6 ^, \+ u #define S31 4
* D0 K8 [! P+ E- h #define S32 11
5 D( Z. e# ^" K' y) k #define S33 16
+ I( G: Q+ ?6 x- C7 } #define S34 23
$ n) Q( w- i9 c+ ?- \) f/ T#define S41 6
1 [0 {( j8 Y$ g9 t$ r. W#define S42 10
- G# V4 A, o/ O, W#define S43 15
* V8 s; _% s) N2 [* H |! t3 _' o#define S44 21
% X- V# c. P- h% e2 i* W+ ^
: m) A- k) |0 `. d" G2 m( Y; Xstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
1 h# n& g3 T' E6 u! w7 k6 K static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; r2 }* C5 l7 L: u5 lstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
/ [, Q6 F# c- V. D. G4 Y+ b$ tstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
- }! ^" h. _& V* X+ y* c% Cstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& x6 L2 A) r- Y. T( u" w, ~
. ?8 ~, i$ l7 v) nstatic 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
0 H5 b# w1 X2 E4 r* J) q8 O& v};
/ Y _6 S: T4 b* J( [$ V
0 {2 {9 ^( H$ d- y/ c7 N, z1 Z /* F, G, H and I are basic MD5 functions.
*/
. c: a7 p5 u, K #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- S7 c& k# \% w. ~ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
- u3 v9 |3 n7 d! _" _' S* q5 P, N #define H(x, y, z) ((x) ^ (y) ^ (z))
, j! t& {3 A( o; O3 _ #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 [7 A6 T$ k0 W2 {- R+ E
- A- d5 p0 M3 U" t" q) b /* ROTATE_LEFT rotates x left n bits.
*/
; p' q+ [& a% k #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( X/ Z) \1 }5 k3 w5 H) v4 r
, ~, ~: _1 _5 b a) O /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- O: }5 f6 o3 K$ b7 aRotation is separate from addition to prevent recomputation.
*/
" u4 c3 I0 E6 Z+ v* }9 { #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
) d! {0 P e" I#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ p! I9 P8 k+ y. D: ]3 A: { #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 X. ]7 Q3 ?9 v$ w. s8 \6 R, Z& e#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ z# O8 f1 C6 b5 s" P: ~
: q- _5 G8 O; r, e6 ~- M% J: E /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' S" s$ j8 j! `, T9 I, {void MD5Init (context)
+ _- c/ p- `1 Y" q9 U) L; n MD5_CTX *context; /* context */
6 a' x& Q1 R$ X9 g{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
B' G! }1 `# ~5 |, z& B9 K) u*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
5 T8 ]" E* }! `9 V% D5 ^/ M}
7 e8 e' A4 S2 |
! X/ Q( c$ n7 h; b /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
: n! O6 W. `1 K1 I) nvoid MD5Update (context, input, inputLen)
5 L2 G" t9 u# E: VMD5_CTX *context; /* context */
( b# o+ `3 V1 K unsigned char *input; /* input block */
4 ~7 ]8 Q$ V2 r: p" Munsigned int inputLen; /* length of input block */
/ _) m' D: a; D* `) {6 D {
unsigned int i, index, partLen;
4 t% K2 v8 i$ I& L& P( \( F# z
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: x: t9 Q# }. B* c
/* 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++程序


! I2 N$ Z" f% m3 z6 N$ G
, y6 n+ a. y0 N3 h
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-23 19:24 , Processed in 0.072150 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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