我正在创建一个 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
的异常,并显示以下错误消息:
目标多字节代码页中不存在 Unicode 字符的映射。 (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;
}
关于c# - 如何使用 C# 为我的 WinRT 应用程序生成 MD5 哈希码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299142/