public class ChangeKey
{
static CngKey alicKey;
static CngKey bobKey;
static byte[] alicePubkeyBlob;
static byte[] bobPubkeyBlob;
/// <summary>
/// 交互密钥和安全传输
/// 双方有各自密钥,同时也有对方的公钥
/// 在传输过程中,先采用非对称加密,而后再进行对称加密
/// 对方获取后进行非对称解密,后进行对称解密
/// </summary>
public static void Smain()
{
CreateKeys();
byte[] encrytpedData = AliceSendsData("secret message");
BobReceiversData(encrytpedData);
}
/// <summary>
/// 生成密钥
/// </summary>
private static void CreateKeys()
{
alicKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);//用指定算法生成密钥
bobKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
alicePubkeyBlob = alicKey.Export(CngKeyBlobFormat.EccPublicBlob);//获取公钥
bobPubkeyBlob = bobKey.Export(CngKeyBlobFormat.EccPublicBlob);
}
/// <summary>
/// 数据加密的时候,向量未进行加密处理
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
private static byte[] AliceSendsData(string message)
{
Console.WriteLine("Alice sends message:{0}", message);
byte[] rawData = Encoding.UTF8.GetBytes(message);
byte[] encryptedData = null;
using (var aliceAlgorithm = new ECDiffieHellmanCng(alicKey))
using (CngKey bobPubKey = CngKey.Import(bobPubkeyBlob, CngKeyBlobFormat.EccPublicBlob))
{
byte[] symmkey = aliceAlgorithm.DeriveKeyMaterial(bobPubKey);
Console.WriteLine("Alice creates this sysmmetric key with " +
"Bobs public key information:{0}", Convert.ToBase64String(symmkey));
var aes = new AesCryptoServiceProvider();
aes.Key = symmkey;
aes.GenerateIV();
using (ICryptoTransform encryptor = aes.CreateEncryptor())
using (MemoryStream ms = new MemoryStream())
{
var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
ms.Write(aes.IV, 0, aes.IV.Length);
cs.Write(rawData, 0, rawData.Length);
cs.Close();
encryptedData = ms.ToArray();
}
aes.Clear();
}
Console.WriteLine("Alice :Message is encrypted:{0}", Convert.ToBase64String(encryptedData));
Console.WriteLine();
return encryptedData;
}
private static void BobReceiversData(byte[] encrytpedData)
{
Console.WriteLine("Bob receives encrypted data");
byte[] rawData = null;
var aes = new AesCryptoServiceProvider();
int nBytes=aes.BlockSize>>3;//主要是向量未加密直接写入流中,此处直接读取
byte[] iv = new byte[nBytes];
for (int i = 0; i < iv.Length; i++)
{
iv[i] = encrytpedData[i];
}
using(var bobAlgorithm=new ECDiffieHellmanCng(bobKey))
using (CngKey alicePubKey = CngKey.Import(alicePubkeyBlob,CngKeyBlobFormat.EccPublicBlob))
{
byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);
Console.WriteLine("Bob Creates this symmetric key with " +
"Alices public key information:{0}", Convert.ToBase64String(symmKey));
aes.Key = symmKey;
aes.IV = iv;
using (ICryptoTransform decryptor = aes.CreateDecryptor())
using (MemoryStream ms = new MemoryStream())
{
var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
cs.Write(encrytpedData, nBytes, encrytpedData.Length-nBytes);
cs.Close();
rawData = ms.ToArray();
Console.WriteLine("Bob decrypts message to :{0}", Encoding.UTF8.GetString(rawData));
}
aes.Clear();
}
}
}
ECDiffieHellmanCng
最新推荐文章于 2019-07-12 17:29:17 发布