中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
4 n+ Q$ R( t2 o; a: o+ o/ \3 N #include "md5.h"
4 Z h% J% Y3 B: H
. P9 l3 g4 @- X! @! ?7 H/* Constants for MD5Transform routine.
*/
& z: G0 L/ U& {, a4 X4 @ k
" W$ J j7 r$ Z% w( X* q
8 ]0 t, o4 X- [ ^% O5 S #define S11 7
4 A8 ~9 ^- f& `! Q u#define S12 12
: \1 n& V8 W# [: m8 L/ P, N) h#define S13 17
. b# C R$ J2 v: e, O #define S14 22
% u# N6 s* f2 _" o, Y, g, r$ @; C#define S21 5
7 W6 @& m2 R- W3 ^9 e" e+ p! k" h#define S22 9
0 a2 Z, D; \1 Z3 |( T- W' K #define S23 14
1 t7 o+ R/ H* {* i; u7 [6 X#define S24 20
# \+ g7 z' _; P$ ^1 w7 D9 H' F#define S31 4
+ ]6 a% q" x a$ Q) `- D! t ~#define S32 11
v/ p; v [& Q5 O! o& |1 K, G7 ? #define S33 16
( U! s9 F8 N/ J- L- \ #define S34 23
; L$ j7 p, m4 v9 ?9 y( ~5 r( W6 N! a4 E#define S41 6
" z/ u) j8 t2 I9 k0 M5 g; |#define S42 10
5 G! n' b9 e2 w9 B6 ?9 [* W$ C3 q #define S43 15
q# C, U* `7 U7 T #define S44 21
: v7 l& d7 W3 L* z0 g# J' D( @
6 B$ F8 f) c2 X2 w% `7 `4 a# estatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! [ Q/ m! t5 F" B, m. O$ rstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 N. z' O8 |) p5 P, u) n2 O: T static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
3 d$ \! g& C Y! u8 ^static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
2 ]8 |2 n+ C7 ^3 z Lstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
) g* }) ?: J% Z1 M# W' ~
9 d" K N/ ]1 l( o( z* w( istatic 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
" K" C* O* s: Q ^* x) w};
U, Y2 z6 t& F# y4 ~
3 S' X! \1 z% f; M: F1 s0 d3 p /* F, G, H and I are basic MD5 functions.
*/
! O- [! l9 O7 y+ M6 n0 L: i#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, G! @( T2 G( ]. D) n" A #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( @4 n- ^! L$ F" z. a( r; k q #define H(x, y, z) ((x) ^ (y) ^ (z))
$ {, j9 e' F2 p8 k8 w) l#define I(x, y, z) ((y) ^ ((x) | (~z)))
% A8 ]# a) C, Q9 Q
* q7 M6 k. }' d7 x* a1 J /* ROTATE_LEFT rotates x left n bits.
*/
, _4 M! N4 ]2 ? #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
( `' ?& t- r! h3 n
/ I6 ~9 T& N9 i) C /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* D# x2 V J9 M& RRotation is separate from addition to prevent recomputation.
*/
7 _$ P) u9 H. 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); \
}
3 q# X: L* a2 F: t2 z& e l* N' `! G#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 \. M' \' k/ k$ m2 Q7 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); \
}
' K! a7 O5 X. C4 w#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, U0 Y# J/ S+ Z# V) p: s' p
* |) y* Z& ^! @ Y3 ^ /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
u+ |' u" R1 n- ]+ ~4 _/ I3 Xvoid MD5Init (context)
+ ?# j. h1 t2 v* T% QMD5_CTX *context; /* context */
# n. w' Y6 S$ w% a{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
) H' w ^9 S( `: C- s% T */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 u8 j0 [% y& N2 {6 j3 K' x6 @ }
: m( T0 y# e% T" I1 B
: l( E$ ]' g* Y2 ^/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
: \* \, i* C, i; c void MD5Update (context, input, inputLen)
, W% O4 V" r5 XMD5_CTX *context; /* context */
8 `) _# q; y( q/ J5 I3 A& \unsigned char *input; /* input block */
, ]) [+ o5 L1 I) ` unsigned int inputLen; /* length of input block */
2 y; Y5 E1 r1 U# i* S8 I4 ~ {
unsigned int i, index, partLen;
- o! ~ k, {7 y9 V% L0 o& E0 p: o
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
6 y4 M: b3 R# c5 _" F t& a
/* 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++程序


( V! `6 M0 e" o/ v; M5 }
: V+ N. Q( N; Y: ?) Q( O5 Q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-29 16:59 , Processed in 0.058295 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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