18

请您建议在.NET平台上使用椭圆曲线密码学的任何实现吗?

另外,如果您使用过它们,您能告诉我应该使用的推荐曲线吗?

[编辑]

正如@FatCat 提到的,它的实现在 .NET 框架 3.5 中可用,但仅在 windows vista 上可用。您能否建议另一种使用方式/库?

4

5 回答 5

13

.NET Framework 已经包含 Diffie-Hellman,它是一种椭圆曲线加密算法。查看System.Security.Cryptography.ECDiffieHellmanCng

于 2009-03-27T17:17:15.310 回答
11

查看 C# 的Bouncy Castle库,它有 ECDH 和 ECDSA。

于 2009-04-13T18:10:30.907 回答
3

通常使用 ECC 进行加密的方式是使用“Ephemeral-Static Diffie-Hellman”。

它是这样工作的:

  • 获取预期的接收者公钥(可能来自证书)。这是静态键。
  • 生成临时 ECDH 密钥对。这是临时密钥对。
  • 使用密钥生成共享对称密钥。
  • 使用对称密钥加密数据。
  • 将加密数据与来自临时密钥对的公钥一起传输。

接收者现在可以使用临时公钥和他自己的静态私钥来重新创建对称密钥并解密数据。

您可以在“高效密码学标准”中了解更多信息:SEC 1:椭圆曲线密码学第 5.1.3 节。

于 2009-03-30T06:33:06.940 回答
3

查看SecureBlackBox组件

于 2009-04-16T15:19:58.963 回答
1

伟大的!我试过了,但找不到如何使用它来加密消息。似乎没有任何“加密”功能

这是System.Security.Cryptography.ECDiffieHellmanCng.

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


class Alice
{
    public static byte[] alicePublicKey;

    public static void Main(string[] args)
    {
        using (ECDiffieHellmanCng alice = new ECDiffieHellmanCng())
        {

            alice.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            alice.HashAlgorithm = CngAlgorithm.Sha256;
            alicePublicKey = alice.PublicKey.ToByteArray();
            Bob bob = new Bob();
            CngKey k = CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob);
            byte[] aliceKey = alice.DeriveKeyMaterial(CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob));
            byte[] encryptedMessage = null;
            byte[] iv = null;
            Send(aliceKey, "Secret message", out encryptedMessage, out iv);
            bob.Receive(encryptedMessage, iv);
        }

    }

    private static void Send(byte[] key, string secretMessage, out byte[] encryptedMessage, out byte[] iv)
    {
        using (Aes aes = new AesCryptoServiceProvider())
        {
            aes.Key = key;
            iv = aes.IV;

            // 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 
{
    public byte[] bobPublicKey;
    private byte[] bobKey;
    public Bob()
    {
        using (ECDiffieHellmanCng bob = new ECDiffieHellmanCng())
        {

            bob.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            bob.HashAlgorithm = CngAlgorithm.Sha256;
            bobPublicKey = bob.PublicKey.ToByteArray();
            bobKey = bob.DeriveKeyMaterial(CngKey.Import(Alice.alicePublicKey, CngKeyBlobFormat.EccPublicBlob));

        }
    }

    public void Receive(byte[] encryptedMessage, byte[] iv)
    {

        using (Aes aes = new AesCryptoServiceProvider())
        {
            aes.Key = bobKey;
            aes.IV = iv;
            // 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);
                }
            }
        }
    }

}
于 2015-01-20T08:09:28.517 回答