中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
# p3 w: Z& {: A#include "md5.h"
; L/ R$ \( [9 [. o) V9 m
- F' v L( m5 |/ X( L; C /* Constants for MD5Transform routine.
*/
0 |$ \, i7 U8 f1 U+ f
( d; b' H! L. F( Q
' V( A, t q) m5 k& M0 A4 q#define S11 7
( ~% `. k9 a2 _/ O0 t \) i s #define S12 12
* G0 X) F' V! s8 m. ] #define S13 17
: X) N) V: ]% G#define S14 22
. _" C/ p5 i, e3 _) ~: v#define S21 5
! r) q- A1 h- I" s* r7 h: a2 B; e #define S22 9
9 ]# `; R% A3 W8 R* B' v' ?#define S23 14
7 h* T. ]: z6 g5 m; |% w/ r #define S24 20
( o. x h+ X; @7 v#define S31 4
- L8 r" Y3 ?& v% e# H) f+ u( v #define S32 11
' s6 j q B5 a" i8 S#define S33 16
2 b* n$ b# e6 Z3 m: T#define S34 23
/ P4 C6 [4 r. X4 x. p: ]% T9 Y #define S41 6
! A/ O6 Z# c) r1 | J#define S42 10
4 w# q0 j; q* z$ Q/ R& x#define S43 15
- H6 z9 e0 S n2 j% \ #define S44 21
3 a# z5 H, M6 K9 w) g* m
4 u) O4 F5 T1 b; b. @) Dstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
4 d7 \, U' b. M0 ystatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
) a" j* Y1 W1 D7 Z static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
. `4 ?/ X- N, B4 K' ystatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
8 e/ P0 A) F6 U0 z) S. ]! T/ O; istatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
$ D* t& f- d7 [1 T1 G
+ ^5 S+ Y0 c5 t7 ^) n* |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
- W$ L3 Q+ p5 y( \};
3 O& |; ~& Q( g8 X
6 V' W: N |. j( A' y$ _/* F, G, H and I are basic MD5 functions.
*/
9 S3 [3 u+ I! P: r5 ]$ G) d2 {4 @2 f #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
! g: w. w, T& o5 ~( T* r" b2 c #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
, u! {/ s5 ^9 B! [! L& s#define H(x, y, z) ((x) ^ (y) ^ (z))
# i' w, P3 b# y0 q0 {#define I(x, y, z) ((y) ^ ((x) | (~z)))
" ?& W9 x" q( h
" O9 i* E5 S. B( i! s S/* ROTATE_LEFT rotates x left n bits.
*/
6 T( {1 Q1 S/ @4 ^ R" M #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. t1 w1 `/ W4 n5 s
1 H4 q9 \: y# t3 a! I /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* E( X! \6 d" z" L) h: g Rotation is separate from addition to prevent recomputation.
*/
1 B- X9 u! K {' X& ^2 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); \
}
7 Z7 e+ L/ j; J/ u#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
( E+ ]$ }1 `4 _% [; x8 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); \
}
1 F; a% ^' I/ i/ [3 K& N #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
% y$ t' `- `% u. m2 {$ e
* v- S Z; x# c5 z5 v% b/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
; C" ?; k! t1 u; p/ xvoid MD5Init (context)
2 u3 O' J5 _" |7 g MD5_CTX *context; /* context */
9 i9 ~3 e$ p9 L9 O6 G {
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
( ^! C' S2 y% O */
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
2 O2 A2 @, e7 r6 Q }
J4 M- X4 {. u+ s
, p& T W8 c7 J' e2 e/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
/ u; g0 n5 N. A5 A& |9 ]6 R G; ^ void MD5Update (context, input, inputLen)
k6 Z* T$ |. _: T( ZMD5_CTX *context; /* context */
. x4 L1 q/ j& X% C r+ {% |; ounsigned char *input; /* input block */
" N! @. {; h0 ^; O- P# [ unsigned int inputLen; /* length of input block */
' j) l5 l: a" C, Z* u6 h {
unsigned int i, index, partLen;
/ _* }7 N8 V5 w6 X8 h" R2 Y
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
, O0 F3 b) K i2 V, 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++程序


3 G) M2 }; l% h K1 ]
. r" u- Y+ q# X- R: n( U
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-31 09:22 , Processed in 0.181973 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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