中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
' k3 E$ j" U& K) U$ z7 N#include "md5.h"
0 h, D, o. O4 x
' P, S Q; U* O' N- t9 H0 q /* Constants for MD5Transform routine.
*/
% r7 X( w" d3 U( Z0 N
! W( }, X6 c2 M" m
5 b5 Q9 D, `! j#define S11 7
" O9 T! @% R0 n, q6 r, N. k% E#define S12 12
! T- n& Z b- l C& T& X) \ #define S13 17
8 A/ @ l" c B! d7 B #define S14 22
2 y9 }4 t J* P5 \: ~& z/ W6 y #define S21 5
3 ~/ s! g& C# { @9 I' W #define S22 9
4 p7 ~& d" ?6 J) M( {#define S23 14
2 V g1 S3 n; v; K) Q, n- h* B3 ^#define S24 20
' o1 g# K! J9 j5 n4 j6 t #define S31 4
* g" w8 R! [& l2 j' F #define S32 11
& g O2 U% [3 i# _7 L8 X0 q#define S33 16
/ o6 _7 y# I4 }$ H; D #define S34 23
7 S" U4 Q/ h* V5 W. { #define S41 6
! a r' P% E! ?#define S42 10
3 P7 _" ^) F+ [1 D #define S43 15
( _" C9 T9 d0 Y& H#define S44 21
7 {" Y* U; j, G! i
: d3 }/ N' ~3 `; fstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
0 R# p+ G+ `2 ` ^. D, s6 ^static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
& U$ A2 u2 q& ustatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
7 o4 } @; g1 N( u; U static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
0 j5 h' u* i4 Y; \$ tstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, _8 X9 p4 X+ _9 i
$ x$ X4 O: `( I) o& m: t' ~ 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
$ n7 q m0 `; S/ l5 l$ t6 h };
8 I C% P- Y$ g% F
' g8 r1 }7 Q. m! R: w/* F, G, H and I are basic MD5 functions.
*/
2 Y2 W# J6 a6 G% U #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. D8 y2 t7 y* U1 c1 t#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
- m- e4 [: j, i$ @( X) b, x* r #define H(x, y, z) ((x) ^ (y) ^ (z))
' O& n# { T1 ~' ? g#define I(x, y, z) ((y) ^ ((x) | (~z)))
* X, Q3 b' e+ p9 `
' z6 q8 J. k. S/* ROTATE_LEFT rotates x left n bits.
*/
. A+ u; |0 v3 L7 [#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
; j6 ]5 d! k% |# z
* ]/ C- M; q) V% {. l3 U+ g! f /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
; \0 r( @* u- `% Z Rotation is separate from addition to prevent recomputation.
*/
- A7 P3 l' A* H8 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); \
}
9 Q1 f- E, L3 o! I* O1 d #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
$ [* P0 m8 {( q3 b+ D #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! w+ y7 ?1 S- c: A, 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); \
}
R& s: w( N5 ~8 o( I
9 g% E8 N: U1 [ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
! l$ C8 b6 A! V# a$ ? void MD5Init (context)
; G2 o3 `2 ^8 D2 V% U" Z1 }MD5_CTX *context; /* context */
1 y4 _3 \: j: ^8 \7 w {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
6 b! Q* Q: f: w( N$ \*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 l+ s( b8 `, l) E}
; T3 p. p' b) a1 B
8 o. d% k/ g% N! N& |% t+ O/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" |# B/ O) g% s1 f& g0 I2 hvoid MD5Update (context, input, inputLen)
( J, g( w( M: [9 j& ? MD5_CTX *context; /* context */
) `% D$ Q& F0 punsigned char *input; /* input block */
$ W# B5 b7 Z* Z' y unsigned int inputLen; /* length of input block */
2 w' N/ S2 _, T- b7 S- i {
unsigned int i, index, partLen;
9 r* A% U) W4 E; [) u y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
- E% v e, 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++程序


+ h- p/ Q! E. K( ]( a' I% m
: T$ V; H1 r, Y& k1 ~' j
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-2 08:33 , Processed in 0.216374 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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