中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
. ~7 L9 Z j: \3 _3 P3 ]#include "md5.h"
8 p' R$ G8 r9 o) G% d7 d; F
& h2 X4 m: H) J$ g /* Constants for MD5Transform routine.
*/
; `2 W2 _& Q/ d. l0 F( W" Q. G5 ^
; K2 W v9 g4 \) n8 B C/ w$ y& a
* b0 T6 b" \$ S1 h) }#define S11 7
% e2 J) B0 o b9 n* K9 a8 T8 V#define S12 12
9 X+ }' ?9 ?; V9 ], b7 y #define S13 17
% ~* V( Z8 x3 O$ g- u #define S14 22
. Z5 Y3 a- b9 U4 N, N4 ^#define S21 5
1 P0 b' o z0 k#define S22 9
) t& m( o& X0 }) n4 x+ w7 D5 y#define S23 14
l) B# x/ Z; q#define S24 20
9 C0 `: R# ]2 B' r" K7 C #define S31 4
, t5 O. o9 K. i& v0 H+ c# ~% f& V#define S32 11
- {* n7 E: j$ j q+ B* z #define S33 16
( P1 n) u, j6 g; v #define S34 23
6 {! k/ w/ s) y" t9 e#define S41 6
" Y4 u- `& Q7 o+ l) C" ^#define S42 10
! F. V) N1 `7 p#define S43 15
1 b F% Z: [. `$ M& j#define S44 21
: m' k$ _. O' U; ?8 w
" L( |* M7 Y" ?& K3 estatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
' s7 i: a) M& c- U static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& K2 ?3 n+ k0 D1 \2 V2 V+ t1 I static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
/ M* h B* q c static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 N3 R' U; V2 u6 o1 [& d) d static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
; i. C5 b, c% G1 A1 C% n$ O
( U( ~ F8 Q2 m8 ` 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
+ j9 T' x5 M9 Q7 m* W* x8 Y};
" n, t2 B! h# S# |( y% z, |
% J g/ ?6 b: W3 i0 ^$ d7 |% G T /* F, G, H and I are basic MD5 functions.
*/
% ~" F. ? x: j5 K6 v s1 R#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; y9 q" A; S* N2 x7 n- P- C #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( N" Y8 z* R+ `#define H(x, y, z) ((x) ^ (y) ^ (z))
6 M" T0 E. B- R#define I(x, y, z) ((y) ^ ((x) | (~z)))
* C# `* _* H$ [! G `7 b& S8 v! G" C
3 j7 I6 w3 ?6 T' I4 P& ^( z& N9 u# k o /* ROTATE_LEFT rotates x left n bits.
*/
5 Q# |, \- K/ b' Z! j# j' O' x #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, L0 m/ p- _! [1 I- D
l" T, X1 H. G% b( M. L /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ s1 f* w0 N, \, qRotation is separate from addition to prevent recomputation.
*/
R; m; ~0 o+ b6 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); \
}
8 D) \- i9 X0 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); \
}
' S" z; c, r' {: X! S |$ L6 w #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. p: U- I1 O( @- c! D#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* v, f5 N1 {! `+ i( p' B" M# `+ c
& u- s+ j- W- ? /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) T5 r4 ^; M6 ?/ r# C+ L# Fvoid MD5Init (context)
/ s! s& e* M, B MD5_CTX *context; /* context */
: Y9 ?% e) b4 Z" [% u- @4 f9 a; L {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 V& {# V' C2 ]4 c$ K */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
$ P/ J0 \2 \$ C2 k. u}
) Y, M5 z5 {* H% G
# M/ T6 F8 r" m1 z/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
; T, i- n& ^# j/ s7 K" @6 yvoid MD5Update (context, input, inputLen)
& M3 y1 \& H/ R+ x MD5_CTX *context; /* context */
! e4 H, ]" n0 eunsigned char *input; /* input block */
% y: U/ \- o T+ Gunsigned int inputLen; /* length of input block */
7 b% H' y% a F- h3 p {
unsigned int i, index, partLen;
, }4 C( \% L+ u
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
2 u. U7 V- p9 E8 A1 W
/* 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++程序


2 i) j/ E7 u# e2 K3 A3 i1 k
4 N+ t9 h" _6 Y" [* f9 U* G
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-18 03:52 , Processed in 0.080307 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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