1

在 tmpAEC4.pfx 文件中,我在此消息的底部有表示证书集的内容。当我尝试实例化 X509Certificate2 时:

var data = File.ReadAllBytes(@"C:\Temp\tmpAEC4.pfx");
new X509Certificate2(data, "9G8U922PWY");

我收到以下异常

(System.Security.Cryptography.CryptographicException: 'An error occurred during encode or decode operation.):

密码是正确的,就像我手动导入pfx一样,它可以工作。任何想法?

证书:

MIILwgIBAzCCC3wGCSqGSIb3DQEHAaCCC20EggtpMIILZTCCBXIGCSqGSIb3DQEH
AaCCBWMEggVfMIIFWzCCBVcGCyqGSIb3DQEMCgECoIIE+jCCBPYwKAYKKoZIhvcN
AQwBAzAaBBRYH0IH869hSttnQkGSPYO8MxzqxAICBAAEggTIImUZkkLMC/XfOwba
SgTzP9XiKuW8Y+yaQ4lM3vQe6+6ibJSKIKg3mwrtUu8A1sYmf90++M+hWMechZPp
1JVPtTOVmah7HMFdpYZ/Ot2OUx2KaOutgPkw5kTzy9QAsC35cf6BI0cnrgI3DIkn
7ub5zPmzaNPa/nUd2tH4kBj+em4+HfKoL59Vu0DCowzLt9KMQlAzgPaMNOtoWP/H
j2y7km29sJlziaSTI/olihdWlL0jW7B5vF5kPfUEXGEriyi/uRqWMbxtL4cLM7A6
77NXHRIwZ5cK2exZG5MePO6AT9VLan/C7r0n0B9Jae3D7mD71UK0bb/pT/BBLSwz
qmGPXPtpmYcAnW/8BK7QerHTasKJ7YR0V0T7oXG3+sYiMJAZFKf2wSx7fm2ZKMTX
2V05KI00SPayQ1dPM0I1rg0glRNNWKWN37LeWOTfjxqm2fyjUmgBvFkoTR2Ltw1E
Jtd80oxmcMuiBfAO9iHGngTgpxLxppUdA+YRq+pUtOGnztCsu0HZmrwFkP5F87Ca
Jy3FwotK98cZMxSsVtPE/YRPYI+4VsysaGynytATLhy3MlAnKRttYKD+92IqBNAx
S8mP9e6MyiqzUOoIwWjQhvWqkzt4V1KGekjrhigUEVDCM290cTvVHFCZQWBFXT0I
tIKfYjc40oB1rD0v6HEjpivYC9Oa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1 回答 1

7

问题是您正在以二进制形式读取 Base64 文本文件。您需要阅读文本,然后将 Base64 文本转换为字节数组:

String base64 = File.ReadAllText(@"C:\Temp\tmpAEC4.pfx");
Byte[] data = Convert.FromBase64String(base64);
var cert = new X509Certificate2(data, "9G8U922PWY");

File类属于System.IO命名空间;

于 2018-06-25T06:59:43.460 回答