中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
; ]& B }# Z# [7 a! L8 A9 C: } #include "md5.h"
5 v a* I1 W( @/ L4 S
9 c( D2 W( R9 c* g/* Constants for MD5Transform routine.
*/
5 ~8 `$ [: q! [( p \8 p
2 p; H, M' j0 G+ h F; |0 F5 [
l/ V% N8 P1 Z4 H/ o#define S11 7
4 r8 F. b! ^: J: N #define S12 12
1 z9 n& X/ C9 o* U+ `#define S13 17
$ m7 J$ E# U9 l! w$ W9 k, K #define S14 22
$ {7 z/ R6 S" {( O& x5 q#define S21 5
6 N- Z. t$ {1 `3 [ #define S22 9
% ]6 O2 R" H6 H5 h0 t$ [4 m#define S23 14
3 w5 a- g) `3 B1 e# O #define S24 20
7 U4 H$ f+ g$ L# H #define S31 4
4 s ^+ u6 U$ `8 N+ b" U5 ?( H #define S32 11
) q1 Y+ F$ t" c3 P2 \ #define S33 16
7 S1 X# v5 Z2 r" Z+ g#define S34 23
% g6 K3 E( _' D4 i7 f #define S41 6
+ x0 S" e2 z# z! y- [1 G, W* P #define S42 10
, m1 [$ y4 o0 K* V% x#define S43 15
: l3 O" n0 n* V; K& V) v#define S44 21
# U- ?/ ? c# R X, i2 O0 R9 m
+ b0 k: g# d) e static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
) j0 o4 \8 b' a1 p, r static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
! V* ?/ {9 P: o$ ^ static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
, [+ j7 s$ L. f- [: D static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ Q# z: u( O9 R; Ystatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
1 I) q! O) t# c" o1 u
4 ~' `. A# m0 Kstatic 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
4 Z+ q, x" K5 F};
7 F. ~. r8 b. R& G: e" P* D4 B x! ]( A) u
. S1 u5 h% L% l( z- ` ~8 x- X0 @, m /* F, G, H and I are basic MD5 functions.
*/
4 w; k- t2 O6 W$ z; v+ S! } #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
, ?" ^2 ~# B. e! `7 U, j" m( O Z #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
' H6 [0 |4 A4 Z. n7 U #define H(x, y, z) ((x) ^ (y) ^ (z))
$ ^& V: ~) [5 P #define I(x, y, z) ((y) ^ ((x) | (~z)))
6 v" ]5 u; w# f' x5 s$ Z2 f i+ x
m" p# ~3 u I) o. {/* ROTATE_LEFT rotates x left n bits.
*/
7 z; r! ^0 c Y+ ^8 H' G#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 U3 z9 r+ M; V' l4 A
/ v7 a r9 \& h( v- f; Q' t! k /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
4 b( F: z% \5 K/ d( R Rotation is separate from addition to prevent recomputation.
*/
* B c5 y+ e4 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); \
}
0 u! D. K# d0 O$ |! {5 M& 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); \
}
. P& N( V7 K$ a#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 y$ ~: c$ C9 L L8 N, S4 u0 c #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; u5 ?# c7 a! d6 o6 O
. L! D4 G' Z3 N. w- H: ? /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
$ `7 x }0 R/ i+ P: H7 Wvoid MD5Init (context)
. r% A. x) ^' Y+ b/ Q4 F MD5_CTX *context; /* context */
) u5 g/ I6 a5 A9 r{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
# S6 d+ C: a& I' d M7 }, H+ m */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ S; X4 i2 J, A! k9 X+ T) N }
( Z. v& G$ ]0 m
+ o2 T, ]' u$ U% w, W/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
- [9 m, l- j/ d- mvoid MD5Update (context, input, inputLen)
3 M* \! T, @% t7 g1 `- v9 C7 BMD5_CTX *context; /* context */
6 A) {' J+ R4 q unsigned char *input; /* input block */
7 R" n% ^% o/ R0 E" runsigned int inputLen; /* length of input block */
, N5 d# A( D% \5 w" k5 \& S" s: |{
unsigned int i, index, partLen;
+ I t2 L# o: ~7 ] N8 P
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
: X2 {* K0 S- a% b" I5 |/ S! q' b
/* 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$ m! A. j4 V w6 P5 p) R
9 R# L7 C' S3 U
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-4 03:38 , Processed in 0.082048 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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