C#ECDiffieHellman问题,为什么这段代码不能正常运转?

我在进行解密的时候出了一些问题,似乎无法解密,但他们的对称密钥是相同的。

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 项目1
{
 class Program
 {
  CngKey _aliceKey, _bobKey;
  byte[] _alicePubKey, _bobPubKey;
  static void Main()
  {
   new Program().RunAsync();
   Console.ReadLine();
  }
  public async void RunAsync()
  {
   CreateKeys();
   byte[] data = await Send("我是保罗");
   await Receive(data);
  }
  private void CreateKeys()
  {
   _aliceKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP521);
   _bobKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP521);
   _alicePubKey = _aliceKey.Export(CngKeyBlobFormat.EccPublicBlob);
   _bobPubKey = _bobKey.Export(CngKeyBlobFormat.EccPublicBlob);
  }
  private async Task<byte[]> Send(string msg)
  {
   byte[] data = Encoding.UTF8.GetBytes(msg);
   using (var alg = new ECDiffieHellmanCng(_aliceKey))
   {
    using (var bobPubKey = CngKey.Import(_bobPubKey, CngKeyBlobFormat.EccPublicBlob))
    {
     byte[] symmKey = alg.DeriveKeyMaterial(bobPubKey);
     Console.WriteLine(Convert.ToBase64String(symmKey));
     var aes = new AesCryptoServiceProvider();
     aes.Key = symmKey;
     aes.GenerateIV();
     using (var ms = new MemoryStream())
     {
      using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
      {
       ms.Write(aes.IV, 0, aes.IV.Length);
       cs.Write(data, 0, data.Length);
       cs.Close();
      }
      return ms.ToArray();
     }
    }
   }
  }
  public async Task Receive(byte[] data)
  {
   byte[] raw = null;
   var aes = new AesCryptoServiceProvider();
   int length = aes.BlockSize / 8;
   byte[] iv = new byte[length];
   for (int i = 0; i < iv.Length; i++)
   {
    iv[i] = data[i];
   }
   using (var alg = new ECDiffieHellmanCng(_bobKey))
   using (var alicePubKey = CngKey.Import(_alicePubKey, CngKeyBlobFormat.EccPublicBlob))
   {
    byte[] symmKey = alg.DeriveKeyMaterial(alicePubKey);
    Console.WriteLine(Convert.ToBase64String(symmKey));
    aes.Key = symmKey;
    aes.IV = iv;
    using (var ms1 = new MemoryStream())
    {
     using (var cs = new CryptoStream(ms1, aes.CreateDecryptor(), CryptoStreamMode.Write))
     {
      await cs.WriteAsync(iv, length, iv.Length - length);
      ms1.Position = 0;
      while (true)
      {
       Console.WriteLine(ms1.Length);
      }
     }
    }
    aes.Clear();
   }
   //Console.WriteLine(Encoding.UTF8.GetString(raw));
   //Console.WriteLine(raw.Length);
  }
 }
}
阅读 1.9k
1 个回答
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
class Program
{
    CngKey _aliceKey, _bobKey;
    byte[] _alicePubKey, _bobPubKey;
    static void Main()
    {
        new Program().RunAsync();
        Console.ReadLine();
    }
    public async void RunAsync()
    {
        CreateKeys();
        byte[] data = await Send("我是保罗");
        await Receive(data);
    }
    private void CreateKeys()
    {
        _aliceKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP521);
        _bobKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP521);
        _alicePubKey = _aliceKey.Export(CngKeyBlobFormat.EccPublicBlob);
        _bobPubKey = _bobKey.Export(CngKeyBlobFormat.EccPublicBlob);
    }
    private async Task<byte[]> Send(string msg)
    {
        byte[] data = Encoding.UTF8.GetBytes(msg);
        using (var alg = new ECDiffieHellmanCng(_aliceKey))
        {
            using (var bobPubKey = CngKey.Import(_bobPubKey, CngKeyBlobFormat.EccPublicBlob))
            {
                byte[] symmKey = alg.DeriveKeyMaterial(bobPubKey);
                Console.WriteLine(Convert.ToBase64String(symmKey));
                var aes = new AesCryptoServiceProvider();
                aes.Key = symmKey;
                aes.GenerateIV();
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        ms.Write(aes.IV, 0, aes.IV.Length);
                        cs.Write(data, 0, data.Length);
                        cs.Close();
                    }
                    return ms.ToArray();
                }
            }
        }
    }
    public async Task Receive(byte[] data)
    {
        byte[] raw = null;
        var aes = new AesCryptoServiceProvider();
        int length = aes.BlockSize / 8;
        byte[] iv = new byte[length];
        for (int i = 0; i < iv.Length; i++)
        {
            iv[i] = data[i];
        }
        using (var alg = new ECDiffieHellmanCng(_bobKey))
        using (var alicePubKey = CngKey.Import(_alicePubKey, CngKeyBlobFormat.EccPublicBlob))
        {
            byte[] symmKey = alg.DeriveKeyMaterial(alicePubKey);
            Console.WriteLine(Convert.ToBase64String(symmKey));
            aes.Key = symmKey;
            aes.IV = iv;
            using (var ms1 = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms1, aes.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    await cs.WriteAsync(iv, length, iv.Length - length);   <-  ❌  这里应该是data 、data.Length - length
                    ms1.Position = 0;
                    while (true)
                    {
                        Console.WriteLine(ms1.Length);
                    }
                }
            }
            aes.Clear();
        }
        //Console.WriteLine(Encoding.UTF8.GetString(raw));
        //Console.WriteLine(raw.Length);
    }
}

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