本文整理汇总了C#中System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter类的典型用法代码示例。如果您正苦于以下问题:C# RSAOAEPKeyExchangeDeformatter类的具体用法?C# RSAOAEPKeyExchangeDeformatter怎么用?C# RSAOAEPKeyExchangeDeformatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RSAOAEPKeyExchangeDeformatter类属于System.Security.Cryptography命名空间,在下文中一共展示了RSAOAEPKeyExchangeDeformatter类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Alice
{
public static void Main(string[] args)
{
using (Bob bob = new Bob())
{
using (RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider())
{
// Get Bob's public key
rsaKey.ImportCspBlob(bob.key);
byte[] encryptedSessionKey = null;
byte[] encryptedMessage = null;
byte[] iv = null;
Send(rsaKey, "Secret message", out iv, out encryptedSessionKey, out encryptedMessage);
bob.Receive(iv, encryptedSessionKey, encryptedMessage);
}
}
}
private static void Send(RSA key, string secretMessage, out byte[] iv, out byte[] encryptedSessionKey, out byte[] encryptedMessage)
{
using (Aes aes = new AesCryptoServiceProvider())
{
iv = aes.IV;
// Encrypt the session key
RSAOAEPKeyExchangeFormatter keyFormatter = new RSAOAEPKeyExchangeFormatter(key);
encryptedSessionKey = keyFormatter.CreateKeyExchange(aes.Key, typeof(Aes));
// Encrypt the message
using (MemoryStream ciphertext = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] plaintextMessage = Encoding.UTF8.GetBytes(secretMessage);
cs.Write(plaintextMessage, 0, plaintextMessage.Length);
cs.Close();
encryptedMessage = ciphertext.ToArray();
}
}
}
}
public class Bob : IDisposable
{
public byte[] key;
private RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();
public Bob()
{
key = rsaKey.ExportCspBlob(false);
}
public void Receive(byte[] iv, byte[] encryptedSessionKey, byte[] encryptedMessage)
{
using (Aes aes = new AesCryptoServiceProvider())
{
aes.IV = iv;
// Decrypt the session key
RSAOAEPKeyExchangeDeformatter keyDeformatter = new RSAOAEPKeyExchangeDeformatter(rsaKey);
aes.Key = keyDeformatter.DecryptKeyExchange(encryptedSessionKey);
// Decrypt the message
using (MemoryStream plaintext = new MemoryStream())
using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(encryptedMessage, 0, encryptedMessage.Length);
cs.Close();
string message = Encoding.UTF8.GetString(plaintext.ToArray());
Console.WriteLine(message);
}
}
}
public void Dispose()
{
rsaKey.Dispose();
}
}
开发者ID:.NET开发者,项目名称:System.Security.Cryptography,代码行数:84,代码来源:RSAOAEPKeyExchangeDeformatter
注:本文中的System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论