C#将证书字符串转换为X509证书
C# convert certificate string into X509 certificate
我收到一个字符串并想使用 C# 将其转换为证书。我尝试了以下代码并得到了“The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
”错误:
byte[] bytes = Convert.FromBase64String(((string[])request.Headers.GetValues("MY-Cert"))[0]);
var cert = new X509Certificate2(bytes);
使用 C# 读取此类字符串的最佳方法是什么。
有人问过类似的问题 here 但使用的是 C++
我收到以下格式的字符串:
-----BEGIN CERTIFICATE-----
MIIExDCCA6ygAwIBAgIJAK0JmDc/YXWsMA0GCSqGSIb3DQEBBQUAMIGcMQswCQYD
VQQGEwJJTjELMAkGA1UECBMCQVAxDDAKBgNVBAcTA0hZRDEZMBcGA1UEChMQUm9j
a3dlbGwgY29sbGluczEcMBoGA1UECxMTSW5kaWEgRGVzaWduIENlbnRlcjEOMAwG
A1UEAxMFSU1BQ1MxKTAnBgkqhkiG9w0BCQEWGmJyYWphbkBSb2Nrd2VsbGNvbGxp
bnMuY29tMB4XDTExMDYxNjE0MTQyM1oXDTEyMDYxNTE0MTQyM1owgZwxCzAJBgNV
BAYTAklOMQswCQYDVQQIEwJBUDEMMAoGA1UEBxMDSFlEMRkwFwYDVQQKExBSb2Nr
d2VsbCBjb2xsaW5zMRwwGgYDVQQLExNJbmRpYSBEZXNpZ24gQ2VudGVyMQ4wDAYD
VQQDEwVJTUFDUzEpMCcGCSqGSIb3DQEJARYaYnJhamFuQFJvY2t3ZWxsY29sbGlu
cy5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDfjHgUAsbXQFkF
hqv8OTHSzuj+8SKGh49wth3UcH9Nk/YOug7ZvI+tnOcrCZdeG2Ot8Y19Wusf59Y7
q61jSbDWt+7u7P0ylWWcQfCE9IHSiJIaKAklMu2qGB8bFSPqDyVJuWSwcSXEb9C2
xJsabfgJr6mpfWjCOKd58wFprf0RF58pWHyBqBOiZ2U20PKhq8gPJo/pEpcnXTY0
x8bw8LZ3SrrIQZ5WntFKdB7McFKG9yFfEhUamTKOffQ2Y+SDEGVDj3eshF6+Fxgj
8plyg3tZPRLSHh5DR42HTc/35LA52BvjRMWYzrs4nf67gf652pgHh0tFMNMTMgZD
rpTkyts9AgMBAAGjggEFMIIBATAdBgNVHQ4EFgQUG0cLBjouoJPM8dQzKUQCZYNY
y8AwgdEGA1UdIwSByTCBxoAUG0cLBjouoJPM8dQzKUQCZYNYy8ChgaKkgZ8wgZwx
CzAJBgNVBAYTAklOMQswCQYDVQQIEwJBUDEMMAoGA1UEBxMDSFlEMRkwFwYDVQQK
ExBSb2Nrd2VsbCBjb2xsaW5zMRwwGgYDVQQLExNJbmRpYSBEZXNpZ24gQ2VudGVy
MQ4wDAYDVQQDEwVJTUFDUzEpMCcGCSqGSIb3DQEJARYaYnJhamFuQFJvY2t3ZWxs
Y29sbGlucy5jb22CCQCtCZg3P2F1rDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEB
BQUAA4IBAQCyYZxEzn7203no9TdhtKDWOFRwzYvY2kZppQ/EpzF+pzh8LdBOebr+
DLRXNh2NIFaEVV0brpQTI4eh6b5j7QyF2UmA6+44zmku9LzS9DQVKGLhIleB436K
ARoWRqxlEK7TF3TauQfaalGH88ZWoDjqqEP/5oWeQ6pr/RChkCHkBSgq6FfGGSLd
ktgFcF0S9U7Ybii/MD+tWMImK8EE3GGgs876yqX/DDhyfW8DfnNZyl35VF/80j/s
0Lj3F7Po1zsaRbQlhOK5rzRVQA2qnsa4IcQBuYqBWiB6XojPgu9PpRSL7ure7sj6
gRQT0OIU5vXzsmhjqKoZ+dBlh1FpSOX2
-----END CERTIFICATE-----
这是您需要转换为 PKCS7 的 PEM 格式。
有关格式的更多信息,请点击此处。
https://comodosslstore.com/resources/a-ssl-certificate-file-extension-explanation-pem-pkcs7-der-and-pkcs12/
使用 OpenSSL 转换为正确的格式。
openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cer
certificate.cer 将是您当前保存在 certificate.cer 文件中的 PEM 文本,假设您从 CA 或自行生成的 CA 获得此文件,您将需要提供 CACert.cer 进行签名输出。
正如评论中指出的那样,您需要从 PEM 字符串中删除第一行和最后一行,然后将 Base64 字符串转换为 X509Certificate2
对象。
String certPemString = ((string[])request.Headers.GetValues("MY-Cert"))[0];
certPemString = certPemString
.Replace("-----BEGIN CERTIFICATE-----",null)
.Replace("-----END CERTIFICATE-----",null);
var cert = new X509Certificate2(Convert.FromBase64String(certPemString));
以下代码对我有用;
IEnumerable<string> headerValues;
var usercert = request.Headers.TryGetValues("MY-Cert", out headerValues);
var data = WebUtility.UrlDecode(((string[])headerValues)[0]);
byte[] bytes = Encoding.ASCII.GetBytes(data);
var cert = new X509Certificate2(bytes);
我收到一个字符串并想使用 C# 将其转换为证书。我尝试了以下代码并得到了“The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
”错误:
byte[] bytes = Convert.FromBase64String(((string[])request.Headers.GetValues("MY-Cert"))[0]);
var cert = new X509Certificate2(bytes);
使用 C# 读取此类字符串的最佳方法是什么。
有人问过类似的问题 here 但使用的是 C++
我收到以下格式的字符串:
-----BEGIN CERTIFICATE-----
MIIExDCCA6ygAwIBAgIJAK0JmDc/YXWsMA0GCSqGSIb3DQEBBQUAMIGcMQswCQYD
VQQGEwJJTjELMAkGA1UECBMCQVAxDDAKBgNVBAcTA0hZRDEZMBcGA1UEChMQUm9j
a3dlbGwgY29sbGluczEcMBoGA1UECxMTSW5kaWEgRGVzaWduIENlbnRlcjEOMAwG
A1UEAxMFSU1BQ1MxKTAnBgkqhkiG9w0BCQEWGmJyYWphbkBSb2Nrd2VsbGNvbGxp
bnMuY29tMB4XDTExMDYxNjE0MTQyM1oXDTEyMDYxNTE0MTQyM1owgZwxCzAJBgNV
BAYTAklOMQswCQYDVQQIEwJBUDEMMAoGA1UEBxMDSFlEMRkwFwYDVQQKExBSb2Nr
d2VsbCBjb2xsaW5zMRwwGgYDVQQLEx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BSgq6FfGGSLd
ktgFcF0S9U7Ybii/MD+tWMImK8EE3GGgs876yqX/DDhyfW8DfnNZyl35VF/80j/s
0Lj3F7Po1zsaRbQlhOK5rzRVQA2qnsa4IcQBuYqBWiB6XojPgu9PpRSL7ure7sj6
gRQT0OIU5vXzsmhjqKoZ+dBlh1FpSOX2
-----END CERTIFICATE-----
这是您需要转换为 PKCS7 的 PEM 格式。
有关格式的更多信息,请点击此处。 https://comodosslstore.com/resources/a-ssl-certificate-file-extension-explanation-pem-pkcs7-der-and-pkcs12/
使用 OpenSSL 转换为正确的格式。
openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cer
certificate.cer 将是您当前保存在 certificate.cer 文件中的 PEM 文本,假设您从 CA 或自行生成的 CA 获得此文件,您将需要提供 CACert.cer 进行签名输出。
正如评论中指出的那样,您需要从 PEM 字符串中删除第一行和最后一行,然后将 Base64 字符串转换为 X509Certificate2
对象。
String certPemString = ((string[])request.Headers.GetValues("MY-Cert"))[0];
certPemString = certPemString
.Replace("-----BEGIN CERTIFICATE-----",null)
.Replace("-----END CERTIFICATE-----",null);
var cert = new X509Certificate2(Convert.FromBase64String(certPemString));
以下代码对我有用;
IEnumerable<string> headerValues;
var usercert = request.Headers.TryGetValues("MY-Cert", out headerValues);
var data = WebUtility.UrlDecode(((string[])headerValues)[0]);
byte[] bytes = Encoding.ASCII.GetBytes(data);
var cert = new X509Certificate2(bytes);