[英]How to generate MD5 hash code for my WinRT app using C#?
我正在創建一個MetroStyle應用程序,我想為我的字符串生成一個MD5代碼。 到目前為止我用過這個:
public static string ComputeMD5(string str)
{
try
{
var alg = HashAlgorithmProvider.OpenAlgorithm("MD5");
IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
var hashed = alg.HashData(buff);
var res = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, hashed);
return res;
}
catch (Exception ex)
{
return null;
}
}
但它拋出System.ArgumentOutOfRangeException
類型的異常,並顯示以下錯誤消息:
No mapping for the Unicode character exists in the target multi-byte code page. (Exception from HRESULT: 0x80070459)
我在這做錯了什么?
好。 我發現了如何做到這一點。 這是最終的代碼:
public static string ComputeMD5(string str)
{
var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
var hashed = alg.HashData(buff);
var res = CryptographicBuffer.EncodeToHexString(hashed);
return res;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.