请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
MD5算法简单的来说就是把任意长度的字串变换成固定长度(通常是128位)的16进制串。RFC 1321定义了MD5算法。
/ h( R9 b! O; B! {$ l) Z- S! s; NMD5的用途主要有:. x: g4 ^7 @/ ]& f0 Q% F
一致性验证。比如我们从网上下载了某个文件,网站上一般会给出该文件的MD5值,我们下载下来后,可以利用工具计算出
R0 p/ b# \# R* @" E# i: } ^/ x2 T新的MD5值,与正确的MD5值进行对照,如果不一样,则可以断定该文件下载出错或被篡改了。
# i5 X) C5 e$ y3 e数字签名。可以用MD5算法对发布的程序或发布的消息生成MD5值作为签名等。% y: v7 G( P+ [3 M0 z
密码存储。在传输过程中或存储过程中,直接用明文的密码都是很危险的。可以在传输之前先用MD5加密,存储也不用存储* D$ m# Z$ h' Z9 y1 k
明文,可以直接存储MD5值。在验证时,先把输入的密码转换成MD5值再与存储值进行对比。 对于密码存储,Asp.net的实现方法为:
! M; m9 D6 ?# I[mw_shl_code=csharp,true]protected void Page_Load(object sender, EventArgs e); }- H d5 M3 j% p9 [5 t
{
/ \! P( O4 x" }0 m6 C8 a) U- z string plainPassword = "innovator";7 Z9 i! ?) y5 f- m% U
% t* V# Z( Y8 M7 r
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();+ Q `5 d* I, K
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
4 q" A( s2 O0 s6 ^ byte[] data = ascii.GetBytes(plainPassword);& s% o$ F/ ]& { ?1 U$ x: P t
byte[] result = md5.ComputeHash(data);
. t( N# Q& Y5 `- ` // Convert the MD5 result to Hexadecimal string
3 B8 |: V V5 j$ M6 b
; L7 Q( @: k6 ]' a string MD5Password = BinaryToHex(result);
; @: L j) R- ^# ]7 C4 S# T0 U1 B string aa = "";
1 I) }+ I" T, A0 m }[/mw_shl_code] ) q4 d* v( U6 R( @
[mw_shl_code=csharp,true]private String BinaryToHex(byte[] BinaryArray)
, [1 M. c' D* I, o0 L1 o5 X( [6 E {/ L6 E) N. Q$ U/ W3 f2 l! q$ o
string result = "";+ t8 A- @' L2 N3 w9 t- I2 y0 r
long lowerByte;
" E7 w# [# v5 c. v; v. e* }; T long upperByte;9 T4 D5 Q% a1 H
2 M' J: D( q/ A8 _7 f0 h0 A
foreach (Byte singleByte in BinaryArray)# x* G" L: P% j
{# x7 [! D. ^% `6 e4 E; Q4 v
lowerByte = singleByte & 15;
0 e H& ?: {$ Q# e; w' k4 j( }$ H* D/ ` upperByte = singleByte >> 4;# V1 W) U* ?1 H
+ N, i p! D8 T& q- k! r8 @1 g% |
result += NumberToHex(upperByte);
) Z/ q& y( g( w! ?9 | result += NumberToHex(lowerByte);
2 ^; M: {; h& A4 Z }9 V- T! x- e0 N; I6 ^
return result;
: W7 [1 g3 ]3 ^, ^" Q% L+ ^ }[/mw_shl_code]
6 r; J" A8 V# o[mw_shl_code=csharp,true]private static char NumberToHex(long Number)
0 A5 {/ F: d7 d: s/ ?* y! V { X8 Y+ {, t% Y1 w
if (Number > 9)2 u% V! s/ t1 Z6 o$ k9 S8 X8 w3 n
return Convert.ToChar(65 + (Number - 10)); H1 H! \: w8 j: G
else3 h, Z3 t: Z; W6 p% S# W/ j' N" M
return Convert.ToChar(48 + Number);/ X, e: z) f5 p6 Z" I9 X* x
}[/mw_shl_code] ( e9 [$ g* h4 |/ J
|