中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; P4 y! t; V2 ?8 V. k" E#include "md5.h"
# C6 i8 t* \( G5 @) ~) N
' z1 t' K7 j2 \9 o! Z% H, T3 X3 n6 h/* Constants for MD5Transform routine.
*/
/ U4 J& u* u3 w
3 J5 A0 k1 U2 r3 T" z6 ^' y
1 _ c9 F o6 i9 r" P* t#define S11 7
! E# w4 @9 l: ^& U* C, U' [ #define S12 12
! R7 y* W& z9 F#define S13 17
" n& @5 O8 ? a#define S14 22
6 g6 m4 h1 R6 w4 E: T#define S21 5
8 }$ d- z" \; m# v5 y0 @* W #define S22 9
]0 @- o9 E W3 q #define S23 14
0 s: p# b2 {- q1 L, U#define S24 20
5 y, |" ~6 h2 @2 }3 T% M4 Q- a #define S31 4
# v1 x$ l- ?+ h #define S32 11
* _2 ~. ?; |' X0 ^- k #define S33 16
7 d# g) W/ _2 u4 Q8 A#define S34 23
" n$ a+ F- q! b: b #define S41 6
0 K! q# x" g$ P' r2 J, { #define S42 10
z# K1 z+ l' o2 b #define S43 15
) `) x! e# L8 n. T #define S44 21
" b5 i/ n+ f) K9 k8 D
6 _7 N" o+ W6 ostatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 |& J7 F/ q# w. D static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
/ E4 @! N: d+ Xstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
7 n( R% s+ Q, ? static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
& J/ M y& J+ a. k3 P# Jstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
8 q7 j2 X/ j6 y9 S+ j- A8 `5 W# m
/ a5 N/ L3 M- a! }& p) I& B ` 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
- C. w/ C7 u7 q9 F! s" Y" s; B1 p& V};
3 V- H3 I; q+ i/ H% [$ n
; L! m* D$ I" a5 y /* F, G, H and I are basic MD5 functions.
*/
, Y$ W2 ~- ~1 j8 r# Q #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
# S7 w9 j3 _+ f$ M- N #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
; E0 B5 O. l- X# i$ b1 v j; \ #define H(x, y, z) ((x) ^ (y) ^ (z))
0 p7 C, s* T- Z' m9 P: m- z- j #define I(x, y, z) ((y) ^ ((x) | (~z)))
+ Q/ e) Z/ E% |5 r8 m' U' M
; V4 X( ^6 |0 E /* ROTATE_LEFT rotates x left n bits.
*/
+ g; q, u: {0 j7 U; ~* r#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. q* M( ?. X# R/ \0 z2 k2 l! n* K
6 i3 y k/ O. \$ | Y$ V A/ F- M/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
& f6 j5 J+ ?2 v; @, V- W Rotation is separate from addition to prevent recomputation.
*/
; k9 p6 |# Y1 M2 b$ e' K' h#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
* H4 d- w6 U6 d#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: g7 e3 l! P6 ?5 ? #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 l" |9 V9 V. w# |- O+ _ #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 ^3 Q1 v( p+ a9 z
2 q0 P! }/ [" k/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" H# \% L* {; |3 k& }void MD5Init (context)
! K. W9 ^. S9 f# U5 m9 d3 D; _ MD5_CTX *context; /* context */
" j8 h8 d3 H6 ~& t {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
: Q$ F! \+ V3 F. O* U I*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
) O' ^# H9 S0 ^ }
7 Q8 u2 m( v4 R, J A" j5 i
" }: n: r2 j9 P2 }0 j# Y9 s1 y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
) Y8 s1 a6 W1 D8 Y- r5 F* jvoid MD5Update (context, input, inputLen)
/ S! I6 d& q7 Q) a6 X MD5_CTX *context; /* context */
9 @+ z2 q1 s) P! @unsigned char *input; /* input block */
3 g b( Q1 M9 b unsigned int inputLen; /* length of input block */
4 E7 _* n$ }* f {
unsigned int i, index, partLen;
& L, X8 C P' R; w* D: U
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
' B* c' U$ x, b* H2 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++程序


0 ]- y' N0 `4 L
) e- t) C$ o/ N2 Q, X ]& E
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-6-6 14:29 , Processed in 0.061518 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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