请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
MD5算法简单的来说就是把任意长度的字串变换成固定长度(通常是128位)的16进制串。RFC 1321定义了MD5算法。
5 I* }2 W6 G* `- d+ [: ~4 i6 PMD5的用途主要有:. @/ \7 Q/ {) X
一致性验证。比如我们从网上下载了某个文件,网站上一般会给出该文件的MD5值,我们下载下来后,可以利用工具计算出" n6 i# u7 T% O. M; l& }" n& M8 `
新的MD5值,与正确的MD5值进行对照,如果不一样,则可以断定该文件下载出错或被篡改了。
p8 W7 w7 C {4 ? j( E数字签名。可以用MD5算法对发布的程序或发布的消息生成MD5值作为签名等。$ m# C1 B! ]1 x* P9 u) u
密码存储。在传输过程中或存储过程中,直接用明文的密码都是很危险的。可以在传输之前先用MD5加密,存储也不用存储
1 e5 n0 G: M( y; P! c明文,可以直接存储MD5值。在验证时,先把输入的密码转换成MD5值再与存储值进行对比。 对于密码存储,Asp.net的实现方法为: # j' c/ T* D+ n- ^( ~
[mw_shl_code=csharp,true]protected void Page_Load(object sender, EventArgs e)
$ n7 b5 d/ s Q# ^; h {3 ?$ W; {$ i# a
string plainPassword = "innovator";3 h1 j/ Y& F: D& B8 B0 b& x8 s. l
; k( E% L' ?' K* {4 D, h
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();: K1 Y# y4 [; L) d' B
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();+ g, B: W6 Q' _" u
byte[] data = ascii.GetBytes(plainPassword);
7 E; }) g3 M2 C* N byte[] result = md5.ComputeHash(data);
! ]* ?3 g$ J6 y: Q6 D // Convert the MD5 result to Hexadecimal string% `0 e7 @4 ~: L( z
; J7 ^0 D0 Z9 m! O3 Q
string MD5Password = BinaryToHex(result);, `6 W* T2 a' S& x: o
string aa = "";! Q4 y2 W4 B6 b2 M7 @, x8 M
}[/mw_shl_code] ) b9 m* ~! [. e! N9 ^$ T' k J
[mw_shl_code=csharp,true]private String BinaryToHex(byte[] BinaryArray)6 _1 b& }" s% b% e! W
{8 ]% e6 k1 @. {5 W7 d# z6 c
string result = "";0 M& E7 l% [+ b/ F, Y% c
long lowerByte;
& x4 W$ Y+ ^) v! Y( F long upperByte;
% E6 ^! }; B4 Z& w' _! j+ f8 N" x
foreach (Byte singleByte in BinaryArray)7 d( \2 C& N0 f O+ [
{
% J/ a% }5 S& K3 I2 z lowerByte = singleByte & 15;+ O& I2 E+ C0 c. l/ y
upperByte = singleByte >> 4;& T( u$ U9 r$ I# y& W8 u
6 f \: R" Y4 Y' k/ G; o+ R
result += NumberToHex(upperByte); |5 L4 n. c8 M2 t* B! b3 @# U
result += NumberToHex(lowerByte);
: f4 p+ I, q9 C1 O( ]! X0 \$ s }
+ P+ K" M: q( T: K" ] return result;
, U! H8 N) M$ o0 j% t }[/mw_shl_code] 8 Y* w/ J% B( |/ v! M
[mw_shl_code=csharp,true]private static char NumberToHex(long Number)
/ l* p7 C8 a1 g5 R {8 }! k4 f7 y3 b% N% o9 `( W
if (Number > 9)
; G" d$ [/ y r, U return Convert.ToChar(65 + (Number - 10));
& d6 h2 H: b+ N else
5 S, R5 S" C/ O* v) q3 I# Y return Convert.ToChar(48 + Number);
% }' O- s* ]- A ?! ` }[/mw_shl_code]
% ~+ n. w% g. q4 a |