中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ k& r/ d& \3 H& @; E: ] #include "md5.h"
) E1 M% Y3 O/ r# l9 D5 b) K- X
1 V" h: ^( e8 n/ _$ Y/* Constants for MD5Transform routine.
*/
^, {6 s7 e% V/ d- Y5 n, d" K5 _
7 x5 S. K; R' c/ ?: A* y" n$ M
- m+ ?% ~. m) A+ y/ Y5 S #define S11 7
2 }* F, n2 p6 }4 U# n, ^' O #define S12 12
* ^* _6 r1 D0 h: z7 a( F, ~0 x #define S13 17
( P6 d$ [, E' @( i( w #define S14 22
% e1 q5 }5 \/ [+ M# |#define S21 5
- x& p1 o& }( K#define S22 9
! R; @$ b, F7 O#define S23 14
" q8 _( x$ j( G# Q1 \1 a#define S24 20
! t; W% C) q1 G% j. e- { #define S31 4
: J! e8 e2 C* P1 L- o+ a- {#define S32 11
7 X& k- X( I+ j$ Y: T) x#define S33 16
. i) a! T3 x6 A& |8 ?9 k. d#define S34 23
3 s" ~4 Y. i0 C7 K. m- X# @4 ^ #define S41 6
. { Y" j O a0 t7 Y8 f3 s n #define S42 10
" d4 N( ?3 u- N& P; y& v #define S43 15
7 \+ o# X$ C3 l/ L4 R' s% j#define S44 21
9 l& l! x9 G; T% G# L* H( p* b
* H+ w( o9 A7 ~static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
! M- j1 Q# V& O5 g4 M7 q static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
; s- q' w& p) N0 b9 fstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
' i' t5 ^$ W/ I% \static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
! q6 E' |- L/ g static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
: }' W( y t- x
4 a* r* S% D# O0 s/ [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
$ N2 e9 F) {4 z8 v/ O }};
6 n, ?; a* ^- k8 P2 s
5 q2 S4 l6 M V8 R7 k! a$ U /* F, G, H and I are basic MD5 functions.
*/
0 {' f# H) V- L, ] #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+ I6 n7 r6 g9 n7 q& K #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
8 A4 N/ N8 r: E: D" v4 N- Z& g3 e/ G#define H(x, y, z) ((x) ^ (y) ^ (z))
9 C' G, g! v% a( c* ^#define I(x, y, z) ((y) ^ ((x) | (~z)))
9 Q8 E& ?* g9 T f8 B- s
& c6 L. D9 B, F% y /* ROTATE_LEFT rotates x left n bits.
*/
" d8 L Q& h0 K0 Y. m, v#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
" y* F/ O) Z0 R% X7 d, ?9 w% W
0 e$ Q, m2 ^/ ?: Q5 n8 u, ?/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
" _2 g* @ {! D4 r& uRotation is separate from addition to prevent recomputation.
*/
; I9 ?( M8 O6 _) [- p$ ~#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( s2 d6 |8 y8 A [" i#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
3 E; r! J7 S2 [7 a0 o3 y5 e #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( _8 I8 t8 J' f' ] #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' g3 v5 j u! t/ p" a! _& h
) E+ u, G E5 X2 c! r; w, e/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
) s2 T) \/ [# d) w3 s/ Gvoid MD5Init (context)
4 f$ P! r5 I$ F: c4 D MD5_CTX *context; /* context */
+ F" I3 Y6 e4 j5 u7 d{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
" W: \8 }- o' h; \ */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ h$ Q2 M( K2 k+ f$ B. C. y1 ~* f }
& v. \5 z+ `) `
7 J5 _9 ]4 c' q9 ?- F- ^/ L( c' { /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
8 h" t& G2 k) y; F( I void MD5Update (context, input, inputLen)
/ |8 ~+ j3 B2 b" Y6 c0 XMD5_CTX *context; /* context */
+ T; @7 N& Y- S( {& K unsigned char *input; /* input block */
' N2 P: d! v7 M \3 |unsigned int inputLen; /* length of input block */
) X+ K* ^; G" T {
unsigned int i, index, partLen;
. m3 {8 y8 c& V. v K( z) {
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
* ?$ {9 q0 a& G$ H
/* 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++程序


# i5 M( }7 _/ n( F! ]
. b, x( o; g# N- E2 K A
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-23 16:50 , Processed in 0.130485 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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