中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
3 W& ~% b- a D* i- z #include "md5.h"
: q& s' v8 f: c
# W3 P |7 N- L0 w1 X# s0 j$ [" \) C /* Constants for MD5Transform routine.
*/
+ N1 r6 W L/ V1 l$ e+ f
1 o9 I' _& K! b2 N
& i1 ]8 ^# d7 \, C; C- P#define S11 7
( |! P/ o7 x/ s2 z) N$ i, [# ]: o' i #define S12 12
$ c( G8 G5 o5 O7 b# D# ?+ I #define S13 17
: ~* J2 f' s* C: Q5 w ]8 B #define S14 22
1 L" `# P* x% C) x8 [0 v3 \#define S21 5
& H0 o4 ?4 A% M: C& n% y #define S22 9
& L! ?' U! l6 m #define S23 14
9 N0 g9 ^3 Y: K! P' \' N #define S24 20
$ c; i Z; o5 Q; d9 q" W#define S31 4
+ U3 t, a4 K) q) y#define S32 11
( z" q7 @; b0 q5 O, t# v! G #define S33 16
- h( ?3 X4 r# h #define S34 23
9 a g9 Z4 ]5 M" X" x' O3 r7 B9 m #define S41 6
7 K6 ?, q* u6 ? P2 C/ \ #define S42 10
% b* `, \' {2 e0 \% T! J #define S43 15
0 f& M4 N9 [0 h; S3 p( x! w#define S44 21
, w6 b$ W" R. h/ k
) n: J* Z( c. `% Ystatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
# S; c- o& A) b! L* d- J! vstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
7 F! @0 H! P7 a! I' Kstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ a6 `! [2 ~% A( E- H, K. s static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 S1 |) j9 V* m$ O% }5 v static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
I/ E3 Z! J, Y* R2 `- @7 O0 U8 `0 z# o# P
! e: ?# Y, k g' `3 L2 Cstatic 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
6 v2 C0 _8 H4 S- u };
0 z0 d& }1 g, Q: x4 }& j( R
# S2 Y! H# s# e$ M+ N, {+ ^/* F, G, H and I are basic MD5 functions.
*/
( L$ y( c3 `3 I3 ~#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
& a2 o0 ~" Y% D #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
K) Q1 I6 J- E% v8 |6 u#define H(x, y, z) ((x) ^ (y) ^ (z))
2 I9 {$ v* H2 ?# l2 D7 @# j #define I(x, y, z) ((y) ^ ((x) | (~z)))
# V4 g9 ~9 O$ u4 S- i" u1 w
. `6 d7 q9 Z+ {- u+ [2 G! B /* ROTATE_LEFT rotates x left n bits.
*/
( w3 {5 }# o2 E6 U0 d" |#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/ _" V- d7 K) F+ w, [
# I+ x0 P) k+ K* w* E /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
' T4 E* x+ C) m/ DRotation is separate from addition to prevent recomputation.
*/
1 q; r1 K" Y" [# h* p" x6 B: m #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
! [# Y% x( i, a" C9 Q #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
+ @- v4 o% v) ^: G! Z#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
6 ?7 T( x; f$ ~* _6 t/ v#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
8 E. U' X5 ^9 y- \9 K2 c
+ w7 g: u. v" L& _7 S5 I; `/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
% X' F2 \# r1 G9 M$ i. n void MD5Init (context)
9 q' ], R" X2 W/ [2 E MD5_CTX *context; /* context */
! T& e3 F9 ?# U+ M' M$ S {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ H: k$ ` X5 ^* C* e, T) A*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# }& M+ D7 F4 q9 i u1 c }
$ D8 S1 f8 V( v1 |$ o% }
& O; w8 d9 F E( o5 ]" G) a/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
# Z2 S, f2 ^$ P void MD5Update (context, input, inputLen)
0 X2 w) f' P1 OMD5_CTX *context; /* context */
! T5 N. y" M# X# X" z2 L, h7 \ unsigned char *input; /* input block */
& H# T0 d6 v1 n% f, H) J4 ^ unsigned int inputLen; /* length of input block */
* e* F: p/ k' k; \9 D) R6 d {
unsigned int i, index, partLen;
6 J) ]0 t9 {2 q4 k2 x
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
" m `: i5 P8 R1 F; 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++程序


d- C Z7 g5 ` X( f
( V, o3 X+ V1 q
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-15 07:05 , Processed in 0.055224 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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