`
lindexi-gd
  • 浏览: 130851 次
社区版块
存档分类
最新评论

win10应用 UWP 使用MD5算法

    博客分类:
  • UWP
UWP 
阅读更多

windows有自带的算法来计算MD5
原本在WPF是

        private string get_MD5(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();            
        }

进行MD5,可是UWP没有System.Security.Cryptography.MD5CryptoServiceProvider

在msdn是

Windows.Security.Cryptography.Core.CryptographicHash

来作为MD5的类
这个类只有两个方法:Append,GetValueAndReset
不知道微软是怎么要这样做

弄了好久才知道要怎么去md5

下面代码演示了三段不同字符串,经过MD5之后得到值
不过之前需要

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

代码

        public void ce()
        {
            //可以选择MD5 Sha1 Sha256 Sha384 Sha512
            string strAlgName = HashAlgorithmNames.Md5;

            // 创建一个 HashAlgorithmProvider 对象
            HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(strAlgName);

            // 创建一个可重用的CryptographicHash对象           
            CryptographicHash objHash = objAlgProv.CreateHash();


            string strMsg1 = "这是一段待加密的字符串";
            IBuffer buffMsg1 = CryptographicBuffer.ConvertStringToBinary(strMsg1 , BinaryStringEncoding.Utf16BE);
            objHash.Append(buffMsg1);
            IBuffer buffHash1 = objHash.GetValueAndReset();
            string strHash1 = CryptographicBuffer.EncodeToBase64String(buffHash1);


            string strMsg2 = "和前面一串不相同的字符串";
            IBuffer buffMsg2 = CryptographicBuffer.ConvertStringToBinary(strMsg2 , BinaryStringEncoding.Utf16BE);
            objHash.Append(buffMsg2);
            IBuffer buffHash2 = objHash.GetValueAndReset();
            string strHash2 = CryptographicBuffer.EncodeToBase64String(buffHash2);


            string strMsg3 = "每个都不相同";
            IBuffer buffMsg3 = CryptographicBuffer.ConvertStringToBinary(strMsg3 , BinaryStringEncoding.Utf16BE);
            objHash.Append(buffMsg3);
            IBuffer buffHash3 = objHash.GetValueAndReset();
            string strHash3 = CryptographicBuffer.EncodeToBase64String(buffHash3);


            _reminder.Append(strMsg1 + "\r\n");
            _reminder.Append(strHash1 + "\r\n");
            _reminder.Append(strMsg2 + "\r\n");
            _reminder.Append(strHash2 + "\r\n");
            _reminder.Append(strMsg3 + "\r\n");
            _reminder.Append(strHash3 + "\r\n");
        }

_reminder是一个StringBuilder用于展示
这个MD5结果有英文字符和数字特殊字符

上面代码其实也可以改为Sha1 Sha256 Sha384 Sha512只要在第一句的MD5改为你要的

参考文献:https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/windows.security.cryptography.core.cryptographichash.aspx

判断ctrl按下

        private void reminderkeydown(object sender , KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Control)
            {
                _ctrl = true;
            }
            if (_ctrl)
            {
                xreminder.Text = "ctrl+" + e.Key.ToString();
            }
            else
            {
                xreminder.Text =  e.Key.ToString();
            }
        }
        private bool _ctrl;

        private void reminderkeyup(object sender , KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Control)
            {
                _ctrl = false;
            }
        }

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics