中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
4 O8 d, U# D% F3 a4 x0 M- I#include "md5.h"
4 u5 {( ^: q7 v( K
7 f1 ]; X9 g' ^0 m, R/* Constants for MD5Transform routine.
*/
; y( U2 O* y4 M( L; W
* j- v4 t8 r7 t7 Y7 J
/ z( ]2 G! w& G; W2 i3 A; t, F#define S11 7
S4 `0 R; d3 ^; q1 o8 q; W #define S12 12
7 G a/ s, j1 ^) f$ K/ I; w #define S13 17
8 A% O* n$ t# g8 D( \/ ~7 Y#define S14 22
' w# f3 C2 K# T8 A1 f+ a% B/ q #define S21 5
( b4 U9 y0 R' ?5 s9 S9 E2 h" ` #define S22 9
, c6 \9 h# S1 s) a #define S23 14
8 [, M/ ~& ^% e3 Z) @$ F9 h- D& t #define S24 20
. O b/ C: `& H* t" o* L#define S31 4
! A- E" J' N2 A4 K #define S32 11
1 p; V; M w4 V3 F" L' Y #define S33 16
/ z3 a V& ?+ d' s6 K( Y #define S34 23
& h7 K; ]% `( w' a8 b7 j6 y#define S41 6
" `( r, W2 k8 X7 |+ B' i+ m#define S42 10
5 _6 r0 Z4 q# i O% x2 Q' u8 C' p #define S43 15
! G- p9 @. Z$ T; u- `/ e#define S44 21
. ^, r, q" t! ]
: {0 C2 p2 K/ S static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 O. `/ l8 z1 j9 b% x static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
0 o1 L+ B$ W9 C1 zstatic void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
* H: C$ i3 B5 a' M& ]0 I( K9 H static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
) a% B5 m, s" `7 g2 g5 @, Sstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
2 |4 `* P0 q `" E6 ^- Q
! {$ ^# J' z5 F6 |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
' T6 `; R0 M" X };
/ T4 x# _3 K# R- O) ~# ]. |
# v" J' O& N! K' A /* F, G, H and I are basic MD5 functions.
*/
6 c, q5 S4 S/ U. \- v5 F2 K" w2 n1 F& L #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
; m2 X' j- Y$ G; t3 { #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
" ~4 h7 b! F) o' L2 e, @) D8 W& q#define H(x, y, z) ((x) ^ (y) ^ (z))
* M3 X+ z# X/ P- t' N #define I(x, y, z) ((y) ^ ((x) | (~z)))
. n; A3 z9 ?3 x: I! e& ~+ y6 A( e
3 D# u; c3 ~# `. D; u* ~1 R [/* ROTATE_LEFT rotates x left n bits.
*/
' W* }& z7 e& ?; g L s#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
4 ]! C% V$ B; e. q2 R+ Z O
2 O4 c5 l8 @. D+ t/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
2 L9 Q# ~5 t3 D. K Rotation is separate from addition to prevent recomputation.
*/
% n: R* y, \) Q #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
1 F# O" ^, x! J1 x3 h #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
7 p4 }5 Q' p' j7 E' `$ b2 N# [ #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
}- m6 O: e6 j( a! | #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; |1 V" {* B, `
- e) u: [6 o: }8 \' R" b /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
" ^# z' ?4 j) a* y: U3 { void MD5Init (context)
/ ]$ F' r+ m: XMD5_CTX *context; /* context */
# j$ Y1 e% M- ?{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
2 ^, B" E9 x" m% u5 {: u*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
+ d6 o8 r: ?& \5 Y! j }
4 P$ ]" S0 M, s. I" a9 p; L
; k2 X! D S3 D: h" u T. T4 L /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
- H% q! }9 H# S3 m: |6 p void MD5Update (context, input, inputLen)
" l. f5 F" E) M$ ~! e( r MD5_CTX *context; /* context */
2 @6 ~2 v9 g2 o" ~unsigned char *input; /* input block */
9 S( f! p" u6 L/ [1 H: X4 \unsigned int inputLen; /* length of input block */
# d% G) |& H' x# l{
unsigned int i, index, partLen;
" M" F' @8 Q6 U6 E. [
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
( j! ?0 M3 L' Z+ d( u
/* 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++程序


g% i3 X& q2 s, x. s! ?- B1 g
+ [+ z7 J* m9 Q# W
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-7 06:28 , Processed in 0.064007 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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