我正在尝试使用CNG API和Microsoft证书库中的证书使用ECDSA签署文件.我已阅读了大量文档,并且已接近完成但我仍然挂断了从证书导入私钥.我用RSA完成了同样的事情,但似乎完全不同.这是我到目前为止的代码:
static void signFile()
{
X509Certificate2 myCert =
selectCert(StoreName.My,
StoreLocation.CurrentUser,
"Select a Certificate",
"Please select a certificate from the list below:");
Console.Write("Path for file to sign: ");
string path = Console.ReadLine();
TextReader file = null;
try
{
file = new StreamReader(path);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Write("\nPress any key to return to the main menu: ");
Console.ReadKey();
}
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(file.ReadToEnd());
ECDsaCng dsa = new ECDsaCng(
CngKey.Import(StringToByteArray(myCert.PrivateKey.ToString()),
CngKeyBlobFormat.EccPrivateBlob,
CngProvider.MicrosoftSoftwareKeyStorageProvider));
dsa.HashAlgorithm = CngAlgorithm.Sha384; …Run Code Online (Sandbox Code Playgroud) 我有一项任务,需要使用NIST SP 800-56A第5.8.1节中描述的密钥导出函数来获取密钥材料.我不是密码学方面的专家,所以如果问题很幼稚,请原谅我.这是我到目前为止所做的:
现在我尝试使用C#(.NET 4)ECDiffieHellmanCng类使用ECDH 1.3.132.1.12生成共享密钥,如下所示:
// The GetCngKey method reads the private key from a certificate in my Personal certificate store
CngKey cngPrivateKey = GetCngKey();
ECDiffieHellmanCng ecDiffieHellmanCng = new ECDiffieHellmanCng(cngPrivateKey);
ecDiffieHellmanCng.HashAlgorithm = CngAlgorithm.ECDiffieHellmanP256;
ecDiffieHellmanCng.KeyDerivationFunction = ?? // What do I set here
Run Code Online (Sandbox Code Playgroud)最后这样做:
ecDiffieHellmanCng.DeriveKeyMaterial(otherPartyPublicKey:);
Run Code Online (Sandbox Code Playgroud)
在哪里/如何设置其他参数算法ID,Party U Info,Party V Info?
编辑 我愿意使用像Bouncy Castle这样的其他库(只要它们可以从.NET调用)
我正在尝试从ECDiffieHellmanCng对象的新实例导出密钥,以便稍后可以使用相同的密钥创建它的实例.但是我在尝试导出它时遇到错误.
//Create new ECDiffieHellmanCng which automatically creates new keys
var ecdh = new ECDiffieHellmanCng();
//Export the keys
var privateKey = ecdh.Key.Export(CngKeyBlobFormat.EccPrivateBlob);
Run Code Online (Sandbox Code Playgroud)
当我使用消息" 不支持请求的操作 " 调用Export方法时,我收到CryptographicException .在代码中放入一些断点之后,它甚至在执行方法之前抛出了异常.查看Export方法的定义,它使用SecuritySafeCriticalAttribute进行装饰,因此我怀疑此属性实际上是在抛出异常.导致此异常的原因是什么?如何保存密钥以便以后可以创建相同ECDiffieHellmanCng对象的实例?