.NET Core 3.1 [Windows 10 Pro OS] 中的自签名RSA证书导出私钥参数异常。

问题描述 投票:0回答:1

我已经创建了一个自签名的 RSA 证书,并将私钥存储为 .pfx 文件,然后在我的 .Net Core 3.1 代码中,我试图用 .pfx 文件实例化 X509Certificate2 对象。然后在我的 .Net Core 3.1 代码中,我试图用 .pfx 文件实例化 X509Certificate2 对象。X509Certificate2 实例创建成功,但在 "certificate2.GetRSAPrivateKey().ExportParameters(true) "代码中得到一个异常 "The requested operation is not supported"。

X509Certificate2 certificate2 = new X509Certificate2(privateKeyData, _privateKeyPwd, X509KeyStorageFlags.Exportable);
RSAParameters rSAParameters = certificate2.GetRSAPrivateKey().ExportParameters(true);

Exception:Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException: 'The requested operation is not supported'。

请你帮帮我。

编辑:rSAParameters将用于解密加密的对称密钥。

rsaProvider.ImportParameters(rSAParameters);
byte[] encryptedSymmetricKey = Convert.FromBase64String(dataKey);
// Decrypt using OAEP padding.
byte[] decryptedSymmetricKey = rsaProvider.Decrypt(encryptedSymmetricKey, fOAEP: true);

rsa x509certificate2 encryption-asymmetric .net-core-3.1 windows-10-desktop
1个回答
2
投票

当我看到这样的信息 rsaKey.ExportParameters(true),在99.999%的情况下,这说明代码中的设计模式不对。

事实上,你不需要导出和重新导入参数,只需要简单的做。

X509Certificate2 certificate2 = new X509Certificate2(privateKeyData, _privateKeyPwd, X509KeyStorageFlags.Exportable);
RSA privateKey = certificate2.GetRSAPrivateKey();
// decrypt data
byte[] decryptedSymmetricKey = privateKey.Decrypt(encryptedSymmetricKey, RSAEncryptionPadding.OaepSHA1);
© www.soinside.com 2019 - 2024. All rights reserved.