中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
- P) L$ S4 z# z #include "md5.h"
/ L7 d0 H; q1 d! Z9 }2 `* o, k
7 p7 C, { S" R Z; h* g4 U; P" j/* Constants for MD5Transform routine.
*/
, ~ K. `7 w: G+ N9 {( @7 g
7 B* i' I4 @- u5 {$ w) f- K
; ]8 R N) y2 h1 u: P/ P #define S11 7
4 t- d( m/ T5 w. _ #define S12 12
$ @! |+ H* ^/ m# U1 k5 I #define S13 17
, F9 R/ N* P1 @+ J' E5 g2 |#define S14 22
& d$ \# t8 c; Y! n% ^3 L: w#define S21 5
5 z# j9 O% s2 X! Z( {#define S22 9
. R! m! D5 D( m: f! Y- J#define S23 14
9 g, ^. Y6 M! P7 ` #define S24 20
4 m0 c$ c( ]0 X$ v9 I x( ` ~ #define S31 4
5 L& t+ N; p$ g" c #define S32 11
" |) s, ] r8 q* k- z* x #define S33 16
- g- z6 a' E r, k#define S34 23
% Q$ g7 K/ A( L. n5 J( w @#define S41 6
2 L! d. ]. ^- ~) ` t$ R! Y, ]#define S42 10
. E4 g$ T6 X. S* I9 a#define S43 15
8 M. i; c) Y" F' v2 H0 B$ r#define S44 21
! W9 P/ L+ R1 q( R2 i6 s/ x* v
5 Z i: f0 O6 z+ J( h0 x6 M6 fstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
$ g) K# H' V* z; l! b/ Kstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
* m1 z+ A( _4 ?; @& p, H( b$ x static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
$ t1 v/ Q6 c nstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
5 K$ o9 q5 p0 v5 C2 L static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. u Q( z. _! T3 E9 g. X) m
2 j1 w' [2 l; R; Cstatic 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
- x, S8 d4 _$ t. J};
, ^* J) s2 O- o
1 _3 m% m* L: z2 F' E3 w' N- b& d /* F, G, H and I are basic MD5 functions.
*/
# n) }3 g- c9 i" ^% K: ^; a2 S/ I: p- v #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1 y5 D) E$ g9 Q* U #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
2 E! O1 i# n: I# I# o* l9 P) ^ #define H(x, y, z) ((x) ^ (y) ^ (z))
3 o, q8 e8 F+ C7 V/ l1 R9 f #define I(x, y, z) ((y) ^ ((x) | (~z)))
" j, {3 a; C3 o E3 a7 c2 e
% C) T4 ^: L: f6 n! m4 p% |/* ROTATE_LEFT rotates x left n bits.
*/
! a9 ^! m6 \5 t+ R#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. P4 l: d- \" U
$ H. J! ^* h; T7 Y1 y( Y# b /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
' z. g0 y. w; A! d Rotation is separate from addition to prevent recomputation.
*/
: V- Y {2 V( j1 x2 v#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
2 D* h( o4 c& y, P6 M8 X, h/ o#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
, k6 i+ o9 Y" I) k #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: u! G+ ~6 C" e0 f Q5 ? #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
. e% Z1 c4 E. `6 k3 h7 ~
2 B0 ], L* y- {9 U9 j /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
! Z4 C7 p- y/ ^; Nvoid MD5Init (context)
4 S g* y" z; V; f# L4 P& RMD5_CTX *context; /* context */
. @6 W7 z6 }7 `( R( V( z% w{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ f1 v5 z* }9 E*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
; B& ]: V9 D* \" |7 ] }
$ Z' T; R& b) T1 z+ i7 r7 U0 K. l4 A
; A" J" V9 ^" N" U. d, @7 ~& z/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
+ F" s! [1 ]' W# Q& I: z* e* } void MD5Update (context, input, inputLen)
! l d$ v) a" \, P4 V) u MD5_CTX *context; /* context */
7 T5 Z5 l* W* W9 d6 d" C unsigned char *input; /* input block */
" r i# O8 F* A Nunsigned int inputLen; /* length of input block */
, f( Z8 E6 T9 `, F. o; M% |- ~ {
unsigned int i, index, partLen;
& A& b3 n( S5 l4 f8 t
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 |5 |! f, U" S/ I3 S2 Q9 \+ c/ H; j1 t
/* 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++程序


8 i" m4 ?/ c% u! Z3 q/ N) V: e* x
" ^* R8 Y- c7 J
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-22 20:14 , Processed in 0.113958 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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