介紹
重新想象 Windows 8 Store Apps 之 加密解密
非對稱算法(RSA)
簽名和驗證簽名 (RSA)
通過 CryptographicBuffer 來實現 string hex base64 binary 間的相互轉換
示例
1、 演示如何使用非對稱算法(RSA)
Crypto/Asymmetric.xaml.cs
/*
* 演示如何使用非對稱算法(RSA)
*/
using System;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Crypto
{
public sealed partial class Asymmetric : Page
{
public Asymmetric()
{
this.InitializeComponent();
}
private void btnDemo_Click(object sender, RoutedEventArgs e)
{
string plainText = "i am webabcd";
uint keySize = 2048;
lblMsg.Text = "原文: " + plainText;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "keySize: " + keySize / 8;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine;
string[] algorithmNames = { "RSA_PKCS1", "RSA_OAEP_SHA1", "RSA_OAEP_SHA256", "RSA_OAEP_SHA384", "RSA_OAEP_SHA512" };
foreach (var algorithmName in algorithmNames)
{
/*
* 對於 RSA 非對稱加密來說,其對原文的長度是有限制的,所以一般用 RSA 來加密對稱算法的密鑰
*
* RSA_PKCS1 要求原文長度 <= 密鑰長度 - 3,單位:字節
* OAEP 要求原文長度 <= 密鑰長度 - 2 * HashBlock - 2,單位:字節
* RSA_OAEP_SHA1 - 密鑰長度為 1024 時,最大原文長度 1024 / 8 - 2 * 20 - 2 = 90
* RSA_OAEP_SHA256 - 密鑰長度為 1024 時,最大原文長度 1024 / 8 - 2 * (256 / 8) - 2 = 66
* RSA_OAEP_SHA384 - 密鑰長度為 2048 時,最大原文長度 2048 / 8 - 2 * (384 / 8) - 2 = 162
* RSA_OAEP_SHA512 - 密鑰長度為 2048 時,最大原文長度 2048 / 8 - 2 * (512 / 8) - 2 = 130
*/
IBuffer buffer; // 原文
IBuffer encrypted; // 加密後
IBuffer decrypted; // 解密後
IBuffer blobPublicKey; // 公鑰
IBuffer blobKeyPair; // 公鑰私鑰對
CryptographicKey keyPair; // 公鑰私鑰對
// 原文的二進制數據
buffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
// 根據算法名稱實例化一個非對稱算法提供程序
AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithmName);
try
{
// 根據密鑰長度隨機創建一個公鑰私鑰對
keyPair = asymmetricAlgorithm.CreateKeyPair(keySize);
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
return;
}
// 加密數據(通過公鑰)
encrypted = CryptographicEngine.Encrypt(keyPair, buffer, null);
// 加密後的結果
lblMsg.Text += algorithmName + " encrypted: " + CryptographicBuffer.EncodeToHexString(encrypted) + " (" + encrypted.Length + ")";
lblMsg.Text += Environment.NewLine;
// 導出公鑰
blobPublicKey = keyPair.ExportPublicKey();
// 導出公鑰私鑰對
blobKeyPair = keyPair.Export();
// 導入公鑰
CryptographicKey publicKey = asymmetricAlgorithm.ImportPublicKey(blobPublicKey);
// 導入公鑰私鑰對
CryptographicKey keyPair2 = asymmetricAlgorithm.ImportKeyPair(blobKeyPair);
// 解密數據(通過私鑰)
decrypted = CryptographicEngine.Decrypt(keyPair2, encrypted, null);
// 解密後的結果
lblMsg.Text += algorithmName + " decrypted: " + CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decrypted);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine;
}
}
}
}
2、演示如何通過非對稱算法(RSA)來簽名和驗證簽名
Crypto/Sign.xaml.cs
/*
* 演示如何通過非對稱算法(RSA)來簽名和驗證簽名
*/
using System;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Crypto
{
public sealed partial class Sign : Page
{
public Sign()
{
this.InitializeComponent();
}
private void btnDemo_Click(object sender, RoutedEventArgs e)
{
string plainText = "i am webabcd";
uint keySize = 2048;
lblMsg.Text = "原文: " + plainText;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "keySize: " + keySize / 8;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine;
string[] algorithmNames = { "RSASIGN_PKCS1_SHA1", "RSASIGN_PKCS1_SHA256", "RSASIGN_PKCS1_SHA384", "RSASIGN_PKCS1_SHA512", "RSASIGN_PSS_SHA1", "RSASIGN_PSS_SHA256", "RSASIGN_PSS_SHA384", "RSASIGN_PSS_SHA512" };
foreach (var algorithmName in algorithmNames)
{
IBuffer buffer; // 原文
IBuffer blobPublicKey; // 公鑰
IBuffer blobKeyPair; // 公鑰私鑰對
CryptographicKey keyPair; // 公鑰私鑰對
// 原文的二進制數據
buffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
// 根據算法名稱實例化一個非對稱算法提供程序
AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithmName);
try
{
// 根據密鑰長度隨機創建一個公鑰私鑰對
keyPair = asymmetricAlgorithm.CreateKeyPair(keySize);
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
return;
}
// 對原文進行簽名(通過私鑰)
IBuffer signature = CryptographicEngine.Sign(keyPair, buffer);
lblMsg.Text += algorithmName + " - 原文已被簽名,簽名後的數據: " + CryptographicBuffer.EncodeToHexString(signature);
lblMsg.Text += Environment.NewLine;
// 導出公鑰
blobPublicKey = keyPair.ExportPublicKey();
// 導出公鑰私鑰對
blobKeyPair = keyPair.Export();
// 導入公鑰
CryptographicKey publicKey = asymmetricAlgorithm.ImportPublicKey(blobPublicKey);
// 驗證簽名(通過公鑰)
bool isAuthenticated = CryptographicEngine.VerifySignature(publicKey, buffer, signature);
lblMsg.Text += "簽名驗證的結果: " + isAuthenticated;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine;
}
}
}
}
3、通過 CryptographicBuffer 來實現 string hex base64 binary 間的相互轉換
Crypto/CryptographicBufferDemo.xaml.cs
/*
* 通過 CryptographicBuffer 來實現 string hex base64 binary 間的相互轉換
*
* 注:CryptographicBuffer 相當於加解密過程中的一個輔助類
*/
using Windows.Security.Cryptography;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Crypto
{
public sealed partial class CryptographicBufferDemo : Page
{
public CryptographicBufferDemo()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 將 IBuffer 對象轉換為 string
// string CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding encoding, IBuffer buffer);
// 將 string 轉換為 IBuffer 對象
// IBuffer CryptographicBuffer.ConvertStringToBinary(string value, BinaryStringEncoding encoding);
// 將 IBuffer 對象中的數據寫入到 byte[]
// void CryptographicBuffer.CopyToByteArray(IBuffer buffer, out byte[] value);
// 將 byte[] 轉換為 IBuffer 對象
// IBuffer CryptographicBuffer.CreateFromByteArray(byte[] value);
// 將 base64 編碼字符串轉換為 IBuffer 對象
// IBuffer CryptographicBuffer.DecodeFromBase64String(string value);
// 將 十六 進制編碼字符串轉換為 IBuffer 對象
// IBuffer CryptographicBuffer.DecodeFromHexString(string value);
// 將 IBuffer 對象中的數據轉換為 base64 編碼字符串
// string CryptographicBuffer.EncodeToBase64String(IBuffer buffer);
// 將 IBuffer 對象中的數據轉換為 十六 進制編碼字符串
// string CryptographicBuffer.EncodeToHexString(IBuffer buffer);
// 生成一個 uint 類型的隨機數
// uint CryptographicBuffer.GenerateRandomNumber();
// 根據指定長度生成一個包含隨機數據的 IBuffer 對象
// IBuffer CryptographicBuffer.GenerateRandom(uint length);
// 比較兩個 IBuffer 對象是否相等
// bool CryptographicBuffer.Compare(IBuffer object1, IBuffer object2);
}
}
}
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar