如何生成MD5哈希值$ C $下使用C#我的WinRT的应用程序吗?我的、应用程序、哈希值、WinRT

2023-09-03 00:21:09 作者:浅陌

我创建一个MetroStyle应用程序,我想生成一个MD5 $ C $下我的字符串。到目前为止,我已经用这样的:

I'm creating a MetroStyle app and I want to generate a MD5 code for my string. So far I've used this:

    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 ,出现以下错误消息:

but it throws an exception of type System.ArgumentOutOfRangeException with the following error message:

没有映射为统一code字符存在于多字节的目标code页面。 (从HRESULT异常:0x80070459)

我在做什么错在这里?

推荐答案

确定。我发现如何做到这一点。这里是最后的code:

OK. I've found how to do this. Here's the final code:

    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;
    }