8

I am receiving a string and want to convert that into a certificate using C#. I tried following code and got "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. " error:

 byte[] bytes = Convert.FromBase64String(((string[])request.Headers.GetValues("MY-Cert"))[0]);
                var cert = new X509Certificate2(bytes);

What is the best way to read such string using C#.

Similar question was asked here but using C++

I am receiving string in following format:

-----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-----
SharpCoder
  • 18,279
  • 43
  • 153
  • 249
  • Are you removing the "-----BEGIN CERTIFICATE-----" prefix (and the matching suffix)? – John Wu Dec 17 '20 at 23:48
  • Yes, remove the first and last lines and it will work fine. You may also want to remove the cert from your post, because now we can decode it quite easily too... – DavidG Dec 17 '20 at 23:50
  • @DavidG No, the post needs the certificate to make sense to other readers of the post. SharpCode just needs to generate a different certificate for in-production use. – Andrew Shepherd Dec 18 '20 at 00:31
  • This is public certificate. It doesn't contain any sensitive information, so what is your concern, @DavidG, @AndrewShepherd? – Crypt32 Dec 18 '20 at 06:37
  • @Crypt32 I didn't try to decode it, so had no idea if it was public or not. – DavidG Dec 18 '20 at 10:47
  • There is no need to decode. PEM header is self-explanatory, that it is public certificate without private key. – Crypt32 Dec 18 '20 at 10:51
  • 1
    @Crypt32 That might be something you have in your mental box of tricks, but us mere mortals don't... – DavidG Dec 18 '20 at 10:55

2 Answers2

11

Following code worked for me;

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);
              
SharpCoder
  • 18,279
  • 43
  • 153
  • 249
6

As it was pointed in comments, you need to remove first and last lines from PEM string and then convert Base64 string to X509Certificate2 object.

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));
Crypt32
  • 12,850
  • 2
  • 41
  • 70