中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
7 S% G' R4 J" T, m) D* q* ^4 f #include "md5.h"
4 v7 x! O* L6 U# D6 S }
: O7 y/ ?, X& L4 R/ n/* Constants for MD5Transform routine.
*/
, A2 a1 R; g. X2 e% g2 e5 m( Y
0 J- N9 A+ R R4 r4 n
% U; X9 @; E- d% ^& {6 _#define S11 7
+ s% q1 A( B6 G0 w/ G( ?) D% { #define S12 12
& m& `% O0 {" q$ A' r0 _: p #define S13 17
" G6 v: F) X3 q6 L$ C) F* v2 e #define S14 22
x1 \- N+ h( l, E# r2 _/ G- m$ O$ Z #define S21 5
' d+ d: X! [1 S #define S22 9
5 m9 c( L- Y9 q#define S23 14
- `6 d; |; Z& I- K9 l( O #define S24 20
8 j; {3 e* w$ j8 V#define S31 4
, ~3 D+ M' \, [2 l9 T* f8 N#define S32 11
, u2 C1 m [9 ]' V #define S33 16
' f$ |0 j+ Y+ ^3 p5 k* x#define S34 23
8 [& c) k y& l. g. P( L #define S41 6
7 B5 r6 S7 B* _/ [; Y6 \5 L) Y #define S42 10
( p) Q2 n& Y+ N9 g+ j#define S43 15
3 k! B- s/ ]! S/ k! M) x6 g( {+ j#define S44 21
A5 @- @0 ~' o" [, E9 r+ S" R% B
$ Q: {6 C- A; p& p1 D static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
" I: a9 Z! D! E% \/ n static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
5 a- q- Z% ?/ i2 Z4 Estatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
9 a; L2 m1 ]: Kstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ l8 Y% g! ~: T( t/ z static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
& Q- z4 B: N5 S* p; V+ ]
5 ], ]& l8 [ Z. X; a, ?) H 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
# ?2 r0 P8 N7 c \( J/ \};
' r9 q; B+ I, D+ J) ?' @
( z! d) M" O% x* j/* F, G, H and I are basic MD5 functions.
*/
3 M' q: O; I! ~: l#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
4 W$ c# ]# C+ s. c7 c+ _+ Z #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
/ v1 Y6 y0 b5 |: v6 v#define H(x, y, z) ((x) ^ (y) ^ (z))
6 P. R' u* s$ A' v #define I(x, y, z) ((y) ^ ((x) | (~z)))
8 {2 X& W1 K' `. a8 y- ^
: o/ O. |# A8 r, M! R+ N6 |3 X/* ROTATE_LEFT rotates x left n bits.
*/
O" A7 S. i& p& | j#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
u6 r5 m! ~6 j& ?
0 I( e) v7 M$ M) J /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
. D. M8 x r0 i5 Q9 y6 }' n Rotation is separate from addition to prevent recomputation.
*/
3 k, ^8 w/ l0 r+ n: c #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& H# M/ d3 q+ H9 Y #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 }6 g! e" b# C- e9 f, ` #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 @3 D# x2 k& ~5 {* U; B#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
& H- I |' t% ~- j; F
4 |" e) { S8 Q; Z5 T3 [/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
4 i) K" H2 p5 p3 u9 R' D9 ^) u void MD5Init (context)
+ n T4 }" C1 k/ \9 y/ yMD5_CTX *context; /* context */
4 u" _: e) J4 @+ n- k) K/ A, ~ {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 m3 d' q4 ]- d# L5 w6 e, ?*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
i5 C) u/ l( s* M# T' l4 T. G }
3 I5 @) m7 {; ^: }& Y" t$ |' S
' D) g; C2 G4 N' b /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 G* b9 T0 Z8 {6 g2 N: r. yvoid MD5Update (context, input, inputLen)
% m% h" a/ D; n; o% c& u8 x3 {/ xMD5_CTX *context; /* context */
?- ~$ C c8 k" s: G unsigned char *input; /* input block */
" ]+ U: a& J5 F5 C G: p unsigned int inputLen; /* length of input block */
; Z1 |6 y/ T F {
unsigned int i, index, partLen;
j2 S7 }2 q; t% V, t
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( U0 v; s& L: A9 Q! O( v/ |
/* 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++程序


' E* u- ]. L( T0 J- ~0 v7 N q
! G+ o* z' m. o! j0 b% H# s
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-29 18:23 , Processed in 0.055165 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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