huysentruitw / pem-utils

Managed .NET (C#) utility library for working with PEM files with DER/ASN.1 encoding

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The format PRIVATE KEY is not yet implemented

jl2035 opened this issue · comments

I would like to load a pem file into C# and I can't find the proper tool. I was hoping pem-utils is exactly what I need, but I'm getting the following error:

Unhandled Exception: System.NotImplementedException: The format PRIVATE KEY is not yet implemented

I didn't know there are other formats besides "PRIVATE KEY". Is any other format 'implemented' in this library?

This is how my code looks like:

        RSA rsa = RSA.Create();
        string file = "/home/user/certs/ca5.pem";
        using (var stream = File.OpenRead(file))
        using (var reader = new PemReader(stream))
        {
            var rsaParameters = reader.ReadRsaKey();
            rsa.ImportParameters(rsaParameters);
        }
        Console.WriteLine("RSA: " + rsa.KeySize);

It crashes at ReadRsaKey() line.

Any help would be great.

The only private key format currently supported is RSA PRIVATE KEY (which is the most common one). What type of key are you dealing with?

If possible, can you generate a similar private key that you can share as a test key here?

Many thanks to you, sir! I used the Keystore Explorer tool to generate private key, and for some reason this tool generates the first line as PRIVATE KEY and not RSA PRIVATE KEY. If I generate the key using openssl it all works fine.

Hi, now I'm getting the following error:

Unhandled Exception: System.InvalidOperationException: Data on the stream doesn't match the required PEM format

What does that mean?

It means your private key does not adhere to this regular expression: https://github.com/huysentruitw/pem-utils/blob/master/src/PemUtils/PemReader.cs#L56

Can you please post an example private key?

Sorry, I've also figured this out. I had both keys in the file. After removing public key from the file (and with correct format "BEGIN RSA PRIVATE KEY") it worked.