我现在使用的是RSACryptoServiceProvider,我想换成RSACng。我正在使用它来签署数据。更改的原因是我使用的是Pkcs1填充,我知道Pss填充是首选的。我们正在进行安全审计。
我的问题是,如何实例化RSACng,使其每次都使用相同的私钥/公钥?
对于RSACryptoServiceProvider,我正在做以下工作:
CspParameters cp = new CspParameters();
cp.KeyContainerName = "ContainerName";
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cp);传入容器名称意味着它使用了保存在机器上容器存储中的键。
在RSACng中,我尝试这样做,但得到了一个异常:“请求的操作不受支持”
RSACng RSA = new RSACng(CngKey.Create(CngAlgorithm.Sha256, ContainerName));我只需要能够传递存储密钥名称,这样它每次都使用相同的密钥,而不是生成新的密钥。
发布于 2018-06-05 23:24:29
如果要使用CNG创建命名/持久化RSA密钥:
private static RSA CreatePersistedRSAKey(string name, int keySizeInBits)
{
CngKeyCreationParameters creationParameters = new CngKeyCreationParameters
{
// This is what an ephemeral key would have had
// (allows ExportParameters(true) to succeed). Adjust as desired.
//
// The default is not exportable (only applies to the private key)
ExportPolicy =
CngExportPolicies.AllowExport | CngExportPolicies.AllowPlaintextExport,
};
creationParameters.Parameters.Add(
new CngProperty(
"Length",
BitConverter.GetBytes(keySizeInBits),
CngPropertyOptions.Persist));
// RSACng will extract the data it needs from this key object,
// but doesn't take ownership
using (CngKey key = CngKey.Create(CngAlgorithm.Rsa, name, creationParameters))
{
return new RSACng(key);
}
}这将跳过在调用CngKey.Open时需要尝试/捕获的部分,或者可能想要删除键(使用CngKey.Open打开它,然后在CngKey实例上调用Delete键)。
(在net46中添加了CngAlgorithm.Rsa。如果您使用的是较旧的版本,则等效项为new CngAlgorithm("RSA"))
https://stackoverflow.com/questions/50689372
复制相似问题