当前位置:首页 » 密码管理 » vcmd5加密类

vcmd5加密类

发布时间: 2022-10-02 10:58:15

Ⅰ 这段MD5加密算法是什么意思,谁能帮我解释下不会java看不懂。。。求逐行注解下。越通俗越好。谢谢

package com.util; // 包名
/**
*MD5密码加密类
*
*/

import java.security.*; // 引入java.security包下的所有类
import java.security.spec.*;
public final class MD5 { // 命名类

public final static String MD5(String s){ // 静态 final 方法
char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f'}; // char类型数组,变量名为hexDigits
try {
byte[] strTemp = s.getBytes(); // 把参数 s 转换成byte类型数组
MessageDigest mdTemp = MessageDigest.getInstance("MD5"); // 得到一个MessageDigest 类型的变量mdTemp,提供

信息摘要算法的功能
mdTemp.update(strTemp); // 更新摘要
byte[] md = mdTemp.digest(); // 这个方法应该是加密后返回的byte数组
int j = md.length; // 记录md的长度
char str[] = new char[j * 2]; // 存储结果用
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // hexDigits 数组中对应的十六进制数放入str中
str[k++] = hexDigits[byte0 & 0xf]; // 有高人吗?再解释一下?
}
return new String(str); // 返回加密后的字符串
}
catch (Exception e){
return null; // 如果有异常返回null
}
}
}

Ⅱ 求一个VC下实现MD5加密和解密算法。。最好是类,恳请各位指点

md5只有加密一说,没有解密。

虽然可以实现碰撞,但这个技术非常保密,是不可能被普通人知道的。使用md5加密,你google一下呗,在网络里能问出个什么来?

Ⅲ 用VB实现MD5加密

md5加密运算是不可逆的,就是说不能通过那一串古古怪怪的东西算出它原始的样子。

以下提供VB可用的16位和32位MD5加密函数代码:

PrivateConstBITS_TO_A_BYTE=8
PrivateConstBYTES_TO_A_WORD=4
PrivateConstBITS_TO_A_WORD=32

Privatem_lOnBits(30)
Privatem_l2Power(30)

PrivateFunctionLShift(lValue,iShiftBits)
IfiShiftBits=0Then
LShift=lValue
ExitFunction
ElseIfiShiftBits=31Then
IflValueAnd1Then
LShift=&H80000000
Else
LShift=0
EndIf
ExitFunction
ElseIfiShiftBits<0OriShiftBits>31Then
Err.Raise6
EndIf

If(lValueAndm_l2Power(31-iShiftBits))Then
LShift=((lValueAndm_lOnBits(31-(iShiftBits+1)))*m_l2Power(iShiftBits))Or&H80000000
Else
LShift=((lValueAndm_lOnBits(31-iShiftBits))*m_l2Power(iShiftBits))
EndIf
EndFunction

PrivateFunctionRShift(lValue,iShiftBits)
IfiShiftBits=0Then
RShift=lValue
ExitFunction
ElseIfiShiftBits=31Then
IflValueAnd&H80000000Then
RShift=1
Else
RShift=0
EndIf
ExitFunction
ElseIfiShiftBits<0OriShiftBits>31Then
Err.Raise6
EndIf

RShift=(lValueAnd&H7FFFFFFE)m_l2Power(iShiftBits)

If(lValueAnd&H80000000)Then
RShift=(RShiftOr(&H40000000m_l2Power(iShiftBits-1)))
EndIf
EndFunction

PrivateFunctionRotateLeft(lValue,iShiftBits)
RotateLeft=LShift(lValue,iShiftBits)OrRShift(lValue,(32-iShiftBits))
EndFunction

PrivateFunctionAddUnsigned(lX,lY)
DimlX4
DimlY4
DimlX8
DimlY8
DimlResult

lX8=lXAnd&H80000000
lY8=lYAnd&H80000000
lX4=lXAnd&H40000000
lY4=lYAnd&H40000000

lResult=(lXAnd&H3FFFFFFF)+(lYAnd&H3FFFFFFF)

IflX4AndlY4Then
lResult=lResultXor&H80000000XorlX8XorlY8
ElseIflX4OrlY4Then
IflResultAnd&H40000000Then
lResult=lResultXor&HC0000000XorlX8XorlY8
Else
lResult=lResultXor&H40000000XorlX8XorlY8
EndIf
Else
lResult=lResultXorlX8XorlY8
EndIf

AddUnsigned=lResult
EndFunction

PrivateFunctionmd5_F(x,y,z)
md5_F=(xAndy)Or((Notx)Andz)
EndFunction

PrivateFunctionmd5_G(x,y,z)
md5_G=(xAndz)Or(yAnd(Notz))
EndFunction

PrivateFunctionmd5_H(x,y,z)
md5_H=(xXoryXorz)
EndFunction

PrivateFunctionmd5_I(x,y,z)
md5_I=(yXor(xOr(Notz)))
EndFunction

