中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
2 I9 }8 N9 L5 {) i5 w0 p3 ^0 {#include "md5.h"
5 u9 v6 `% F' Y
* `0 O/ u" }8 V- p2 `/* Constants for MD5Transform routine.
*/
& |$ D5 {5 n% i
7 F" w9 V* l. ?/ f: S- Q- F" ]6 J
; H# n5 Y! f5 \/ z8 r' H$ F #define S11 7
# S- ^, Y( I/ Z0 j#define S12 12
8 z2 W; P8 j+ c# s4 A& P#define S13 17
& L$ r/ s' x h* L" A$ {& R #define S14 22
6 L& @0 l2 M, k& l5 t #define S21 5
8 s: p+ M$ S3 t9 a& w: ]2 Q- s- ]9 z #define S22 9
8 r0 Z1 d: f! S$ S#define S23 14
$ z( `+ D/ }! X! [ #define S24 20
5 Z7 |# O7 Y( R, t# g7 [/ g, }! D) j7 m#define S31 4
- p5 U; j2 V# E. L$ U ^#define S32 11
- n2 M% K: {6 d" k; D' q #define S33 16
' ~; d. h% ^5 V3 U, c$ p# A9 F: M) H #define S34 23
$ N+ w9 S+ u% n7 N# ^5 } #define S41 6
/ w4 t3 R/ L9 \ u' i#define S42 10
% h& i6 z: g1 u5 b$ L) D( ?" z t#define S43 15
8 L: N: _# v+ M. B7 _$ L" g6 E #define S44 21
0 n5 b7 i/ U" V0 W" r. h+ g! [
$ u, h6 v6 e1 T2 u( m' Jstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) h0 c# y+ p# G; X static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
$ H* [- m. X' w4 a7 ` static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
! g( `9 C/ `# _: D$ P/ d$ @ static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
- z4 \9 f8 C* v8 @& O" Xstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
! Z# o4 D$ S9 a; |+ ~2 y
+ Y4 e( r3 I9 C5 N2 Y: y, P' I& Gstatic 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
7 t+ V' ]0 R, u1 U7 M};
$ k& l. [$ Z1 m1 s" }' o) h
3 p" ~- V9 \* P+ n2 B5 R /* F, G, H and I are basic MD5 functions.
*/
+ n, r6 U: J4 ^* y#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
* S; i8 Q. c* V( Z9 S #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+ E- B! x* T) w, C' U$ H #define H(x, y, z) ((x) ^ (y) ^ (z))
0 W, v: ~7 N: q: B! Y' |; b; e% e: l#define I(x, y, z) ((y) ^ ((x) | (~z)))
3 ~4 |% v( E* d9 \2 U% D: C
, I" x- h9 X9 |/* ROTATE_LEFT rotates x left n bits.
*/
7 c" e; \2 }1 ~% n #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
) c3 y6 s, ~0 |7 U
% Z; {, I. m4 K: E( ?2 V/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
8 L" ~. k3 @- }% c Rotation is separate from addition to prevent recomputation.
*/
0 I7 h; x7 ~) Q" e0 Z+ ~#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; X( p1 }$ W1 v/ d: V#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: y+ z& M' c( v# H #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. V# `% c- u- h5 ?#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
" v, U3 ?$ x3 y [" G
% G& W* ?: I7 w /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
4 j0 X4 ?1 }, o7 z! D: A void MD5Init (context)
8 g5 r. d5 Z2 t# oMD5_CTX *context; /* context */
- J( @9 C8 c% p+ V{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
' _' m. @; I: C2 z j, w- U- P*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
7 Z5 ~6 V4 V4 m9 T& q}
, k! @4 |& o, e
9 b1 ^# `, a4 t! n7 A6 Y/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
. O0 @" v y+ _% s% b* m4 g& { void MD5Update (context, input, inputLen)
5 B" E/ v8 ]: M! x2 r" d% o, M% d3 EMD5_CTX *context; /* context */
% ^! J: V9 w4 @' h, E9 Q unsigned char *input; /* input block */
% P+ n$ c6 R3 Y# ^" t( D unsigned int inputLen; /* length of input block */
z8 ]* ]" B# |1 _6 f+ G {
unsigned int i, index, partLen;
3 C- w1 C9 J9 N$ o" x
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
^+ _7 M. p, U9 c
/* 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++程序


/ J J& [6 o! z: S4 c% W
7 z& ~6 z3 U$ C \! v
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-22 08:29 , Processed in 0.058127 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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