中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
" i0 V/ R+ k" o0 B) J( a#include "md5.h"
+ A4 E8 c% E: V2 k- ]2 D( c/ a3 L& J
% N/ {" p7 W1 S) r8 A# h/* Constants for MD5Transform routine.
*/
9 p, z! q4 a5 r! t- O7 y# I
( F% r9 u. s) V$ ?4 q' f+ X$ }
4 [- p& a+ c. F6 c: M#define S11 7
' U. R& a& _5 B$ n #define S12 12
1 b9 H9 g, v i7 F6 U #define S13 17
9 E+ J9 n1 C/ b, ^+ {: k2 M #define S14 22
% @" U$ U4 H& Q6 w7 ~#define S21 5
% z* ^) p7 G- E# c#define S22 9
; {: ^2 j, b, i- ~#define S23 14
. R: D6 Y5 \6 [5 z% R4 R #define S24 20
9 ~7 h! Q0 h E( O( Z#define S31 4
6 E5 A0 ]7 \) l) `2 b( ?: ]0 @ #define S32 11
1 Z3 S. b( X$ s6 D2 }7 Z#define S33 16
n$ w/ P$ p: }! d6 R* b% q#define S34 23
0 X* p$ H1 `0 l5 ?) ? #define S41 6
4 D" a5 r/ [$ [2 {( j #define S42 10
: l5 y2 D8 J4 P. }2 A6 w, d#define S43 15
* T; R7 |$ t4 d. D9 q+ z& X #define S44 21
" P" ^7 M. ]8 t$ z# v
$ f- M; B c! V& U static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
- s; T# `) Y7 r static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
* a+ B0 n3 c+ U! L$ m static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
7 B2 \" r) K5 S4 h5 _static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
" y9 M* U; i: I. c static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
, z3 D4 {1 G& i% g. p
) K% S5 b5 ]$ 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
( Z& A) {! V3 u; F- P6 Q% D };
& L9 K1 ?% L, M' N& m% `
8 l9 y* k- r: L+ t1 O/* F, G, H and I are basic MD5 functions.
*/
8 B' V, Y. P! a8 a6 J/ ` #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
' j1 D% {7 z/ o( b5 D #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
( y `- ]/ k* I- J! H* [/ F #define H(x, y, z) ((x) ^ (y) ^ (z))
8 {0 R/ j- W6 O0 t* n+ Q#define I(x, y, z) ((y) ^ ((x) | (~z)))
, p7 q: {# v; [/ z0 [4 @% O5 o
$ v7 E$ m, m* B8 T8 s8 v( I) E; C /* ROTATE_LEFT rotates x left n bits.
*/
3 _3 v2 } ]" z- a# j#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
4 M4 s& @, `9 ~& Y5 d
4 k- B7 ?4 V; l& x8 M/ R! p* x/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ U. S7 I8 o' P5 p; K- XRotation is separate from addition to prevent recomputation.
*/
, e- h: @+ |. [ j9 E #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
r+ b& i' `+ g( s- j5 f- w; C #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; k) U0 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); \
}
! }( D2 R- K; y/ h. X7 ^0 R #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
0 _2 y, @! w. a2 i1 a
% R' V/ C, T' D8 t: g /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
; M$ W2 n! f, a* d; C$ o2 P4 p \void MD5Init (context)
, n0 r8 D5 m, N6 w MD5_CTX *context; /* context */
3 {( r$ w( y. B- {( D) q {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
* V4 Z0 P' b- i0 q4 _ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
( ^/ W/ ~4 y @* E* e( N1 |1 u }
' L6 e0 C& `1 T; `+ ?
: N7 ~! \' j$ q! z2 A2 K/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
0 X! r( z* r2 _7 B0 @void MD5Update (context, input, inputLen)
; \1 I w2 R1 ^+ \# t z; [MD5_CTX *context; /* context */
. m" ~. a3 E* h; C" l unsigned char *input; /* input block */
1 V) i+ j5 c4 l6 ?6 {% U$ munsigned int inputLen; /* length of input block */
" [: p% D2 e. n4 B {
unsigned int i, index, partLen;
* q; ~1 o8 b+ ]# R S5 ^
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
' w7 B' {1 x A/ x3 q! p! ^
/* 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++程序


9 z6 ^1 p# `2 p- C* Z" i/ D
& B/ T* p8 z6 `2 x& y! ?. N
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-13 18:03 , Processed in 0.058532 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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