假設你的意思是你想堅持一個鑰匙插入OS密鑰存儲(的RSACryptoServiceProvider使用名爲key),與CNG的界面有點不同:
private static ECDsa CreateOrOpenECDsaKey(string keyName)
{
CngKey key;
if (CngKey.Exists(keyName))
{
key = CngKey.Open(keyName);
}
else
{
// You can also specify options here, like if it should be exportable, in a
// different overload.
key = CngKey.Create(CngAlgorithm.ECDsaP521, keyName);
}
// The ECDsaCng constructor will duplicate the key reference, so we can close this one.
using (key)
{
return new ECDsaCng(key);
}
}
如果你的意思是你想做像RSAParameters一樣的導出/導入,該功能存在於.NET Core中,但在.NET Framework中尚不存在。
感謝您的回答,它非常有用。我已經嘗試過了,但是作爲ECDsaCng值返回,我得到一個異常:「與ECDsaCng算法一起使用的密鑰必須具有ECDsa算法組。\ r \ nParameter name:key」。我的密鑰是用BouncyCastle庫生成的,它似乎是作爲ECDH而不是ECDSA創建的。我必須在這方面找出答案。 – santiPipes
@santiPipes已修復.NET Framework 4.6.2(http://referencesource.microsoft.com/#System.Core/System/Security/Cryptography/ECDsaCng.cs,bab9826847a21b79) – bartonjs
我明白了,非常感謝@bartonjs。這意味着我必須在將要執行該軟件的所有計算機上安裝新框架,不是嗎?我想這不是隻解決框架4.6.2編譯(我知道,愚蠢的問題) – santiPipes