中国安防论坛

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

md5加密算法C++程序

[复制链接]

安防中学生

Rank: 2

积分
147
发表于 2004-11-26 20:11:20 | 显示全部楼层 |阅读模式
#include "global.h"
* ?- m9 h' |/ N4 r3 O#include "md5.h"
5 |2 R/ X5 o7 v& m q7 P8 I$ e
6 h8 w7 |# @8 m, k" B( [/* Constants for MD5Transform routine.
*/
8 K* M; l" E1 O
! C0 h2 V* ?2 ? ^( L
5 K% G4 A2 {0 c#define S11 7
% `9 Z; Q2 F9 k( o6 J#define S12 12
( H& k* @2 X0 @1 g" J, A( [& S/ {#define S13 17
: p4 t( |9 ^* A( ?: q#define S14 22
) A/ X! Z' Z/ R) f, l2 m# N8 {9 W#define S21 5
$ E: d: a7 V+ K: W#define S22 9
$ U* w$ W9 z0 l9 u #define S23 14
3 ?. h" M6 ? w) M- y* i$ j#define S24 20
/ X/ ?0 u5 `1 @1 b3 q! d5 h0 M #define S31 4
7 X2 q5 P6 J; U3 J4 d4 h3 e! J#define S32 11
1 s* d& U3 ?. a2 M! y/ f# s#define S33 16
! {! Z$ s: g( ] #define S34 23
. o. f. T& _2 U9 s$ z. K) B. a0 c #define S41 6
' J" [9 b( t7 S2 m0 k# ] #define S42 10
6 H- A$ N1 m" n/ q#define S43 15
, D8 z8 O1 W2 h. j #define S44 21
1 }5 s I& m) s) t
2 @! r" k7 |: O9 nstatic void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
8 c# P3 u. y& u* F) ~8 s static void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
6 s* ` h& O4 D1 w5 M o: U static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
) X" g0 O; [: L. J5 K2 i/ A. k1 ` static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; I. U$ C& f+ Wstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
( Q' ?1 c- X) V9 Z% | Q2 c( O# |, y
( J% z# ?. O/ S5 U- y 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 {4 S- B7 U4 [- { };
4 P/ x7 D1 M' W: Z0 |& |
5 V e9 N( X# P; ^' e8 a% P /* F, G, H and I are basic MD5 functions.
*/
( n2 n& ^& t5 ?3 A; P% Q# x1 y #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
' F$ n3 \' z4 n: O#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
: C6 A( z: r2 {9 ^4 ]#define H(x, y, z) ((x) ^ (y) ^ (z))
" t+ P9 M1 ]8 F' b x#define I(x, y, z) ((y) ^ ((x) | (~z)))
* j. A9 ]! K- G0 ?1 h
; ]+ k7 i3 a* w+ C% Q /* ROTATE_LEFT rotates x left n bits.
*/
6 D* V& ]: `- X% z. e% Q% N, ~ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
6 p2 V; P3 ?3 Z2 G8 h* R
- s8 J {( [& H; Y2 T /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
7 i& W$ m k* z# R9 t6 u Rotation is separate from addition to prevent recomputation.
*/
, E0 [, q* {7 ~0 p$ N( g#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
: j% A: u: s- }' 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); \
}
; q: o) X6 d+ j! J5 C' v#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 J2 ^0 g, B1 ]$ Q+ ?' v" X #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 J% H, D- z) _# t7 @: |1 v% x
; o) D) r) k3 @2 s; A6 v: e) U /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
' ~% n) _: f B void MD5Init (context)
7 K( c6 v3 ~0 z MD5_CTX *context; /* context */
4 r4 A% Z: n8 y) c" t{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
: }! p4 f" S% {" G+ B5 ?4 e' A*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
! L& u7 ?- } O" Z9 R3 H}
3 U' @6 x6 Y; p& R! v7 C/ T* W/ X
^2 m9 D, ` }/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
" U! @& V2 q. Q3 \ void MD5Update (context, input, inputLen)
6 C% B: F v4 q% ?) JMD5_CTX *context; /* context */
9 ~& l9 `- p* q unsigned char *input; /* input block */
9 _( z" e3 b/ F6 ~5 nunsigned int inputLen; /* length of input block */
0 f% j3 ^, O" b! U3 ^& B, [{
unsigned int i, index, partLen;
/ d$ {' j) m' S# N
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
1 h8 V: [& s/ d3 A% W0 l0 _
/* 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++程序


4 b, N. k: i' g$ g
) @! S; J z: C& v$ z# s b
guoyun786@sohu.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 05:34 , Processed in 0.058883 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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