中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
, {& D1 a$ Q9 M' D! `0 X' | #include "md5.h"
( _9 J ]3 u9 V$ [( _
, V0 h p* M. ^8 H /* Constants for MD5Transform routine.
*/
6 h1 h* W0 v! b( h2 q# x
0 K2 `& `1 P$ A$ m
v; O \/ L' J% e5 q! A. n1 {; \#define S11 7
) {9 c, m6 j% K8 H3 A#define S12 12
9 ~: _4 ~" Z6 \ ^' B% `#define S13 17
/ ^% A. c- o" ~7 @$ q #define S14 22
; t' P$ K; D* q: T1 A- X' Z#define S21 5
9 N4 H0 O9 ?+ { #define S22 9
; V' i/ n4 h& l #define S23 14
* `; V7 g# U/ f; e% C& f; X5 B#define S24 20
/ ?* K: g# J8 z* @/ ^$ F; r1 j#define S31 4
; q+ E& l' T8 K! S1 x' K# N/ z& f8 e#define S32 11
* d+ _" a3 O8 d* d( ^' L#define S33 16
' a8 V, j+ I9 i1 y& N* s# P. v#define S34 23
& t% T& I) E9 A# c#define S41 6
4 C: R/ g( A0 r1 O, C, K #define S42 10
) m0 H1 U; y8 y0 @$ p #define S43 15
. Q o# U I' ]# T q) i! q' E #define S44 21
( H( o5 o$ T: M, |+ q% l( I
$ M3 Z# M8 j. s5 `4 K) ^6 Z. N5 m! hstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
/ M, k1 i7 |" t3 \% J! tstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 d/ r6 t& @% g! ~( A8 q4 zstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& K) j# ~0 V# _# Q3 \ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
9 U6 D! P: N; |2 Q% O9 _static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
% `" \% ]4 p. r
2 I* z- n3 X2 G 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
+ T$ J7 `- e `" [ };
+ i* i/ B6 ~. _. [7 H
3 |+ {) L5 Z+ X4 B+ A m' A' a /* F, G, H and I are basic MD5 functions.
*/
' J* \, D4 S8 t" B& V# t #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
$ o: K; z' L3 z4 m#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) M9 q' U s/ m#define H(x, y, z) ((x) ^ (y) ^ (z))
J& X8 M) u+ W Z" J, n E0 k, X#define I(x, y, z) ((y) ^ ((x) | (~z)))
' D; U- |! E4 u2 C4 G+ l2 @
: p/ ^! i. Y2 u0 d; K6 y /* ROTATE_LEFT rotates x left n bits.
*/
9 I+ I) W% \5 w2 G #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( B8 {* Y& E! p6 `9 ~( [& b8 b2 ^% R' G
1 ^' U! ^9 L7 K# w. p/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
0 R! P! y4 y" X Rotation is separate from addition to prevent recomputation.
*/
; R3 |2 @; v/ 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); \
}
' ^; a4 ^( u! l) O0 |#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( v+ i+ [) h( l#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: Z0 ~; V& J9 o; R #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 ?7 G* b4 L: C% m7 z R
/ }- Q+ R" C3 D+ C, f2 z5 z$ m /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' }: l7 I) I8 K9 M5 mvoid MD5Init (context)
. b& W! X) t1 ~0 w) t# ] MD5_CTX *context; /* context */
0 s7 w1 z* E9 J# F6 V: N* H{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
1 [* X6 @; Q4 k3 F* ^% H l4 F! g9 O*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( I3 n, |6 x6 @* V Q& r4 a6 r }
9 u. F* \8 U& x4 e6 Z6 D
9 b9 S5 d& d! @4 u/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
3 a+ N2 D k: H: s9 ~void MD5Update (context, input, inputLen)
7 a0 T- b) I$ S* {. o4 Y y MD5_CTX *context; /* context */
% K" ^; @3 I1 q0 c5 uunsigned char *input; /* input block */
' B/ Y/ M) L# i5 K( l) J, h unsigned int inputLen; /* length of input block */
6 \" ~; V8 e, }- R{
unsigned int i, index, partLen;
5 b* e& \% ~3 p; A( S% M
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
* v: ?% X2 `) s. o8 y# ?
/* 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++程序


" h% Y9 W+ w. p2 E }- |" G7 H* s2 b
6 _7 H, M! d( T4 |
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-10 12:35 , Processed in 0.058484 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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