zoukankan      html  css  js  c++  java
  • 2019-7-29-win10-UWP-使用-MD5算法

    title author date CreateTime categories
    win10 UWP 使用 MD5算法
    lindexi
    2019-7-29 12:2:42 +0800
    2018-2-13 17:23:3 +0800
    Win10 UWP

    在我们的应用需求很常见的,我们需要使用md5算法。 uwp的 md5 和 WPF 的使用差不多。

    在 WPF ,我们使用

            private string GetMD5(string str)
            {
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] temp;
                StringBuilder strb = new StringBuilder();
                temp = md5.ComputeHash(Encoding.Unicode.GetBytes(str));
                md5.Clear();
                for (int i = 0; i < temp.Length; i++)
                { 
                    strb.Append(temp[i].ToString("X").PadLeft(2 , '0'));
                }
                return strb.ToString().ToLower();            
            }

    然而在 UWP ,没有System.Security.Cryptography.MD5CryptoServiceProvider,新的加密类放在Windows.Security.Cryptography.Core.CryptographicHash

    UWP 的 md5使用很简单

    首先添加在类的最前,让我们打的时候减少一些。

    using Windows.Security.Cryptography;
    using Windows.Security.Cryptography.Core;
    using Windows.Storage.Streams;
    

    然后把输入的字符串转为 md5 需要的二进制,注意编码。

                Windows.Security.Cryptography.Core.HashAlgorithmProvider objAlgProv = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5);
                Windows.Security.Cryptography.Core.CryptographicHash md5 = objAlgProv.CreateHash();
                Windows.Storage.Streams.IBuffer buffMsg1 = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(str , Windows.Security.Cryptography.BinaryStringEncoding.Utf16BE);
                md5.Append(buffMsg1);
                Windows.Storage.Streams.IBuffer buffHash1 = md5.GetValueAndReset();
    

    buffHash1就是转换后的二进制,我们可以把它转为 base64 或 Hex

    网上很多都是 Hex ,基本看到 md5 就是二进制转 Hex, Hex 就是16进制。

    我们先说下如何转为 Base64

    Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffHash1);
    

    那么如何转为 Hex ?

    CryptographicBuffer.EncodeToHexString(buffHash1);
    

    下面写出代码,测试通过,在站长工具转换结果一样

            public static string Md5(string str)
            {
                HashAlgorithmProvider algorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
                CryptographicHash md5 = algorithm.CreateHash();
                Windows.Storage.Streams.IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf16BE);
                md5.Append(buffer);
                return CryptographicBuffer.EncodeToHexString(md5.GetValueAndReset());
            }
    
    <script src="https://gist.github.com/lindexi/0ecf1d8de7a222cda5f058e74de335c1.js"></script>
  • 相关阅读:
    UVa 12174 (滑动窗口) Shuffle
    UVa 1607 (二分) Gates
    CodeForces ZeptoLab Code Rush 2015
    HDU 1525 (博弈) Euclid's Game
    HDU 2147 (博弈) kiki's game
    UVa 11093 Just Finish it up
    UVa 10954 (Huffman 优先队列) Add All
    CodeForces Round #298 Div.2
    UVa 12627 (递归 计数 找规律) Erratic Expansion
    UVa 714 (二分) Copying Books
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085467.html
Copyright © 2011-2022 走看看