中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, K7 I5 ?3 j1 H3 J7 ?, L$ S #include "md5.h"
" `6 _$ J; ~/ \% o+ w) z
" m2 E" ^. P8 w7 P. N0 L. U) |- T8 e /* Constants for MD5Transform routine.
*/
8 P( B% S& W7 q
# q* c+ c5 l' \! |+ q1 y b
1 O2 h3 |5 _% X& F#define S11 7
( N( {( _5 \( _' \8 l* t#define S12 12
0 o* g4 p8 {5 u& i* c8 m# e7 ]8 x5 S#define S13 17
. d3 h; ]( P2 Y) ?% |& Z#define S14 22
" E, s& T' ]: K #define S21 5
. X* x* e) Z1 o8 h #define S22 9
, p& R; o( a: `( h! K #define S23 14
" g) d& ^* @. G9 h% O# P; \$ ~#define S24 20
) n* V* d4 C; b6 u4 w#define S31 4
& g( Q T- L! W, \$ H( d#define S32 11
& \0 a" k: _ o+ Z#define S33 16
' P# g) b0 H: ]; A7 R7 f: ~#define S34 23
3 l0 n( ~# U! u% ]% J) C #define S41 6
: r* e' ?5 x" [7 ^6 A #define S42 10
9 ~ J$ s) U3 F, Q- p#define S43 15
6 K- ~0 Z0 S5 g' z8 F/ l#define S44 21
2 R/ w4 o3 h( E( U' m
/ B$ c$ P& a( o$ D# R1 f" mstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 R8 q% w" X" j U static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; H3 W6 o. n1 O/ \* _% i static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
" \/ l& i1 {9 ]( d2 D; O( `6 Q static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 @1 F' @( A5 U* n static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
1 R0 ]. W+ [9 l: `
% u: A7 W5 Q5 |9 e 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
& ]0 X: k! i) Y};
0 u$ z0 ~6 s5 F* u
, Z& g8 p" P1 i. v2 o5 H+ q /* F, G, H and I are basic MD5 functions.
*/
; r, a1 Q m9 {' M#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
- |2 \1 a- L$ i6 ?# S( V #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: B# K' o# U: G$ ?. g #define H(x, y, z) ((x) ^ (y) ^ (z))
4 ~7 [! e( o4 _4 m& F) _% @- X1 z#define I(x, y, z) ((y) ^ ((x) | (~z)))
$ R) d+ m. F" O* K& n; a
) F0 I: l) f9 [8 l# f/* ROTATE_LEFT rotates x left n bits.
*/
0 r7 l7 e4 u8 ]2 {, {- V#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
, ?' W$ X/ f& x; d
& `$ P9 k4 h$ ~. x8 Y$ j+ b /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
5 m9 Q6 w; G( f: e7 p Rotation is separate from addition to prevent recomputation.
*/
, S- l5 M+ E- G6 Q6 S#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& O- D. }! T: _( Z; A% l8 k6 k; U#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 B: c# O/ }7 [/ U#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 _- o6 ]/ E" z #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- M1 P5 C/ _1 i* o2 h
- T8 u% R" X% m" {0 v: s& W/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 I, h& v' g- q- P b( Jvoid MD5Init (context)
b1 g0 _$ L7 p) ]) | MD5_CTX *context; /* context */
: N6 Q I( L1 e' x+ I* [1 E! I {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. }5 H; o0 Y3 D2 [( y' z9 Z*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; s4 m1 R9 L5 @' u, E}
& a( I7 w' f) s5 z
2 }3 k8 Y! S# e; R& U9 a/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ F3 e i: S! W2 a+ ]" B( Q! M void MD5Update (context, input, inputLen)
1 _0 [+ {( f; ^' y2 z% l MD5_CTX *context; /* context */
+ X4 t' x; s! ?" Y- B unsigned char *input; /* input block */
8 [/ Q) E6 a' b0 J unsigned int inputLen; /* length of input block */
( s' z! q% R; L U1 D" _8 } {
unsigned int i, index, partLen;
7 f+ z$ h' j2 a& \5 `8 B0 Z( J
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: c2 e. |2 L& V7 E3 p0 U
/* 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++程序


. ?7 m; @; M6 T4 d8 _
4 Z. Q8 x, S* `1 N
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-23 15:55 , Processed in 0.058223 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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