中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
) E# [( c4 Q5 W- d' h #include "md5.h"
& l5 @7 D/ ~* P' C2 w
6 f9 X1 N, \' ]- m" D+ `$ K4 j, y /* Constants for MD5Transform routine.
*/
- G1 v- C. Z7 G
6 U' E( G3 M, n1 `7 P j
4 P r" Q0 ~0 Y" O#define S11 7
3 R) c, f ~4 ?/ Z$ I8 k #define S12 12
* B1 l7 b2 V( ]$ V #define S13 17
0 a1 |2 s' K8 l9 J- Z7 [' Z8 H" L #define S14 22
" k; m+ o4 ~! g% ?( e- b9 k #define S21 5
2 N6 ?, W) M: v, k2 D#define S22 9
* ?$ Y( _2 [" R9 D3 O #define S23 14
7 k) W6 u, g- m: }#define S24 20
6 }$ k: A/ t! `' a: I! X #define S31 4
) {6 e3 {+ d# s! A' f# ?#define S32 11
0 I! ^6 n; E8 `* u n9 K V #define S33 16
P2 ~7 ~* c, i: k5 L#define S34 23
+ N9 o* ^; l! U6 D. n- D3 b #define S41 6
& r) l; T5 B5 V1 A5 ^& b0 I #define S42 10
$ b7 V" b8 k5 y" l2 m5 |#define S43 15
9 Y% z! O( H) |; S #define S44 21
; q$ [, `0 U9 [5 T+ @$ f+ b' q
( ?5 L; c1 _2 C D& q% H. F static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
* l3 e. Q( O8 T D1 E9 t% Mstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ s7 @/ G0 j" i& Z3 ~2 U1 \ static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 V* U( r- O2 g+ A$ c static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
0 g9 X+ w% n0 c: g* rstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! ?) W9 S6 q* d6 I
4 t( d# P& S( \" v0 l4 P; \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
f1 ~5 }0 M" w$ e9 g" i: b. l& F* |};
! s! d. Z r! r# N$ h- H% x
( T! T9 f# f$ t6 J* Y+ m3 q/* F, G, H and I are basic MD5 functions.
*/
; \( ]; O5 T2 e#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
) j5 {) `* w, e& `+ g; C#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
% C; e: ?' P2 n, i! N#define H(x, y, z) ((x) ^ (y) ^ (z))
* q, c% V( S3 B: X#define I(x, y, z) ((y) ^ ((x) | (~z)))
) z) d; ^6 C. L3 Z/ e7 a
# D" [7 k( C5 p& T/ B /* ROTATE_LEFT rotates x left n bits.
*/
+ F1 h: L3 |+ `/ Z, _1 }0 g. ~#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
& z' Y6 u3 v: V2 ]& a% R/ D
/ ?1 I5 M- o7 H, T! A( p /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- B0 N X' w/ a3 ~3 J4 GRotation is separate from addition to prevent recomputation.
*/
& B* x0 e% v$ q ~#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 `/ I; e' a8 K4 f) [#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
q1 Z+ @) k/ T. p7 } #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% A$ ^+ v0 i/ S( I#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
4 E3 }3 C* j4 l- H+ s/ {6 O' F
* N4 ~5 `! f$ Q$ l. G- h" s( C /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
* K; e0 @! T; Z G" Cvoid MD5Init (context)
& k: z! ?" [* `' u" ?. ^7 I MD5_CTX *context; /* context */
& a6 D$ {8 O( T. q{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
9 L! ^4 M- E+ k$ S3 X */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# v& `, Y _3 L; s }
: G" v; C' y2 Z5 N: V* C6 t
- @6 r' C3 ^: Y0 j* B# Y( p- x0 r/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
% K% P, X7 C9 E9 c2 s" s9 Y& q void MD5Update (context, input, inputLen)
3 o. b7 `" a0 s MD5_CTX *context; /* context */
! E: _2 Q5 B0 N unsigned char *input; /* input block */
: E! h; E: M. C6 ^ _( A unsigned int inputLen; /* length of input block */
5 n r8 y7 e6 j0 k1 J7 t{
unsigned int i, index, partLen;
4 v8 u6 r3 z' k# |& C
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
% k6 D! f) X3 Q6 ]
/* 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++程序


! S7 ]3 l- F/ A! r0 E
6 L# o& g- c$ v6 d. B9 q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-10 04:50 , Processed in 0.058753 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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