X509Certificate2 - 如果使用 ECC 证书,则拒绝访问异常
X509Certificate2 - Access Denied Exception if use ECC certificate
我尝试使用 System.Security.Cryptography.Pkcs
加密和解密数据,但它仅适用于 RSA 证书,如果我使用 ECC(曲线 ECDH_brainpoolP512r1)证明 X509Certificate2
的构造函数因 Access Denied
异常而崩溃。
在 Powershell 中创建 SelfSignedCertificate
New-SelfSignedCertificate `
-Subject "CN=Test Code Signing RSA" `
-Type DocumentEncryptionCert `
-KeyUsage "DigitalSignature" `
-FriendlyName "Test Code Signing" `
-NotAfter (get-date).AddYears(5) `
-KeyExportPolicy Exportable `
-SmimeCapabilities `
-KeyAlgorithm ECDH_brainpoolP512r1
在代码中读取 SelfSignedCertificate
var base64cert = "MIIF/wIBAzCCBbsGCSqGSIb3DQEHAaCCBawEggWoMIIFpDCCAkcGCSqGSIb3DQEHAaCCAjgEggI0MIICMDCCAiwGCyqGSIb3DQEMCgECoIIBNjCCATIwHAYKKoZIhvcNAQwBAzAOBAi2P5j9EliEaQICB9AEggEQyJLkopAMyHJh0jQXtnlwK4yjpE0WqYXf9sNPPLOFXgaxNU7gLKc3F6kPJUxLCxnvjOe7bRJS3v4A0GQBBqeFEJjBT9hd88RaQ2NsNxDrQEh/ZAyTUg+l6CyApUtcJb5uehPVnj7xnWtu4vvxDh5hRqSVxSR50wOjk/MKlyX1hhF1JybzRiqESKIMLx84HWJqZ6Fp87asJ0/0isL+kVxarqLrTkv0CGt2QaLxZzu9YDGj6nuGy2EBQwGHwMCEVTFupX55njV4aU3YTG2U+BHFl667NekTtOXH5GXDbp6D+9PntXBxW2d3E68v7lBVMjPKfTsTeCs4aLOwQzsXIFgvouw6GgGsZCrYaQwMNuGayC4xgeIwDQYJKwYBBAGCNxECMQAwEwYJKoZIhvcNAQkVMQYEBAEAAAAwXQYJKoZIhvcNAQkUMVAeTgB0AGUALQA0ADYANwBmADEAOAAxAGMALQBiAGQAZQA0AC0ANAA5AGUANgAtAGEANABjADMALQA4ADQAOAAwADYAMgB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swAzNbnqetOBuJy55+SkO2OsngJAGXIYtW8RBFcTmRYnhCLeIsB/De3khytnaeHNBZVB/x0n/gFqVNMaPZp6l4MPGhEBS8pcvLN4zvO7phxR0Xt3HDFKMBMGCSqGSIb3DQEJFTEGBAQBAAAAMDMGCSqGSIb3DQEJFDEmHiQAVABlAHMAdAAgAEMAbwBkAGUAIABTAGkAZwBuAGkAbgBnAAAwOzAfMAcGBSsOAwIaBBSas13IRWnhNtoPLKp29FJpLmCptgQUkF0JRqyYiDG0Ql7zAPED2uVWzykCAgfQ";
new System.Security.Cryptography.X509Certificates.X509Certificate2(Convert.FromBase64String(base64cert), "qwert");
异常
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Access Denied
at Internal.Cryptography.Pal.CertificatePal.FilterPFXStore(Byte[] rawData, SafePasswordHandle password, PfxCertStoreFlags pfxCertStoreFlags)
at Internal.Cryptography.Pal.CertificatePal.FromBlobOrFile(Byte[] rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)
at PkcsEncryption.Program.Certificate(Boolean rsa) in c:\git\PkcsEncryption\PkcsEncryption\Program.cs:line 88
at PkcsEncryption.Program.Encrypt(Byte[] dataPlain, Boolean useRsa) in c:\git\PkcsEncryption\PkcsEncryption\Program.cs:line 56
at PkcsEncryption.Program.Main(String[] args) in c:\git\PkcsEncryption\PkcsEncryption\Program.cs:line 22
您在那里编码为 base64 的 PFX 已在内部设置标记,表明它是从机器密钥库导出的。您的访问被拒绝表示您不是 运行 管理员(因此无权将密钥添加到计算机的密钥库)。
为确保将来自 PFX 的密钥添加到当前用户的密钥库中,请设置 X509KeyStorageFlags.UserKeySet
标志。或者,如果您已经安装了 .NET Framework v4.7.2 的早期访问版本(或将来发布的版本),您可以使用 EphemeralKeySet
将私钥保存在内存中并完全避免使用密钥库。
new System.Security.Cryptography.X509Certificates.X509Certificate2(
Convert.FromBase64String(base64cert),
"qwert",
X509KeyStorageFlags.UserKeySet);
我尝试使用 System.Security.Cryptography.Pkcs
加密和解密数据,但它仅适用于 RSA 证书,如果我使用 ECC(曲线 ECDH_brainpoolP512r1)证明 X509Certificate2
的构造函数因 Access Denied
异常而崩溃。
在 Powershell 中创建 SelfSignedCertificate
New-SelfSignedCertificate `
-Subject "CN=Test Code Signing RSA" `
-Type DocumentEncryptionCert `
-KeyUsage "DigitalSignature" `
-FriendlyName "Test Code Signing" `
-NotAfter (get-date).AddYears(5) `
-KeyExportPolicy Exportable `
-SmimeCapabilities `
-KeyAlgorithm ECDH_brainpoolP512r1
在代码中读取 SelfSignedCertificate
var base64cert = "MIIF/wIBAzCCBbsGCSqGSIb3DQEHAaCCBawEggWoMIIFpDCCAkcGCSqGSIb3DQEHAaCCAjgEggI0MIICMDCCAiwGCyqGSIb3DQEMCgECoIIBNjCCATIwHAYKKoZIhvcNAQwBAzAOBAi2P5j9EliEaQICB9AEggEQyJLkopAMyHJh0jQXtnlwK4yjpE0WqYXf9sNPPLOFXgaxNU7gLKc3F6kPJUxLCxnvjOe7bRJS3v4A0GQBBqeFEJjBT9hd88RaQ2NsNxDrQEh/ZAyTUg+l6CyApUtcJb5uehPVnj7xnWtu4vvxDh5hRqSVxSR50wOjk/MKlyX1hhF1JybzRiqESKIMLx84HWJqZ6Fp87asJ0/0isL+kVxarqLrTkv0CGt2QaLxZzu9YDGj6nuGy2EBQwGHwMCEVTFupX55njV4aU3YTG2U+BHFl667NekTtOXH5GXDbp6D+9PntXBxW2d3E68v7lBVMjPKfTsTeCs4aLOwQzsXIFgvouw6GgGsZCrYaQwMNuGayC4xgeIwDQYJKwYBBAGCNxECMQAwEwYJKoZIhvcNAQkVMQYEBAEAAAAwXQYJKoZIhvcNAQkUMVAeTgB0AGUALQA0ADYANwBmADEAOAAxAGMALQBiAGQAZQA0AC0ANAA5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6++jZJJ1P2LxZIi9ZgIJQLIWjmBTMP1nswAzNbnqetOBuJy55+SkO2OsngJAGXIYtW8RBFcTmRYnhCLeIsB/De3khytnaeHNBZVB/x0n/gFqVNMaPZp6l4MPGhEBS8pcvLN4zvO7phxR0Xt3HDFKMBMGCSqGSIb3DQEJFTEGBAQBAAAAMDMGCSqGSIb3DQEJFDEmHiQAVABlAHMAdAAgAEMAbwBkAGUAIABTAGkAZwBuAGkAbgBnAAAwOzAfMAcGBSsOAwIaBBSas13IRWnhNtoPLKp29FJpLmCptgQUkF0JRqyYiDG0Ql7zAPED2uVWzykCAgfQ";
new System.Security.Cryptography.X509Certificates.X509Certificate2(Convert.FromBase64String(base64cert), "qwert");
异常
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Access Denied
at Internal.Cryptography.Pal.CertificatePal.FilterPFXStore(Byte[] rawData, SafePasswordHandle password, PfxCertStoreFlags pfxCertStoreFlags)
at Internal.Cryptography.Pal.CertificatePal.FromBlobOrFile(Byte[] rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)
at PkcsEncryption.Program.Certificate(Boolean rsa) in c:\git\PkcsEncryption\PkcsEncryption\Program.cs:line 88
at PkcsEncryption.Program.Encrypt(Byte[] dataPlain, Boolean useRsa) in c:\git\PkcsEncryption\PkcsEncryption\Program.cs:line 56
at PkcsEncryption.Program.Main(String[] args) in c:\git\PkcsEncryption\PkcsEncryption\Program.cs:line 22
您在那里编码为 base64 的 PFX 已在内部设置标记,表明它是从机器密钥库导出的。您的访问被拒绝表示您不是 运行 管理员(因此无权将密钥添加到计算机的密钥库)。
为确保将来自 PFX 的密钥添加到当前用户的密钥库中,请设置 X509KeyStorageFlags.UserKeySet
标志。或者,如果您已经安装了 .NET Framework v4.7.2 的早期访问版本(或将来发布的版本),您可以使用 EphemeralKeySet
将私钥保存在内存中并完全避免使用密钥库。
new System.Security.Cryptography.X509Certificates.X509Certificate2(
Convert.FromBase64String(base64cert),
"qwert",
X509KeyStorageFlags.UserKeySet);