win10应用 UWP 使用MD5算法
2016-06-02 00:00
316 查看
windows有自带的算法来计算MD5
原本在WPF是
进行MD5,可是UWP没有System.Security.Cryptography.MD5CryptoServiceProvider
在msdn是
来作为MD5的类
这个类只有两个方法:Append,GetValueAndReset
不知道微软是怎么要这样做
弄了好久才知道要怎么去md5
下面代码演示了三段不同字符串,经过MD5之后得到值
不过之前需要
代码
这个MD5结果有英文字符和数字特殊字符
上面代码其实也可以改为
参考文献:https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/windows.security.cryptography.core.cryptographichash.aspx
判断ctrl按下
原本在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; } }
相关文章推荐
- WPF C# 字符串读写文件
- win10 uwp App-to-app communication 应用通信
- 用git上传代码到新浪云
- 七大顶级Linux桌面比较
- Windows 10新功能
- 我喜欢网站
- C# 格式化字符串
- C#将dll打包到程序中
- 离线下载Express 2015 for Windows 10
- win10 UWP RSS阅读器
- Developing Universal Windows Apps 开发UWA应用 问答
- LabWindows/CVI 下载
- UWP xaml 圆形头像
- C# wpf 列出文件夹所有文件
- win10 uwp 屏幕常亮
- vs2013 找不到帮助 help查看器
- 粘贴板增加文字
- windows server git
- 三种方式设置特定设备UWP XAML view
- visual studio 2013 生成依赖项关系图出错