中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# J% d6 h( X6 p9 o #include "md5.h"
/ D0 Y# G4 a, x1 P% n2 D
# ]+ a0 \1 a7 F /* Constants for MD5Transform routine.
*/
# o8 z3 \. y& s/ A( t1 Q
& ~( Q# E' U+ B& }6 r0 O5 l' ~1 p
, t5 e( t5 J% N( t #define S11 7
8 _3 m9 p& l) @+ N- f$ y2 h$ Z0 u#define S12 12
0 y3 L0 `* S% C! r) a9 I #define S13 17
- X, w; z1 t2 v#define S14 22
: A% D0 [3 \0 N# W! J& G#define S21 5
- T$ v5 B; @& M$ y+ d9 F/ c #define S22 9
- b' w* }2 s, ?/ n( U! p* U8 P. v#define S23 14
! p3 Y+ A7 I! Z% ~7 i #define S24 20
K# b9 {) z5 ^7 f9 |$ n; f: x* c: P#define S31 4
, z+ N0 u7 P n#define S32 11
9 S0 @4 d8 _4 J' j#define S33 16
0 h" g8 U6 C! A1 j! z" X4 Q#define S34 23
O2 s! n4 r- p0 T8 x1 K- H#define S41 6
7 _7 g: J3 \$ E- }#define S42 10
( j1 Y( e i& y& C! u #define S43 15
- z, ]" N% O4 `' H) a' c #define S44 21
/ ^& ~7 V ^3 j
2 h4 [ `- Y# ^1 w4 f: [7 O static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
9 a5 n8 O% T: @; n static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
# K: ?7 d$ ?8 v# o0 G1 i static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
8 \: o# ]1 F/ ~# J F( W static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 C7 I2 J- V {+ L static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
- A3 K) K( b. H6 F
5 J7 E& m8 _. i( M1 D5 V3 l+ wstatic 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
+ B' k9 Y# D1 K6 z: e' _ };
$ ], O: a4 |! `2 g; V; R2 |& X2 h
$ H: c2 b; i* b6 S2 e* J/* F, G, H and I are basic MD5 functions.
*/
: ?( x5 D! Q3 { #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
6 u1 b d$ r2 p2 B #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 C' n9 e6 @+ A! O* a0 `+ m #define H(x, y, z) ((x) ^ (y) ^ (z))
. X! I% ~9 D! s3 K# M, h7 e/ s #define I(x, y, z) ((y) ^ ((x) | (~z)))
: [& v' S E9 n$ o& }
' ^5 j" Z* @1 g+ [2 D L8 D( A /* ROTATE_LEFT rotates x left n bits.
*/
' {5 t' @4 X( _. C: X) f' ]; ~ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 R9 @: J2 v9 M; F( v
6 Z9 }( `! y9 j, C /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1 W2 A. u2 f; H0 X8 d" U( VRotation is separate from addition to prevent recomputation.
*/
; S, W' }' |3 t) K @3 ?4 u#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, i" t5 G$ Q( `/ J 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); \
}
2 E1 ~5 ?8 u% y#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ r: L0 f* r( c! `5 c#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" ^/ k7 S4 C/ B5 x; x# g1 z
! m' u: S' m5 c9 b( Q5 x8 l' n/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
. P" z; K) s+ M9 M7 N8 c9 H0 O7 hvoid MD5Init (context)
( S6 \1 S8 f6 q0 p MD5_CTX *context; /* context */
8 C8 G" n" B; ~( W `{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
. D( ?1 e# W: f*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
1 y/ [& L3 K5 z% K r}
9 x. X; |) P8 o! v9 ?% l
8 C! p# G6 i% S: X/ J! m H /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. ]7 g2 E/ d8 q* K1 l void MD5Update (context, input, inputLen)
- q$ Q/ o7 p0 a/ P+ I4 I" ]MD5_CTX *context; /* context */
1 w4 c; A6 _$ S) w! g+ c9 w8 hunsigned char *input; /* input block */
6 R& q5 I7 h: F# Aunsigned int inputLen; /* length of input block */
: S: T# t+ v5 m S" c: Z! o{
unsigned int i, index, partLen;
8 P6 p# c" V$ `& M: s
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, @2 `% ]2 X9 z8 w7 Q/ 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++程序


+ N& j9 F0 j* X3 ?
5 ^7 @* X6 G5 g! B9 w
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-21 07:44 , Processed in 0.059303 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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