⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 C#高级编程第6版随书源代码 值得下载
💻 CS
字号:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Wrox.ProCSharp.Security
{
   class Program
   {
      static CngKey aliceKey;
      static CngKey bobKey;
      static byte[] alicePubKeyBlob;
      static byte[] bobPubKeyBlob;

      static void Main()
      {
         CreateKeys();
         byte[] encrytpedData = AliceSendsData("secret message");
         BobReceivesData(encrytpedData);

      }

      private static void BobReceivesData(byte[] encryptedData)
      {
         Console.WriteLine("Bob receives encrpyted data");
         byte[] rawData = null;

         AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
         
         int nBytes = aes.BlockSize >> 3;
         byte[] iv = new byte[nBytes];
         for (int i = 0; i < iv.Length; i++)
            iv[i] = encryptedData[i];

         ECDiffieHellmanCng bobAlgorithm = new ECDiffieHellmanCng(bobKey);

         using (CngKey alicePubKey = CngKey.Import(alicePubKeyBlob, CngKeyBlobFormat.EccPublicBlob))
         {
            byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);
            Console.WriteLine("Bob creates this 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())
            {
               CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
               cs.Write(encryptedData, nBytes, encryptedData.Length - nBytes);
               cs.Close();

               rawData = ms.ToArray();

               Console.WriteLine("Bob decrypts message to: {0}", Encoding.UTF8.GetString(rawData));
            
            }
            aes.Clear();
         }
      }

      private static byte[] AliceSendsData(string message)
      {
         Console.WriteLine("Alice sends message: {0}", message);
         byte[] rawData = Encoding.UTF8.GetBytes(message);
         byte[] encryptedData = null;

         ECDiffieHellmanCng aliceAlgorithm = new ECDiffieHellmanCng(aliceKey);
         using (CngKey bobPubKey = CngKey.Import(bobPubKeyBlob, CngKeyBlobFormat.EccPublicBlob))
         {
            byte[] symmKey = aliceAlgorithm.DeriveKeyMaterial(bobPubKey);
            Console.WriteLine("Alice creates this key with Bobs public key information: {0}", Convert.ToBase64String(symmKey));

            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            aes.Key = symmKey;
            aes.GenerateIV();
            using (ICryptoTransform encryptor = aes.CreateEncryptor())
            using (MemoryStream ms = new MemoryStream())
            {
               // create CryptoStream and encrypt data to send
               CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
               ms.Write(aes.IV, 0, aes.IV.Length); // write the initialization vector not encrypted
               cs.Write(rawData, 0, rawData.Length);
               cs.Close();
               encryptedData = ms.ToArray();
            }
            aes.Clear();
         }
         Console.WriteLine("Alice: message is encrypted: {0}", Convert.ToBase64String(encryptedData)); ;
         return encryptedData;
      }

      private static void CreateKeys()
      {
         aliceKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
         bobKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256);
         alicePubKeyBlob = aliceKey.Export(CngKeyBlobFormat.EccPublicBlob);
         bobPubKeyBlob = bobKey.Export(CngKeyBlobFormat.EccPublicBlob);
      }


   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -