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](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;
}
}
~~~
- 前言
- UWP win10 app 新關鍵字x:Bing
- win10應用 UWP 使用MD5算法
- win10 UWP讀寫文件
- UWP appButtonBar樣式
- C# 6.0 $&quot;Hello {csdn}&quot;
- Win10 UWP xaml 延遲加載元素
- UWP xaml 圓形頭像
- UWP 繪制圖形
- win10 uwp 通知Toast
- win10 UWP 顯示地圖
- win10 uwp 參考
- win10 uwp clone
- win10 uwp 裝機必備應用 含源代碼
- RichEditBox 使用自定義菜單
- win10 UWP FlipView
- win10 UWP 獲取系統信息
- win10 UWP 申請微軟開發者
- win10 UWP button
- win10 UWP Markdown 含源代碼
- win10 UWP 應用設置
- win10 UWP 九幽數據分析
- win10 UWP 圓形等待
- win10 UWP 標題欄后退
- win10 UWP 單元測試
- win10 UWP 你寫我讀
- win10 UWP RSS閱讀器
- win10 UWP MessageDialog 和 ContentDialog
- win10 UWP Hmac
- win10 UWP GET Post
- Win10 UWP Intro to controls and events
- win10 UWP Controls by function
- win10 uwp App-to-app communication 應用通信