PrivateSubmd5_FF(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_F(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

PrivateSubmd5_GG(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_G(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

PrivateSubmd5_HH(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_H(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

PrivateSubmd5_II(a,b,c,d,x,s,ac)
a=AddUnsigned(a,AddUnsigned(AddUnsigned(md5_I(b,c,d),x),ac))
a=RotateLeft(a,s)
a=AddUnsigned(a,b)
EndSub

(sMessage)
DimlMessageLength
DimlNumberOfWords
DimlWordArray()
DimlBytePosition
DimlByteCount
DimlWordCount

ConstMODULUS_BITS=512
ConstCONGRUENT_BITS=448

lMessageLength=Len(sMessage)

lNumberOfWords=(((lMessageLength+((MODULUS_BITS-CONGRUENT_BITS)BITS_TO_A_BYTE))(MODULUS_BITSBITS_TO_A_BYTE))+1)*(MODULUS_BITSBITS_TO_A_WORD)
ReDimlWordArray(lNumberOfWords-1)

lBytePosition=0
lByteCount=0
DoUntillByteCount>=lMessageLength
lWordCount=lByteCountBYTES_TO_A_WORD
lBytePosition=(lByteCountModBYTES_TO_A_WORD)*BITS_TO_A_BYTE
lWordArray(lWordCount)=lWordArray(lWordCount)OrLShift(Asc(Mid(sMessage,lByteCount+1,1)),lBytePosition)
lByteCount=lByteCount+1
Loop

lWordCount=lByteCountBYTES_TO_A_WORD
lBytePosition=(lByteCountModBYTES_TO_A_WORD)*BITS_TO_A_BYTE

lWordArray(lWordCount)=lWordArray(lWordCount)OrLShift(&H80,lBytePosition)

lWordArray(lNumberOfWords-2)=LShift(lMessageLength,3)
lWordArray(lNumberOfWords-1)=RShift(lMessageLength,29)

ConvertToWordArray=lWordArray
EndFunction

PrivateFunctionWordToHex(lValue)
DimlByte
DimlCount

ForlCount=0To3
lByte=RShift(lValue,lCount*BITS_TO_A_BYTE)Andm_lOnBits(BITS_TO_A_BYTE-1)
WordToHex=WordToHex&Right("0"&Hex(lByte),2)
Next
EndFunction

PublicFunctionMD5(sMessage,stype)
m_lOnBits(0)=CLng(1)
m_lOnBits(1)=CLng(3)
m_lOnBits(2)=CLng(7)
m_lOnBits(3)=CLng(15)
m_lOnBits(4)=CLng(31)
m_lOnBits(5)=CLng(63)
m_lOnBits(6)=CLng(127)
m_lOnBits(7)=CLng(255)
m_lOnBits(8)=CLng(511)
m_lOnBits(9)=CLng(1023)
m_lOnBits(10)=CLng(2047)
m_lOnBits(11)=CLng(4095)
m_lOnBits(12)=CLng(8191)
m_lOnBits(13)=CLng(16383)
m_lOnBits(14)=CLng(32767)
m_lOnBits(15)=CLng(65535)
m_lOnBits(16)=CLng(131071)
m_lOnBits(17)=CLng(262143)
m_lOnBits(18)=CLng(524287)
m_lOnBits(19)=CLng(1048575)
m_lOnBits(20)=CLng(2097151)
m_lOnBits(21)=CLng(4194303)
m_lOnBits(22)=CLng(8388607)
m_lOnBits(23)=CLng(16777215)
m_lOnBits(24)=CLng(33554431)
m_lOnBits(25)=CLng(67108863)
m_lOnBits(26)=CLng(134217727)
m_lOnBits(27)=CLng(268435455)
m_lOnBits(28)=CLng(536870911)
m_lOnBits(29)=CLng(1073741823)
m_lOnBits(30)=CLng(2147483647)

m_l2Power(0)=CLng(1)
m_l2Power(1)=CLng(2)
m_l2Power(2)=CLng(4)
m_l2Power(3)=CLng(8)
m_l2Power(4)=CLng(16)
m_l2Power(5)=CLng(32)
m_l2Power(6)=CLng(64)
m_l2Power(7)=CLng(128)
m_l2Power(8)=CLng(256)
m_l2Power(9)=CLng(512)
m_l2Power(10)=CLng(1024)
m_l2Power(11)=CLng(2048)
m_l2Power(12)=CLng(4096)
m_l2Power(13)=CLng(8192)
m_l2Power(14)=CLng(16384)
m_l2Power(15)=CLng(32768)
m_l2Power(16)=CLng(65536)
m_l2Power(17)=CLng(131072)
m_l2Power(18)=CLng(262144)
m_l2Power(19)=CLng(524288)
m_l2Power(20)=CLng(1048576)
m_l2Power(21)=CLng(2097152)
m_l2Power(22)=CLng(4194304)
m_l2Power(23)=CLng(8388608)
m_l2Power(24)=CLng(16777216)
m_l2Power(25)=CLng(33554432)
m_l2Power(26)=CLng(67108864)
m_l2Power(27)=CLng(134217728)
m_l2Power(28)=CLng(268435456)
m_l2Power(29)=CLng(536870912)
m_l2Power(30)=CLng(1073741824)


Dimx
Dimk
DimAA
DimBB
DimCC
DimDD
Dima
Dimb
Dimc
Dimd

ConstS11=7
ConstS12=12
ConstS13=17
ConstS14=22
ConstS21=5
ConstS22=9
ConstS23=14
ConstS24=20
ConstS31=4
ConstS32=11
ConstS33=16
ConstS34=23
ConstS41=6
ConstS42=10
ConstS43=15
ConstS44=21

x=ConvertToWordArray(sMessage)

a=&H67452301
b=&HEFCDAB89
c=&H98BADCFE
d=&H10325476

Fork=0ToUBound(x)Step16
AA=a
BB=b
CC=c
DD=d

md5_FFa,b,c,d,x(k+0),S11,&HD76AA478
md5_FFd,a,b,c,x(k+1),S12,&HE8C7B756
md5_FFc,d,a,b,x(k+2),S13,&H242070DB
md5_FFb,c,d,a,x(k+3),S14,&HC1BDCEEE
md5_FFa,b,c,d,x(k+4),S11,&HF57C0FAF
md5_FFd,a,b,c,x(k+5),S12,&H4787C62A
md5_FFc,d,a,b,x(k+6),S13,&HA8304613
md5_FFb,c,d,a,x(k+7),S14,&HFD469501
md5_FFa,b,c,d,x(k+8),S11,&H698098D8
md5_FFd,a,b,c,x(k+9),S12,&H8B44F7AF
md5_FFc,d,a,b,x(k+10),S13,&HFFFF5BB1
md5_FFb,c,d,a,x(k+11),S14,&H895CD7BE
md5_FFa,b,c,d,x(k+12),S11,&H6B901122
md5_FFd,a,b,c,x(k+13),S12,&HFD987193
md5_FFc,d,a,b,x(k+14),S13,&HA679438E
md5_FFb,c,d,a,x(k+15),S14,&H49B40821

md5_GGa,b,c,d,x(k+1),S21,&HF61E2562
md5_GGd,a,b,c,x(k+6),S22,&HC040B340
md5_GGc,d,a,b,x(k+11),S23,&H265E5A51
md5_GGb,c,d,a,x(k+0),S24,&HE9B6C7AA
md5_GGa,b,c,d,x(k+5),S21,&HD62F105D
md5_GGd,a,b,c,x(k+10),S22,&H2441453
md5_GGc,d,a,b,x(k+15),S23,&HD8A1E681
md5_GGb,c,d,a,x(k+4),S24,&HE7D3FBC8
md5_GGa,b,c,d,x(k+9),S21,&H21E1CDE6
md5_GGd,a,b,c,x(k+14),S22,&HC33707D6
md5_GGc,d,a,b,x(k+3),S23,&HF4D50D87
md5_GGb,c,d,a,x(k+8),S24,&H455A14ED
md5_GGa,b,c,d,x(k+13),S21,&HA9E3E905
md5_GGd,a,b,c,x(k+2),S22,&HFCEFA3F8
md5_GGc,d,a,b,x(k+7),S23,&H676F02D9
md5_GGb,c,d,a,x(k+12),S24,&H8D2A4C8A

md5_HHa,b,c,d,x(k+5),S31,&HFFFA3942
md5_HHd,a,b,c,x(k+8),S32,&H8771F681
md5_HHc,d,a,b,x(k+11),S33,&H6D9D6122
md5_HHb,c,d,a,x(k+14),S34,&HFDE5380C
md5_HHa,b,c,d,x(k+1),S31,&HA4BEEA44
md5_HHd,a,b,c,x(k+4),S32,&H4BDECFA9
md5_HHc,d,a,b,x(k+7),S33,&HF6BB4B60
md5_HHb,c,d,a,x(k+10),S34,&HBEBFBC70
md5_HHa,b,c,d,x(k+13),S31,&H289B7EC6
md5_HHd,a,b,c,x(k+0),S32,&HEAA127FA
md5_HHc,d,a,b,x(k+3),S33,&HD4EF3085
md5_HHb,c,d,a,x(k+6),S34,&H4881D05
md5_HHa,b,c,d,x(k+9),S31,&HD9D4D039
md5_HHd,a,b,c,x(k+12),S32,&HE6DB99E5
md5_HHc,d,a,b,x(k+15),S33,&H1FA27CF8
md5_HHb,c,d,a,x(k+2),S34,&HC4AC5665

md5_IIa,b,c,d,x(k+0),S41,&HF4292244
md5_IId,a,b,c,x(k+7),S42,&H432AFF97
md5_IIc,d,a,b,x(k+14),S43,&HAB9423A7
md5_IIb,c,d,a,x(k+5),S44,&HFC93A039
md5_IIa,b,c,d,x(k+12),S41,&H655B59C3
md5_IId,a,b,c,x(k+3),S42,&H8F0CCC92
md5_IIc,d,a,b,x(k+10),S43,&HFFEFF47D
md5_IIb,c,d,a,x(k+1),S44,&H85845DD1
md5_IIa,b,c,d,x(k+8),S41,&H6FA87E4F
md5_IId,a,b,c,x(k+15),S42,&HFE2CE6E0
md5_IIc,d,a,b,x(k+6),S43,&HA3014314
md5_IIb,c,d,a,x(k+13),S44,&H4E0811A1
md5_IIa,b,c,d,x(k+4),S41,&HF7537E82
md5_IId,a,b,c,x(k+11),S42,&HBD3AF235
md5_IIc,d,a,b,x(k+2),S43,&H2AD7D2BB
md5_IIb,c,d,a,x(k+9),S44,&HEB86D391

a=AddUnsigned(a,AA)
b=AddUnsigned(b,BB)
c=AddUnsigned(c,CC)
d=AddUnsigned(d,DD)
Next

Ifstype=32Then
MD5=LCase(WordToHex(a)&WordToHex(b)&WordToHex(c)&WordToHex(d))
Else
MD5=LCase(WordToHex(b)&WordToHex(c))
EndIf
EndFunction

'下面是测试代码
Subtest()
MsgBoxMD5("a",16)'16位加密
MsgBoxMD5("a",32)'32位加密
EndSub

Ⅳ VS2013中c语言md5加密函数怎么调用

1、主要就是调用库函数,MD5加密说到底也是函数计算,没有什么思路的问题,了解md5的发明算法,本质是一个数学问题。

2、例程:

#ifndefMD5_H
#defineMD5_H

typedefstruct
{
unsignedintcount[2];
unsignedintstate[4];
unsignedcharbuffer[64];
}MD5_CTX;
#defineF(x,y,z)((x&y)|(~x&z))
#defineG(x,y,z)((x&z)|(y&~z))
#defineH(x,y,z)(x^y^z)
#defineI(x,y,z)(y^(x|~z))
#defineROTATE_LEFT(x,n)((x<<n)|(x>>(32-n)))
#defineFF(a,b,c,d,x,s,ac)
{
a+=F(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineGG(a,b,c,d,x,s,ac)
{
a+=G(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineHH(a,b,c,d,x,s,ac)
{
a+=H(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineII(a,b,c,d,x,s,ac)
{
a+=I(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
voidMD5Init(MD5_CTX*context);
voidMD5Update(MD5_CTX*context,unsignedchar*input,unsignedintinputlen);
voidMD5Final(MD5_CTX*context,unsignedchardigest[16]);
voidMD5Transform(unsignedintstate[4],unsignedcharblock[64]);
voidMD5Encode(unsignedchar*output,unsignedint*input,unsignedintlen);
voidMD5Decode(unsignedint*output,unsignedchar*input,unsignedintlen);

#endif
源文件md5.c
#include<memory.h>
#include"md5.h"

unsignedcharPADDING[]={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};

voidMD5Init(MD5_CTX*context)
{
context->count[0]=0;
context->count[1]=0;
context->state[0]=0x67452301;
context->state[1]=0xEFCDAB89;
context->state[2]=0x98BADCFE;
context->state[3]=0x10325476;
}
voidMD5Update(MD5_CTX*context,unsignedchar*input,unsignedintinputlen)
{
unsignedinti=0,index=0,partlen=0;
index=(context->count[0]>>3)&0x3F;
partlen=64-index;
context->count[0]+=inputlen<<3;
if(context->count[0]<(inputlen<<3))
context->count[1]++;
context->count[1]+=inputlen>>29;

if(inputlen>=partlen)
{
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i=partlen;i+64<=inputlen;i+=64)
MD5Transform(context->state,&input[i]);
index=0;
}
else
{
i=0;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}
voidMD5Final(MD5_CTX*context,unsignedchardigest[16])
{
unsignedintindex=0,padlen=0;
unsignedcharbits[8];
index=(context->count[0]>>3)&0x3F;
padlen=(index<56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);
}
voidMD5Encode(unsignedchar*output,unsignedint*input,unsignedintlen)
{
unsignedinti=0,j=0;
while(j<len)
{
output[j]=input[i]&0xFF;
output[j+1]=(input[i]>>8)&0xFF;
output[j+2]=(input[i]>>16)&0xFF;
output[j+3]=(input[i]>>24)&0xFF;
i++;
j+=4;
}
}
voidMD5Decode(unsignedint*output,unsignedchar*input,unsignedintlen)
{
unsignedinti=0,j=0;
while(j<len)
{
output[i]=(input[j])|
(input[j+1]<<8)|
(input[j+2]<<16)|
(input[j+3]<<24);
i++;
j+=4;
}
}
voidMD5Transform(unsignedintstate[4],unsignedcharblock[64])
{
unsignedinta=state[0];
unsignedintb=state[1];
unsignedintc=state[2];
unsignedintd=state[3];
unsignedintx[64];
MD5Decode(x,block,64);
FF(a,b,c,d,x[0],7,0xd76aa478);/*1*/
FF(d,a,b,c,x[1],12,0xe8c7b756);/*2*/
FF(c,d,a,b,x[2],17,0x242070db);/*3*/
FF(b,c,d,a,x[3],22,0xc1bdceee);/*4*/
FF(a,b,c,d,x[4],7,0xf57c0faf);/*5*/
FF(d,a,b,c,x[5],12,0x4787c62a);/*6*/
FF(c,d,a,b,x[6],17,0xa8304613);/*7*/
FF(b,c,d,a,x[7],22,0xfd469501);/*8*/
FF(a,b,c,d,x[8],7,0x698098d8);/*9*/
FF(d,a,b,c,x[9],12,0x8b44f7af);/*10*/
FF(c,d,a,b,x[10],17,0xffff5bb1);/*11*/
FF(b,c,d,a,x[11],22,0x895cd7be);/*12*/
FF(a,b,c,d,x[12],7,0x6b901122);/*13*/
FF(d,a,b,c,x[13],12,0xfd987193);/*14*/
FF(c,d,a,b,x[14],17,0xa679438e);/*15*/
FF(b,c,d,a,x[15],22,0x49b40821);/*16*/

Ⅳ MD5是怎么加密的

1、要有md5加密函数
2、使用这个函数
比如b/s程序 md5一般都是一个文件
需要用到md5的函数的时候 include这个文件
还是 md5(password,16) 就是调用这个函数 password是 变量名16 加密长度

Ⅵ C语言下对字符串进行MD5加密

怎么发给你?
md5.h
//===================================================
#ifndef MD5_H
#define MD5_H

#include <string>
#include <fstream>

/* Type define */
typedef unsigned char byte;
typedef unsigned int uint32;

using std::string;
using std::ifstream;

/* MD5 declaration. */
class MD5 {
public:
MD5();
MD5(const void* input, size_t length);
MD5(const string& str);
MD5(ifstream& in);
void update(const void* input, size_t length);
void update(const string& str);
void update(ifstream& in);
const byte* digest();
string toString();
void reset();

private:
void update(const byte* input, size_t length);
void final();
void transform(const byte block[64]);
void encode(const uint32* input, byte* output, size_t length);
void decode(const byte* input, uint32* output, size_t length);
string bytesToHexString(const byte* input, size_t length);

/* class unable */
MD5(const MD5&);
MD5& operator=(const MD5&);

private:
uint32 _state[4]; /* state (ABCD) */
uint32 _count[2]; /* number of bits, molo 2^64 (low-order word first) */
byte _buffer[64]; /* input buffer */
byte _digest[16]; /* message digest */
bool _finished; /* calculate finished ? */

static const byte PADDING[64]; /* padding for calculate */
static const char HEX[16];
enum { BUFFER_SIZE = 1024 };
};

#endif /*MD5_H*/

md5.cpp
//==============================================
#include "md5.h"

using namespace std;

/* Constants for MD5Transform routine. */
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21

/* F, G, H and I are basic MD5 functions.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))

/* ROTATE_LEFT rotates x left n bits.
*/
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))

/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}

const byte MD5::PADDING[64] = { 0x80 };
const char MD5::HEX[16] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};

/* Default construct. */
MD5::MD5() {
reset();
}

/* Construct a MD5 object with a input buffer. */
MD5::MD5(const void* input, size_t length) {
reset();
update(input, length);
}

/* Construct a MD5 object with a string. */
MD5::MD5(const string& str) {
reset();
update(str);
}

/* Construct a MD5 object with a file. */
MD5::MD5(ifstream& in) {
reset();
update(in);
}

/* Return the message-digest */
const byte* MD5::digest() {

if (!_finished) {
_finished = true;
final();
}
return _digest;
}

/* Reset the calculate state */
void MD5::reset() {

_finished = false;
/* reset number of bits. */
_count[0] = _count[1] = 0;
/* Load magic initialization constants. */
_state[0] = 0x67452301;
_state[1] = 0xefcdab89;
_state[2] = 0x98badcfe;
_state[3] = 0x10325476;
}

/* Updating the context with a input buffer. */
void MD5::update(const void* input, size_t length) {
update((const byte*)input, length);
}

/* Updating the context with a string. */
void MD5::update(const string& str) {
update((const byte*)str.c_str(), str.length());
}

/* Updating the context with a file. */
void MD5::update(ifstream& in) {

if (!in) {
return;
}

std::streamsize length;
char buffer[BUFFER_SIZE];
while (!in.eof()) {
in.read(buffer, BUFFER_SIZE);
length = in.gcount();
if (length > 0) {
update(buffer, length);
}
}
in.close();
}

/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
void MD5::update(const byte* input, size_t length) {

uint32 i, index, partLen;

_finished = false;

/* Compute number of bytes mod 64 */
index = (uint32)((_count[0] >> 3) & 0x3f);

/* update number of bits */
if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) {
++_count[1];
}
_count[1] += ((uint32)length >> 29);

partLen = 64 - index;

/* transform as many times as possible. */
if (length >= partLen) {

memcpy(&_buffer[index], input, partLen);
transform(_buffer);

for (i = partLen; i + 63 < length; i += 64) {
transform(&input[i]);
}
index = 0;

} else {
i = 0;
}

/* Buffer remaining input */
memcpy(&_buffer[index], &input[i], length - i);
}

/* MD5 finalization. Ends an MD5 message-_digest operation, writing the
the message _digest and zeroizing the context.
*/
void MD5::final() {

byte bits[8];
uint32 oldState[4];
uint32 oldCount[2];
uint32 index, padLen;

/* Save current state and count. */
memcpy(oldState, _state, 16);
memcpy(oldCount, _count, 8);

/* Save number of bits */
encode(_count, bits, 8);

/* Pad out to 56 mod 64. */
index = (uint32)((_count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
update(PADDING, padLen);

/* Append length (before padding) */
update(bits, 8);

/* Store state in digest */
encode(_state, _digest, 16);

/* Restore current state and count. */
memcpy(_state, oldState, 16);
memcpy(_count, oldCount, 8);
}

/* MD5 basic transformation. Transforms _state based on block. */
void MD5::transform(const byte block[64]) {

uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];

decode(block, x, 64);

/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */

/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */

/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */

/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */

_state[0] += a;
_state[1] += b;
_state[2] += c;
_state[3] += d;
}

/* Encodes input (ulong) into output (byte). Assumes length is
a multiple of 4.
*/
void MD5::encode(const uint32* input, byte* output, size_t length) {

for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
output[j]= (byte)(input[i] & 0xff);
output[j + 1] = (byte)((input[i] >> 8) & 0xff);
output[j + 2] = (byte)((input[i] >> 16) & 0xff);
output[j + 3] = (byte)((input[i] >> 24) & 0xff);
}
}

/* Decodes input (byte) into output (ulong). Assumes length is
a multiple of 4.
*/
void MD5::decode(const byte* input, uint32* output, size_t length) {

for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) |
(((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24);
}
}

/* Convert byte array to hex string. */
string MD5::bytesToHexString(const byte* input, size_t length) {

string str;
str.reserve(length << 1);
for (size_t i = 0; i < length; ++i) {
int t = input[i];
int a = t / 16;
int b = t % 16;
str.append(1, HEX[a]);
str.append(1, HEX[b]);
}
return str;
}

/* Convert digest to string value */
string MD5::toString() {
return bytesToHexString(digest(), 16);
}

Ⅶ c# MD5加密

这个问题怎么答呢,MD5只是一个算法而不是一个具体的方法,也就是说你完全可以使用不同的语言去实现MD5算法.但不管你的方法如何,但算法是固定了.所以你搜到的可能是PHP,JSP,C#等不同语言下算法.

如果单说MD5在C#中的算法,可能会有说按照算法写一个出来.便你可能还会遇到一个超短的写法!那是因为在C#的库函数中已经有了这个算法的!谁在C#下写MD5的算法也只是用来练习而已!明明类库里有的东西,干嘛要再写一个?

System.Web.Security.FormsAuthentication类有一个共享方法就可以实现MD5与SHA1等加密!他有两个参数,第一个是要加密的官符,第二个是加密的类型.如:
("admin","MD5");就是将admin进行MD5加密,如果是("admin","SHA1");就是将admin字符串进行SHA1加密。这种情况下还有必须再去写MD5加密的算法吗?

Ⅷ 急求 MD5的加密解密算法,用C++实现的源代码 高分答谢

要代码,还是要相关的解释资料?

---------------------------------
要代码的话:

两个文件:
--------------------------
1. md5.h:

#pragma once

typedef unsigned long int UINT32;
typedef unsigned short int UINT16;

/* MD5 context. */
typedef struct {
UINT32 state[4]; /* state (ABCD) */
UINT32 count[2]; /* number of bits, molo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;

void MD5Init (MD5_CTX *);
void MD5Update (MD5_CTX *, unsigned char *, unsigned int);
void MD5Final (unsigned char [16], MD5_CTX *);

--------------------------
2. md5.cpp:

#include "md5.h"

#include "memory.h"

#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21

static void MD5Transform (UINT32 a[4], unsigned char b[64]);
static void Encode (unsigned char *, UINT32 *, unsigned int);
static void Decode (UINT32 *, unsigned char *, unsigned int);

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
};

#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))

#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))

#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}

void MD5Init (MD5_CTX *context)
{
context->count[0] = context->count[1] = 0;

context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}

void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen)
{
unsigned int i, index, partLen;

index = (unsigned int)((context->count[0] >> 3) & 0x3F);

if ((context->count[0] += ((UINT32)inputLen << 3))
< ((UINT32)inputLen << 3))
context->count[1]++;
context->count[1] += ((UINT32)inputLen >> 29);

partLen = 64 - index;

if (inputLen >= partLen) {
memcpy((unsigned char *)&context->buffer[index], (unsigned char *)input, partLen);
MD5Transform (context->state, context->buffer);

for (i = partLen; i + 63 < inputLen; i += 64)
MD5Transform (context->state, &input[i]);

index = 0;
}
else
i = 0;

memcpy((unsigned char *)&context->buffer[index], (unsigned char *)&input[i],
inputLen-i);
}

void MD5Final (unsigned char digest[16], MD5_CTX * context)
{
unsigned char bits[8];
unsigned int index, padLen;

Encode (bits, context->count, 8);

index = (unsigned int)((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update (context, PADDING, padLen);

MD5Update (context, bits, 8);

Encode (digest, context->state, 16);

memset ((unsigned char *)context, 0, sizeof (*context));
}

static void MD5Transform (UINT32 state[4], unsigned char block[64])
{
UINT32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];

Decode (x, block, 64);

/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */

/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */

/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */

/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */

state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;

memset ((unsigned char *)x, 0, sizeof (x));
}

static void Encode (unsigned char *output, UINT32 *input, unsigned int len)
{
unsigned int i, j;

for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}

static void Decode (UINT32 *output, unsigned char *input, unsigned int len)
{
unsigned int i, j;

for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT32)input[j]) | (((UINT32)input[j+1]) << 8) |
(((UINT32)input[j+2]) << 16) | (((UINT32)input[j+3]) << 24);
}

--------------------------
就这两个文件。使用的时候把它们加入工程或者makefile,调用时包含md5.h即可,给个简单的例子,输入一个字符串然后计算它的md5值并输出,在VC6.0和GCC4.4下测试通过:

#include <stdio.h>
#include <string.h>

#include "md5.h"

int main ()
{
char tmp[128];
unsigned char digest[16];

MD5_CTX context;

scanf("%s",tmp);

MD5Init (&context);
MD5Update (&context, (unsigned char*)tmp, strlen(tmp));
MD5Final (digest,&context);

printf("MD5Value:");
for(int i=0; i<16; ++i)
{
printf("%02X",digest[i]);
}
printf("\n");

return 0;
}

Ⅸ 谁有VC中用MD5对字符串进行加密的源码

1 public String md5(String s)
2 {
3 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
4 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
5 bytes = md5.ComputeHash(bytes);
6 md5.Clear();
7
8 string ret = "";
9 for(int i=0 ; i<bytes.Length ; i++)
10 {
11 ret += Convert.ToString(bytes[i],16).PadLeft(2,'0');
12 }
13
14 return ret.PadLeft(32,'0');
15 }

这个函数实现传如string,返回md5的string

Ⅹ md5 算法程序+详细注释,高分求教!

MD5加密算法简介

一、综述
MD5的全称是message-digest algorithm 5(信息-摘要算法),在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest开发出来,经md2、md3和md4发展而来。它的作用是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一 个任意长度的字节串变换成一定长的大整数)。不管是md2、md4还是md5,它们都需要获得一个随机长度的信息并产生一个128位的信息摘要。虽然这些 算法的结构或多或少有些相似,但md2的设计与md4和md5完全不同,那是因为md2是为8位机器做过设计优化的,而md4和md5却是面向32位的电 脑。这三个算法的描述和c语言源代码在internet rfcs 1321中有详细的描述(http://www.ietf.org/rfc/rfc1321.txt),这是一份最权威的文档,由ronald l. rivest在1992年8月向ieft提交。

rivest在1989年开发出md2算法。在这个算法中,首先对信 息进行数据补位,使信息的字节长度是16的倍数。然后,以一个16位的检验和追加到信息末尾。并且根据这个新产生的信息计算出散列值。后来,rogier 和chauvaud发现如果忽略了检验和将产生md2冲突。md2算法的加密后结果是唯一的--既没有重复。
为了加强算法的安全性, rivest在1990年又开发出md4算法。md4算法同样需要填补信息以确保信息的字节长度加上448后能被512整除(信息字节长度mod 512 = 448)。然后,一个以64位二进制表示的信息的最初长度被添加进来。信息被处理成512位damg?rd/merkle迭代结构的区块,而且每个区块要 通过三个不同步骤的处理。den boer和bosselaers以及其他人很快的发现了攻击md4版本中第一步和第三步的漏洞。dobbertin向大家演示了如何利用一部普通的个人电 脑在几分钟内找到md4完整版本中的冲突(这个冲突实际上是一种漏洞,它将导致对不同的内容进行加密却可能得到相同的加密后结果)。毫无疑问,md4就此 被淘汰掉了。
尽管md4算法在安全上有个这么大的漏洞,但它对在其后才被开发出来的好几种信息安全加密算法的出现却有着不可忽视的引导作用。除了md5以外,其中比较有名的还有sha-1、ripe-md以及haval等。
一年以后,即1991年,rivest开发出技术上更为趋近成熟的md5算法。它在md4的基础上增加了"安全-带子"(safety-belts)的 概念。虽然md5比md4稍微慢一些,但却更为安全。这个算法很明显的由四个和md4设计有少许不同的步骤组成。在md5算法中,信息-摘要的大小和填充 的必要条件与md4完全相同。den boer和bosselaers曾发现md5算法中的假冲突(pseudo-collisions),但除此之外就没有其他被发现的加密后结果了。
van oorschot和wiener曾经考虑过一个在散列中暴力搜寻冲突的函数(brute-force hash function),而且他们猜测一个被设计专门用来搜索md5冲突的机器(这台机器在1994年的制造成本大约是一百万美元)可以平均每24天就找到一 个冲突。但单从1991年到2001年这10年间,竟没有出现替代md5算法的md6或被叫做其他什么名字的新算法这一点,我们就可以看出这个瑕疵并没有 太多的影响md5的安全性。上面所有这些都不足以成为md5的在实际应用中的问题。并且,由于md5算法的使用不需要支付任何版权费用的,所以在一般的情 况下(非绝密应用领域。但即便是应用在绝密领域内,md5也不失为一种非常优秀的中间技术),md5怎么都应该算得上是非常安全的了。

二、算法的应用

md5的典型应用是对一段信息(message)产生信息摘要(message-digest),以防止被篡改。比如,在unix下有很多软件在下载的时候都有一个文件名相同,文件扩展名为.md5的文件,在这个文件中通常只有一行文本,大致结构如:
md5 (tanajiya.tar.gz) =
这就是tanajiya.tar.gz文件的数字签名。md5将整个文件当作一个大文本信息,通过其不可逆的字符串变换算法,产生了这个唯一的md5信 息摘要。如果在以后传播这个文件的过程中,无论文件的内容发生了任何形式的改变(包括人为修改或者下载过程中线路不稳定引起的传输错误等),只要你对这个 文件重新计算md5时就会发现信息摘要不相同,由此可以确定你得到的只是一个不正确的文件。如果再有一个第三方的认证机构,用md5还可以防止文件作者的 "抵赖",这就是所谓的数字签名应用。
md5还广泛用于加密和解密技术上。比如在unix系统中用户的密码就是以md5(或其它类似的算 法)经加密后存储在文件系统中。当用户登录的时候,系统把用户输入的密码计算成md5值,然后再去和保存在文件系统中的md5值进行比较,进而确定输入的 密码是否正确。通过这样的步骤,系统在并不知道用户密码的明码的情况下就可以确定用户登录系统的合法性。这不但可以避免用户的密码被具有系统管理员权限的 用户知道,而且还在一定程度上增加了密码被破解的难度。
正是因为这个原因,现在被黑客使用最多的一种破译密码的方法就是一种被称为"跑字 典"的方法。有两种方法得到字典,一种是日常搜集的用做密码的字符串表,另一种是用排列组合方法生成的,先用md5程序计算出这些字典项的md5值,然后 再用目标的md5值在这个字典中检索。我们假设密码的最大长度为8位字节(8 bytes),同时密码只能是字母和数字,共26+26+10=62个字符,排列组合出的字典的项数则是p(62,1)+p(62,2)….+p (62,8),那也已经是一个很天文的数字了,存储这个字典就需要tb级的磁盘阵列,而且这种方法还有一个前提,就是能获得目标账户的密码md5值的情况 下才可以。这种加密技术被广泛的应用于unix系统中,这也是为什么unix系统比一般操作系统更为坚固一个重要原因。

三、算法描述

对md5算法简要的叙述可以为:md5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值。
在md5算法中,首先需要对信息进行填充,使其字节长度对512求余的结果等于448。因此,信息的字节长度(bits length)将被扩展至n*512+448,即n*64+56个字节(bytes),n为一个正整数。填充的方法如下,在信息的后面填充一个1和无数个 0,直到满足上面的条件时才停止用0对信息的填充。然后,在在这个结果后面附加一个以64位二进制表示的填充前信息长度。经过这两步的处理,现在的信息字 节长度=n*512+448+64=(n+1)*512,即长度恰好是512的整数倍。这样做的原因是为满足后面处理中对信息长度的要求。
md5中有四个32位被称作链接变量(chaining variable)的整数参数,他们分别为:a=0x01234567,b=0x89abcdef,c=0xfedcba98,d=0x76543210。
当设置好这四个链接变量后,就开始进入算法的四轮循环运算。循环的次数是信息中512位信息分组的数目。
将上面四个链接变量复制到另外四个变量中:a到a,b到b,c到c,d到d。
主循环有四轮(md4只有三轮),每轮循环都很相似。第一轮进行16次操作。每次操作对a、b、c和d中的其中三个作一次非线性函数运算,然后将所得结 果加上第四个变量,文本的一个子分组和一个常数。再将所得结果向右环移一个不定的数,并加上a、b、c或d中之一。最后用该结果取代a、b、c或d中之 一。
以一下是每次操作中用到的四个非线性函数(每轮一个)。

f(x,y,z) =(x&y)|((~x)&z)
g(x,y,z) =(x&z)|(y&(~z))
h(x,y,z) =x^y^z
i(x,y,z)=y^(x|(~z))
(&是与,|是或,~是非,^是异或)

这四个函数的说明:如果x、y和z的对应位是独立和均匀的,那么结果的每一位也应是独立和均匀的。
f是一个逐位运算的函数。即,如果x,那么y,否则z。函数h是逐位奇偶操作符。

假设mj表示消息的第j个子分组(从0到15),
<< ff(a,b,c,d,mj,s,ti) 表示 a=b+((a+(f(b,c,d)+mj+ti)
<< gg(a,b,c,d,mj,s,ti) 表示 a=b+((a+(g(b,c,d)+mj+ti)
<< hh(a,b,c,d,mj,s,ti) 表示 a=b+((a+(h(b,c,d)+mj+ti)
<< ii(a,b,c,d,mj,s,ti) 表示 a=b+((a+(i(b,c,d)+mj+ti)
<< 这四轮(64步)是:

第一轮

ff(a,b,c,d,m0,7,0xd76aa478)
ff(d,a,b,c,m1,12,0xe8c7b756)
ff(c,d,a,b,m2,17,0x242070db)
ff(b,c,d,a,m3,22,0xc1bdceee)
ff(a,b,c,d,m4,7,0xf57c0faf)
ff(d,a,b,c,m5,12,0x4787c62a)
ff(c,d,a,b,m6,17,0xa8304613)
ff(b,c,d,a,m7,22,0xfd469501)
ff(a,b,c,d,m8,7,0x698098d8)
ff(d,a,b,c,m9,12,0x8b44f7af)
ff(c,d,a,b,m10,17,0xffff5bb1)
ff(b,c,d,a,m11,22,0x895cd7be)
ff(a,b,c,d,m12,7,0x6b901122)
ff(d,a,b,c,m13,12,0xfd987193)
ff(c,d,a,b,m14,17,0xa679438e)
ff(b,c,d,a,m15,22,0x49b40821)

第二轮

gg(a,b,c,d,m1,5,0xf61e2562)
gg(d,a,b,c,m6,9,0xc040b340)
gg(c,d,a,b,m11,14,0x265e5a51)
gg(b,c,d,a,m0,20,0xe9b6c7aa)
gg(a,b,c,d,m5,5,0xd62f105d)
gg(d,a,b,c,m10,9,0x02441453)
gg(c,d,a,b,m15,14,0xd8a1e681)
gg(b,c,d,a,m4,20,0xe7d3fbc8)
gg(a,b,c,d,m9,5,0x21e1cde6)
gg(d,a,b,c,m14,9,0xc33707d6)
gg(c,d,a,b,m3,14,0xf4d50d87)
gg(b,c,d,a,m8,20,0x455a14ed)
gg(a,b,c,d,m13,5,0xa9e3e905)
gg(d,a,b,c,m2,9,0xfcefa3f8)
gg(c,d,a,b,m7,14,0x676f02d9)
gg(b,c,d,a,m12,20,0x8d2a4c8a)

第三轮

hh(a,b,c,d,m5,4,0xfffa3942)
hh(d,a,b,c,m8,11,0x8771f681)
hh(c,d,a,b,m11,16,0x6d9d6122)
hh(b,c,d,a,m14,23,0xfde5380c)
hh(a,b,c,d,m1,4,0xa4beea44)
hh(d,a,b,c,m4,11,0x4bdecfa9)
hh(c,d,a,b,m7,16,0xf6bb4b60)
hh(b,c,d,a,m10,23,0xbebfbc70)
hh(a,b,c,d,m13,4,0x289b7ec6)
hh(d,a,b,c,m0,11,0xeaa127fa)
hh(c,d,a,b,m3,16,0xd4ef3085)
hh(b,c,d,a,m6,23,0x04881d05)
hh(a,b,c,d,m9,4,0xd9d4d039)
hh(d,a,b,c,m12,11,0xe6db99e5)
hh(c,d,a,b,m15,16,0x1fa27cf8)
hh(b,c,d,a,m2,23,0xc4ac5665)

第四轮

ii(a,b,c,d,m0,6,0xf4292244)
ii(d,a,b,c,m7,10,0x432aff97)
ii(c,d,a,b,m14,15,0xab9423a7)
ii(b,c,d,a,m5,21,0xfc93a039)
ii(a,b,c,d,m12,6,0x655b59c3)
ii(d,a,b,c,m3,10,0x8f0ccc92)
ii(c,d,a,b,m10,15,0xffeff47d)
ii(b,c,d,a,m1,21,0x85845dd1)
ii(a,b,c,d,m8,6,0x6fa87e4f)
ii(d,a,b,c,m15,10,0xfe2ce6e0)
ii(c,d,a,b,m6,15,0xa3014314)
ii(b,c,d,a,m13,21,0x4e0811a1)
ii(a,b,c,d,m4,6,0xf7537e82)
ii(d,a,b,c,m11,10,0xbd3af235)
ii(c,d,a,b,m2,15,0x2ad7d2bb)
ii(b,c,d,a,m9,21,0xeb86d391)

常数ti可以如下选择:
在第i步中,ti是4294967296*abs(sin(i))的整数部分,i的单位是弧度。(4294967296等于2的32次方)
所有这些完成之后,将a、b、c、d分别加上a、b、c、d。然后用下一分组数据继续运行算法,最后的输出是a、b、c和d的级联。
当你按照我上面所说的方法实现md5算法以后,你可以用以下几个信息对你做出来的程序作一个简单的测试,看看程序有没有错误。

md5 ("") =
md5 ("a") =
md5 ("abc") =
md5 ("message digest") =
md5 ("abcdefghijklmnopqrstuvwxyz") =
md5 ("") =
md5 ("1234567890") =

如果你用上面的信息分别对你做的md5算法实例做测试,最后得出的结论和标准答案完全一样,那我就要在这里象你道一声祝贺了。要知道,我的程序在第一次编译成功的时候是没有得出和上面相同的结果的。

四、MD5的安全性

md5相对md4所作的改进:

1. 增加了第四轮;

2. 每一步均有唯一的加法常数;

3. 为减弱第二轮中函数g的对称性从(x&y)|(x&z)|(y&z)变为(x&z)|(y&(~z));

4. 第一步加上了上一步的结果,这将引起更快的雪崩效应;

5. 改变了第二轮和第三轮中访问消息子分组的次序,使其更不相似;

6. 近似优化了每一轮中的循环左移位移量以实现更快的雪崩效应。各轮的位移量互不相同。

热点内容
推荐编程课 发布:2025-05-15 22:34:12 浏览:615
表拒绝访问 发布:2025-05-15 22:29:37 浏览:976
电脑怎样解压文件 发布:2025-05-15 22:25:32 浏览:438
dns服务器怎么看 发布:2025-05-15 22:17:27 浏览:150
3dm的压缩包 发布:2025-05-15 22:09:23 浏览:661
和存储字长 发布:2025-05-15 21:54:09 浏览:514
用什么写c语言 发布:2025-05-15 21:35:56 浏览:418
linux读取u盘 发布:2025-05-15 21:32:13 浏览:508
c语言dos 发布:2025-05-15 21:18:17 浏览:664
sci编译英文 发布:2025-05-15 21:16:57 浏览:383