中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. }9 K- O; I5 B1 a #include "md5.h"
. A/ G% q5 l: ?! R: j
y" w7 z9 c8 e/* Constants for MD5Transform routine.
*/
! C( b' J7 F# l
) L" {& G% u; W, e! y
. U6 U+ I$ A& A9 h$ d#define S11 7
3 i) `6 U* X& Z; d; n+ f: \#define S12 12
% ^/ u1 q! x: z #define S13 17
( e* ^5 S( A! P. k' u7 ~( P) T #define S14 22
, b/ `- J8 k& F7 Z+ f0 S #define S21 5
: M* i) C- Z/ _- @ #define S22 9
' C2 z1 ?* z% l" ^#define S23 14
) V, s9 A( S; X! w! L#define S24 20
# q- r7 D- j/ V1 N( V# s9 ] #define S31 4
$ v& a2 Q/ c7 Z& g#define S32 11
* u( y! c/ z: W7 a; A* U# E #define S33 16
' \2 V+ c5 D% I- t#define S34 23
: v% e- D; ]$ V Z* L6 Z& U#define S41 6
0 I: r2 A* L$ A" z #define S42 10
& [" D) `4 \& `$ Y1 |8 \ #define S43 15
: O( y+ t7 H R" [; F2 t# B. m#define S44 21
$ a' o( i# p8 N) m( t/ u
* a( @9 W2 E R# R0 K+ H- I% Tstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
0 v% u' A i1 H4 W# U" t s; ?6 zstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 }1 h9 f1 f( B6 Hstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
2 a$ M# Q- Y0 _ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
) |* J3 l0 O* d8 P H" E5 ~static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# g o% o+ N7 `3 n, I# d
4 J9 e: F& u$ P3 B1 Y J% [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
, a3 {- Q7 k8 `! b};
3 d+ V. e' o" d0 |! s
* {9 m: c' e2 P2 o8 g/* F, G, H and I are basic MD5 functions.
*/
$ |6 u5 d6 X' ?3 X# d4 m" S#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
9 p6 T+ @1 q) Z* V+ S#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
5 q8 ?5 k+ e) ~! `' b#define H(x, y, z) ((x) ^ (y) ^ (z))
' X7 b/ X3 u8 {1 p- a7 K #define I(x, y, z) ((y) ^ ((x) | (~z)))
/ }4 T; A' K2 R6 `' m' s9 Q7 s5 Q
8 l% B9 y2 N( E: H* b, M) ?" n/* ROTATE_LEFT rotates x left n bits.
*/
# G J: I0 C: u2 J2 _% r5 e#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- d2 n7 E6 u# F( a9 ~) k- F
. F' n; J- C; d7 F: I7 m* `/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 y3 B. V# o- f7 G Rotation is separate from addition to prevent recomputation.
*/
1 Q2 e: S+ J: S. B: ]#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" z- }7 ^2 H9 J#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 z4 o( a4 M3 J# V7 I+ H) p9 L; l l& I/ ]#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 N& V$ v' i" ^7 I. W#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ B4 |5 G8 V' m. c& A4 T
! A; m, u( L* F) d0 z/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
; M3 Z: C) C: n# O; G9 Y void MD5Init (context)
3 |- A" v, X9 B, l( Z0 v4 fMD5_CTX *context; /* context */
; Z0 a: Z- T8 w% X4 ] {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
6 v" F. r+ `6 f( B) a5 O; G' [*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
* {& }7 g# C$ _3 B }
6 v' E" S2 _4 [' j7 G
+ `% \: s3 y% L9 S) e, R/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# }$ J& C: x4 H1 [7 g! m: {1 _ void MD5Update (context, input, inputLen)
+ A) r: S& B# n# W" j z) w MD5_CTX *context; /* context */
" z4 |/ ^, h9 n: ~% c unsigned char *input; /* input block */
) z% Q8 J/ F. `4 E5 F- Dunsigned int inputLen; /* length of input block */
% b* k$ o. L" J3 \9 @% b/ y" q1 g{
unsigned int i, index, partLen;
. |) [( `& S+ ^& d' j' a
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
" z6 x2 H! O! 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++程序


3 q6 w0 i2 L: F2 G( R. o
/ u* R1 M7 s# K% t! ~0 C
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-16 20:57 , Processed in 0.084809 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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