中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
% Z5 t; {0 b# D* R, {#include "md5.h"
2 J7 c: ]: Q% E
6 \/ |/ I0 f& Y5 R4 R /* Constants for MD5Transform routine.
*/
7 c2 Z+ O: n: E) `" {
% j7 ]4 _: x, m4 S6 Q/ S1 s
) O& J. r% }# l& g- H) d2 H #define S11 7
$ L/ g4 ^' O. }# S#define S12 12
) j, B- `5 ~( R3 \9 U- t#define S13 17
7 t: ~9 T* {6 Z8 F/ T#define S14 22
6 G7 G9 n, x; X+ _5 A #define S21 5
% X/ }% k& Z# i# Y+ U" y# R #define S22 9
+ ^0 k, K/ K) t. ]* M% e7 z#define S23 14
5 \( [% }. N! Q: s' \- w#define S24 20
# l* R, c& j4 D7 I% n! y #define S31 4
& Y% I2 ]/ f! j#define S32 11
2 Z9 g: B9 g" A9 Y/ d- F9 G1 J#define S33 16
9 R3 Z8 ]+ ~3 `& a% ? #define S34 23
' H1 E7 `5 I/ H# Q" R #define S41 6
' {' L$ {' ^! y& A: g! @ #define S42 10
1 s; m) N3 B8 D A #define S43 15
: r# G/ J- O$ W+ m( z2 n8 Q* |- O; y #define S44 21
1 T* v) {. a( W$ n% w
0 a5 Y4 D0 I0 m3 Bstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
2 b/ j. ], J2 T2 J3 x$ K5 b static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
# o& N5 I% p" a) J% S0 R* G* ustatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, Z* I1 I! o" T3 g$ o \9 \static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
/ ?0 V3 `7 O: W9 a; H S static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
' {: N; n5 w; _! a( E3 N
7 }6 I) H, l6 A) E1 _ 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
9 e+ F) j- x1 h };
& `6 Q- B; J C$ B* m! Y) m
. S3 W# C1 }3 ^# R/ ~* _( R+ Y/* F, G, H and I are basic MD5 functions.
*/
, _2 L. _2 ?2 z" M* d #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
/ j7 z. j/ V2 d' y# B#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: ^. O! c8 A/ r' d9 n #define H(x, y, z) ((x) ^ (y) ^ (z))
& y, [: t# L- E0 _#define I(x, y, z) ((y) ^ ((x) | (~z)))
% |. O/ I1 y$ t( o2 p
5 _- B& I0 L( X+ B% j3 R' x/* ROTATE_LEFT rotates x left n bits.
*/
. I" G2 [4 I4 G* w1 m! ^: b* g #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) N$ _) ~2 d$ i% h1 h! O( @" h! A
. q* V0 P- E% l% ^! U/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1 U0 B# E6 F; j. K% N Rotation is separate from addition to prevent recomputation.
*/
# g7 k1 ^( U# m- D+ ]( y #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; j0 l, r% 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); \
}
& O2 W# t9 W: _( j! R+ M$ [7 K #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: t8 P; V5 W+ W6 N#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" U) {+ ]/ _' ~; O U% Z4 U6 G1 H
8 @% ^/ H- J5 K. A) T/ ~% X( C /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
# F" } p3 e A% x, f! rvoid MD5Init (context)
5 c ~6 ~" S5 w# @MD5_CTX *context; /* context */
: F1 e: a2 m2 {* O2 V: O0 n {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
4 C/ E- R! m: {/ B z. _*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
/ S; W7 y- K/ U k}
2 m# Z* [7 T1 i3 P
% ~' B/ J' E, U; g /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 Q% o8 J: j4 @% A# Svoid MD5Update (context, input, inputLen)
# e( j a) _6 @& m2 HMD5_CTX *context; /* context */
) _+ Z) \: J; H5 Q2 iunsigned char *input; /* input block */
; A: Y7 {* o z) N, Y unsigned int inputLen; /* length of input block */
# t5 p4 s9 [2 v3 v" h1 G{
unsigned int i, index, partLen;
9 M2 Y" T5 x, e. t3 K
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
# k' j% k; q+ O1 B1 E. E3 t) x
/* 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* l: Z7 r7 I
! a$ c6 a2 s$ n5 v1 [4 ^1 ]
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-17 16:45 , Processed in 0.097222 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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