原文:重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类

[源码下载]

重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类

作者:webabcd

介绍
重新想象 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 = ; lblMsg.Text = "原文: " + plainText;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "keySize: " + keySize / ;
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 = ; lblMsg.Text = "原文: " + plainText;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "keySize: " + keySize / ;
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
[源码下载]

重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类的更多相关文章

  1. 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法

    原文:重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法 [源码下载] 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈 ...

  2. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  3. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  4. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...

  5. 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLock

    [源码下载] 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLoc ...

  6. 重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider

    原文:重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider [源码下载] 重新想象 Wind ...

  7. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  8. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  9. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

随机推荐

  1. .idata数据的解析

    每类Section代表不同的数据,不同的数据存储组织方式一定是有非常大区别的.代码段与资源段一定区别巨大,这意味着我需要一个一个的学习每个段的解析. idata段解析 这个段主要存储的是导入符号信息. ...

  2. hdu4521(线段树+dp)

    传送门:小明系列问题——小明序列 题意:有n个数,求间距大于d的最长上升序列. 分析:dp[i]表示在i点以a[i]结束距离大于d的最长上升序列,然后每更新到第i点时,取i-d之前小于a[i]的数为结 ...

  3. windows 2003 自动安全设置

    @echo offecho.echo.echo.echo 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓echo.echo.echo windows 2003 自动安全设置程序 echo. ec ...

  4. c4Droid

    c4可以让用c/c++写的源码打包成apk安装包,支持Console.SDL.Qt. NativeActivity 等一系列扩展库,可以用来写软件,也可以用来写游戏,是手机端练习c/c++的神器.c4 ...

  5. MVC超链接

    <1> return RedirectToAction(Index); //跳转到Index控制器 <%@ Page Language="C#" Inherits ...

  6. C++ Primer 学习笔记_98_特殊的工具和技术 --优化内存分配

    特殊的工具和技术 --优化内存分配 引言: C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自己主动执行合适的构造函数来初始化每一个动态 ...

  7. JVM指令集(指令码、助记符、功能描述)(转)

    JVM指令集(指令码.助记符.功能描述) 指令码 助记符 功能描述 0x00 nop 无操作 0x01 aconst_null 指令格式:  aconst_null 功能描述:  null进栈. 指令 ...

  8. for循环中一个不容小觑的问题

    for(int i=1;i<=100;i++) 作为程序猿,我们很喜欢使用这种for循环. 可是,当中隐含着一个重要的问题. 过多的编程经历可能使我们的思维产生了一些误解,在上面的for循环中, ...

  9. C#实现仿QQ震动

    前提:新建winForm窗体应用程序,放置一个Button,设置按钮的单击事件 ; i < ; i++) { Point p = this.FindForm().Location; ,p.Y+) ...

  10. PHP Html 弹窗,本页面弹窗子页面

    echo '<script type=text/javascript>window.open("","name1","width=100, ...