请您建议在.NET平台上使用椭圆曲线密码学的任何实现吗?
另外,如果您使用过它们,您能告诉我应该使用的推荐曲线吗?
[编辑]
正如@FatCat 提到的,它的实现在 .NET 框架 3.5 中可用,但仅在 windows vista 上可用。您能否建议另一种使用方式/库?
请您建议在.NET平台上使用椭圆曲线密码学的任何实现吗?
另外,如果您使用过它们,您能告诉我应该使用的推荐曲线吗?
[编辑]
正如@FatCat 提到的,它的实现在 .NET 框架 3.5 中可用,但仅在 windows vista 上可用。您能否建议另一种使用方式/库?
.NET Framework 已经包含 Diffie-Hellman,它是一种椭圆曲线加密算法。查看System.Security.Cryptography.ECDiffieHellmanCng。
查看 C# 的Bouncy Castle库,它有 ECDH 和 ECDSA。
通常使用 ECC 进行加密的方式是使用“Ephemeral-Static Diffie-Hellman”。
它是这样工作的:
接收者现在可以使用临时公钥和他自己的静态私钥来重新创建对称密钥并解密数据。
您可以在“高效密码学标准”中了解更多信息:SEC 1:椭圆曲线密码学第 5.1.3 节。
查看SecureBlackBox组件
伟大的!我试过了,但找不到如何使用它来加密消息。似乎没有任何“加密”功能
这是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);
}
}
}
}
}