中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
$ h) M) w2 `" G9 p+ E #include "md5.h"
& x9 `6 b, _! m% ?% P( m
4 I) _6 e7 u2 `; D /* Constants for MD5Transform routine.
*/
6 J5 j' r% h0 Y+ T3 {
! i5 w! ]# h0 y' @0 R! n& m3 ?8 w
( s( U5 {6 C$ C L #define S11 7
2 I: f1 J5 ?# W M' v #define S12 12
r; Q: j8 U$ k) j( P6 z #define S13 17
$ X3 r% k% u& k }* {( }" h #define S14 22
7 J# B7 {7 A% h/ E3 P#define S21 5
* y2 y/ L3 z: R' R$ F2 X# U#define S22 9
4 Z( ~! |9 `7 ?, b% a. o #define S23 14
+ [. K+ g0 |1 a! y5 K$ {#define S24 20
; R f) s" t; e#define S31 4
2 c6 }& n- I* D/ R#define S32 11
% e: q9 @( l# h. k+ N+ ]; Y1 r0 V#define S33 16
7 E8 m1 v1 l# X% F8 }1 L9 w1 @- @ #define S34 23
4 [9 K; P, t5 r6 j, Z/ ?! P: G, V% y#define S41 6
0 q6 k: `# r. n, w$ g! d #define S42 10
4 F7 [1 M' A* T. L/ a& |; g#define S43 15
: d8 i" l0 M* p; M #define S44 21
* i7 R" b4 y- P, ~# ?$ P* G
& f2 ~: d6 }# }8 w' U: M: C/ i static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
5 t1 T: m% f7 a% O0 \; A( vstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
' C9 s8 H9 f# l7 ~' A% ~ static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
& v4 D6 O6 C) ?2 r" Cstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
0 b# O" |. p6 |* Q0 l( ^& G- \ static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# N' x) Q& C* d# M" ?, |
h; @% X5 K' a* Z, C, z3 k 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
3 H O% b' N0 l6 c };
! v) t: r+ H9 B9 u& l0 B
- h1 @# [, W# g' m# B0 Z6 S/* F, G, H and I are basic MD5 functions.
*/
9 z' M" M# M( N z) X, F; A7 p #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
( k# d8 O; `- Z* h9 q7 e#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
' ~' l2 H, z' C$ A7 M, D& X#define H(x, y, z) ((x) ^ (y) ^ (z))
! W; ]( u6 b9 H: S0 T4 c #define I(x, y, z) ((y) ^ ((x) | (~z)))
" n; r3 j9 r2 x. o2 [
$ d! Q2 n5 P8 n /* ROTATE_LEFT rotates x left n bits.
*/
6 C) u: @$ S+ U! p# v8 L#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3 |) C: w& t8 B2 W" ?$ y8 `* h
5 G6 H( G0 C7 [. X% l: K! a5 d/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 p8 Z, g7 c4 a8 r; z5 |# L Rotation is separate from addition to prevent recomputation.
*/
4 A+ X: \1 V2 f& z* F: o#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: O+ R( K* J* \ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 \8 @! }2 t( ?; y# F! [- W #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
; a$ f1 H1 N2 J# B! p- 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); \
}
, Z2 @+ j: F! {: b+ @* G; _2 e
* ]" C* c; K/ N% N' u: q$ {/ o /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
1 ?3 Y# r/ D C; I; h1 m* Svoid MD5Init (context)
' Y! k; O) g# W, l MD5_CTX *context; /* context */
2 n: R% O) H6 B7 w7 [8 Y{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
$ G' R ?9 k) P; d% {; n' u( G*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
# I; s9 ^ e! u }
# ^; u' }/ u3 p% v
1 t. L' S& ^$ F- X/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
2 v9 ?! v5 ]/ k+ p- D: f7 s; L; f void MD5Update (context, input, inputLen)
1 P; e# F! }6 y! m$ E5 {+ { MD5_CTX *context; /* context */
" O ^! w$ G/ sunsigned char *input; /* input block */
$ I# _6 b+ W9 Q, Uunsigned int inputLen; /* length of input block */
* V h" G5 Y6 z! T$ l+ |{
unsigned int i, index, partLen;
- H1 j1 ^1 A1 J8 l F+ {9 B
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
0 g5 q8 U: h' 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++程序


: f' q% l/ n8 k/ @, \
3 A: `$ i% y/ k
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-15 14:18 , Processed in 0.066931 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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