中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* I) Q( _( \9 | #include "md5.h"
d0 J4 _; ]3 }2 F. T
$ V+ W7 ~, S/ G2 }/ D* i/* Constants for MD5Transform routine.
*/
8 X; [ g& `, e1 E
+ n% m. C$ m0 g7 N# |* l: _% W2 W
3 D% }3 Z6 R3 y1 D* e" w#define S11 7
% H# z3 ?5 C6 n) K3 P8 z1 y #define S12 12
" P' w- u$ f* I#define S13 17
0 C' _8 @8 V4 L m #define S14 22
3 H4 D% B& |# _, T7 J#define S21 5
# E/ P% t' {& |4 A. a( d+ M( g& i1 Q #define S22 9
$ I. G! I: V5 c3 x1 v0 z6 w8 l#define S23 14
5 l8 [& L; e6 b# G' I #define S24 20
3 E6 s0 F# x8 e5 f, X#define S31 4
+ c( l- d0 S# u8 i A# h* E+ `, ~+ \#define S32 11
5 _: Z7 v# Y! N0 C#define S33 16
. w$ n- M# e' ^" X#define S34 23
8 ]7 D; d( T. e/ F #define S41 6
& Y5 o6 Q9 M6 T1 E/ b/ O; b #define S42 10
+ n$ \/ c% `" h2 M$ r#define S43 15
: W' ^/ g5 q. r/ w7 k$ a #define S44 21
7 I: C6 a& S1 U" _
2 S, U* g: m1 i$ u0 jstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 x. p6 [* W, U: B2 v static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 O* {/ q# I# ^, w7 g2 ~0 Tstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
: P/ T |6 f( a# S+ R1 ?4 d# [static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; H- V) ` m5 i- c static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
9 T) D4 w" K K2 `
) j& K5 V( {4 P* y. D- e, Bstatic 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
# o; [3 S- P' V$ B0 A};
1 j$ o( `+ T7 e* m. _
) [; O* s0 N: ] /* F, G, H and I are basic MD5 functions.
*/
" x( ?9 v6 ?6 j$ i#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ {# c+ {! P3 a5 v#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
$ P3 N6 {9 `! x# s3 q0 a7 Z#define H(x, y, z) ((x) ^ (y) ^ (z))
# {3 b! J2 d* T0 E u9 Q #define I(x, y, z) ((y) ^ ((x) | (~z)))
. E( e: D) @& E# C6 O' r# [
' C) p7 }7 K9 ]( y2 w/* ROTATE_LEFT rotates x left n bits.
*/
& ^' z7 `; y0 B. ^2 p9 o% d V7 d #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
2 j' X" O5 V7 o5 X# p, {& i5 l1 e# K
- ~! i9 g/ z0 O /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- d {1 x" O' z" _# Y8 g Rotation is separate from addition to prevent recomputation.
*/
! B% q; e i$ X$ [) Z1 h8 O #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 ~" N# |- f- o( l #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 x1 {1 Z1 T) g, h: |3 x7 _#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ |7 I# [! ]& h #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
9 {3 f7 F5 h# a/ i: k5 w+ N6 \* f
4 _) t4 Q. \* Q# i% w6 c9 V. Q, R/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" F# V9 `( A% d1 X8 ?( ~0 J void MD5Init (context)
2 A$ e7 ~- E7 v, e+ m& p MD5_CTX *context; /* context */
( A5 e* `' }* y% o, c {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 [- E' E3 @* Z */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 Z* N; u, r3 H, {* L}
- x9 q3 w0 h, a0 ]
8 \6 s) L! m& Z+ ~+ H$ Q* H/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
' o" M; L8 x q, J6 J# _+ {, |void MD5Update (context, input, inputLen)
" N7 I8 P% |4 r0 ?( G# i, oMD5_CTX *context; /* context */
# E" b( i9 F9 x7 V) {2 [unsigned char *input; /* input block */
1 b+ K: S! C3 @unsigned int inputLen; /* length of input block */
" R- ^& p: J0 w, L @ z" P; k" H; w {
unsigned int i, index, partLen;
- R- o' H. B4 r* v g. r
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
F. G0 P J9 @" s9 W' }- Y7 o$ 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++程序


" O- Y. g: {, F* v4 A
# v3 O) q9 \* ]; m
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-7 06:23 , Processed in 0.082749 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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