会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
阿宽
Nothing is more powerful than habit!
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
c# 中對輸入的值用md5或Hash加密
Posted on
2008-01-07 22:05
宽田
阅读(
957
) 评论(
0
)
编辑
收藏
举报
c# 中對輸入的值用md5或Hash加密........
using
System.Security.Cryptography;
/**/
///
<summary>
///
MD5加密方法
///
</summary>
///
<param name="asSource">
源Md5值
</param>
///
<param name="asDestination">
需對比的字符
</param>
///
<returns>
是否正確
</returns>
private
bool
Md5EncryptJudge(
string
asSource,
string
asDestination)
{
bool
bResult
=
false
;
string
sDestination
=
""
;
//
定義Md5密碼服務類
MD5CryptoServiceProvider mdcpValu
=
new
MD5CryptoServiceProvider();
//
將傳入的值轉換成UTF8格式。便於加密時的格式統一
byte
[] bDestination
=
System.Text.Encoding.UTF8.GetBytes(asDestination);
//
加密
byte
[] bDestinationMd5
=
mdcpValu.ComputeHash(bDestination);
//
將加密后的值賦給字符串
foreach
(
byte
bVal
in
bDestinationMd5)
{
sDestination
+=
bVal.ToString();
}
//
判斷需對比的值加密成md5后與傳入的MD5值是否與傳入的相等
if
(asSource
==
sDestination)
{
bResult
=
true
;
}
else
{
bResult
=
false
;
}
return
bResult;
}
/**/
///
<summary>
///
Hash加密
///
</summary>
///
<param name="asSource">
源加密后的值
</param>
///
<param name="asDestination">
目標字符串
</param>
private
void
HashEncrypt(
string
sScouce,
string
asDestination)
{
byte
[] bDestinationValue
=
System.Text.Encoding.UTF8.GetBytes(asDestination);
HMACSHA1 hsVal
=
new
HMACSHA1();
//
加密
byte
[] bHmacshaValue
=
hsVal.ComputeHash(bDestinationValue);
//
將加密后的值轉換為字符
string
sDesHmaVal
=
Convert.ToBase64String(bHmacshaValue);
if
(sScouce
==
sDesHmaVal)
{
MessageBox.Show(
"
Ok
"
);
}
else
{
MessageBox.Show(
"
False
"
);
}
}
——宽田 著——
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 宽田
Powered by .NET 8.0 on Kubernetes