21

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)

私はここで何が間違っているのですか?

4

1 に答える 1

37

わかった。私はこれを行う方法を見つけました。最終的なコードは次のとおりです。

    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;
    }
于 2011-11-28T17:35:53.477 に答える