中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
8 ]# Q; @- X( o3 n+ B1 O#include "md5.h"
# W( h% u* e9 B v2 E5 r/ e6 F
5 Y3 C U- G. v1 t, e8 Z /* Constants for MD5Transform routine.
*/
9 ^1 `% x5 }& B, X c1 `8 f3 [
2 F% q2 O+ y6 L- J$ K
9 e4 D0 N# _# _8 j' T* C#define S11 7
$ ^* G& K7 V0 F- { C$ X1 ` #define S12 12
3 N* k+ Z& F$ l+ l7 {$ t #define S13 17
1 F% ~% w' e9 W& Z u3 Z, P+ F #define S14 22
; e& h$ U) J- S1 i p#define S21 5
9 `( s( C! c1 T+ r#define S22 9
9 e' h0 p( ^! X1 D #define S23 14
9 x2 }1 s4 P' t3 [5 ~/ P7 ^#define S24 20
6 }! k# K' q F7 f5 _- p" j8 S#define S31 4
; O* L# B6 ]; o5 p6 z* [" t#define S32 11
0 _/ r! a, L, m3 t# B2 w#define S33 16
$ t2 ]; L @- V7 c) c' b- [ #define S34 23
0 k: W8 `, R6 v) H% c" N. s #define S41 6
; k3 O0 ^) z' o( ` #define S42 10
4 s, h2 J. w' Q. N #define S43 15
, h+ |& M+ p" @8 G% s7 _$ i#define S44 21
/ J6 v1 {/ z0 p4 e- r G7 W ?
+ q J! D( p2 b/ J: A; X& p. V; ^# Dstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ o' {0 \# j% z static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
4 Q* J; H4 O, lstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
6 T! e' s& _( q0 ^* N- nstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
4 P ^2 O1 M7 ? o# Mstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 o; T$ s9 i* C8 u4 [. o/ B
0 \3 I% Z$ F9 F1 R! |" u! W! B5 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
" ?7 _9 T7 h, S };
! p4 s" p1 P7 `: }! s
4 V8 c& N, G7 y/ n! E2 s* d* [# E/* F, G, H and I are basic MD5 functions.
*/
5 d; x1 o% j9 C- p6 Y#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
3 h% G' c/ P0 o#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1 N. R) G% e! J/ `0 e #define H(x, y, z) ((x) ^ (y) ^ (z))
) p: y# m4 O1 w G, D2 H4 O #define I(x, y, z) ((y) ^ ((x) | (~z)))
9 y3 x6 F3 M7 L( v+ G7 H1 g; u
7 n" n, S" Y: }; T& Y/* ROTATE_LEFT rotates x left n bits.
*/
, V7 G0 Z; X; R. S#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
: [& q- a" V: p8 v" N" M. h
6 ]$ A& M' g7 j+ j/ ?5 C5 s /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* O# d9 C& e* |# WRotation is separate from addition to prevent recomputation.
*/
4 G8 u$ N" I' l; a( 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); \
}
/ z( `) C4 M$ Z$ ?# P #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% G: @, ] N# Q! C$ 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); \
}
( q+ H) p' E8 B$ @) Y #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* o+ u( F/ v9 N" |) f7 P0 ^
: A- N# h' l% e* S- g8 U /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
5 ?+ k+ U- C( s$ d6 @& m$ ~1 Pvoid MD5Init (context)
/ v9 `! ^% G3 k2 PMD5_CTX *context; /* context */
; J$ a( E# D- G" H6 T{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
0 }+ p6 p( i& z. g# L, V. F*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
& X: u/ l( Y ?! P& J+ U}
y# r2 D Z: }
- Q3 g$ E' l9 m# Q2 w+ u- p5 ` /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
* d7 _, G* f2 O1 T3 y" rvoid MD5Update (context, input, inputLen)
! U6 f5 i& }; K# x5 q0 TMD5_CTX *context; /* context */
* f' H0 i5 s" q7 O$ \/ |& n* dunsigned char *input; /* input block */
t, N2 {$ B$ Z3 M- V& B unsigned int inputLen; /* length of input block */
8 K# g9 a7 m0 V {
unsigned int i, index, partLen;
( ~' g4 }! F H9 ?
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
' S2 @5 ^. F+ U; 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++程序


1 W. H1 @) Z. c$ t( ?+ v
q4 b9 S4 I& d& J* k
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-7 11:03 , Processed in 0.080741 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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