【发布时间】:2010-10-15 23:44:54
【问题描述】:
您能否建议在 .NET 平台上使用椭圆曲线密码学的任何实现?
另外如果你用过的话,能不能告诉我推荐的应该用的曲线?
[编辑]
正如@FatCat 提到的,它的实现在 .NET 框架 3.5 中可用,但仅在 windows vista 上可用。您能否建议另一种使用方式/库?
【问题讨论】:
标签: .net encryption cryptography public-key-encryption elliptic-curve
您能否建议在 .NET 平台上使用椭圆曲线密码学的任何实现?
另外如果你用过的话,能不能告诉我推荐的应该用的曲线?
[编辑]
正如@FatCat 提到的,它的实现在 .NET 框架 3.5 中可用,但仅在 windows vista 上可用。您能否建议另一种使用方式/库?
【问题讨论】:
标签: .net encryption cryptography public-key-encryption elliptic-curve
.NET Framework 已经包含 Diffie-Hellman,这是一种椭圆曲线加密算法。查看System.Security.Cryptography.ECDiffieHellmanCng。
【讨论】:
查看 C# 的 Bouncy Castle 库,它有 ECDH 和 ECDSA。
【讨论】:
看看SecureBlackBox组件
【讨论】:
您通常使用 ECC 进行加密的方式是使用“Ephemeral-Static Diffie-Hellman”。
它是这样工作的:
接收者现在可以使用临时公钥和他自己的静态私钥来重新创建对称密钥并解密数据。
您可以在Standards for Efficient Cryptography: SEC 1: Elliptic Curve Cryptography 5.1.3 部分阅读更多内容。
【讨论】:
太棒了!我试过了,但找不到如何使用它来加密消息。 似乎没有任何“加密”功能
这是System.Security.Cryptography.ECDiffieHellmanCng 的 MSDN 示例。
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);
}
}
}
}
}
【讨论】: