144
Views
3
Comments
[CryptoAPI] RSA private key to PEM
Question
cryptoapi
Service icon
Forge asset by João Barata

Hi Guru's

I see that there is a privatekey from PEM but there is no privatekey to PEM. Would that not be great to have that added to this component? I understand that it's possible to convert the XML output of the RSA_NewKey online but would be nice to do that automatically.

Kind regards!

UserImage.jpg
freek

Would this maybe work?

https://stackoverflow.com/questions/27086117/convert-rsacryptoserviceprovider-rsa-xml-key-to-pkcs8


public static void Convert(string privXmlFilename, string privPkcs8Filename) {
            StringBuilder sb = new StringBuilder ();
            string line;
            var xmlIn = new StreamReader (privXmlFilename);
            while ((line = xmlIn.ReadLine ()) != null) {
                sb.Append (line);
            }
            var xmlKey = sb.ToString ();
            var rsa = RSA.Create ();
            rsa.FromXmlString (xmlKey);
            var bcKeyPair = DotNetUtilities.GetRsaKeyPair(rsa);
            var pkcs8Gen = new Pkcs8Generator (bcKeyPair.Private);
            var pemObj = pkcs8Gen.Generate ();
            var pkcs8Out = new StreamWriter (privPkcs8Filename, false);
            var pemWriter = new PemWriter (pkcs8Out);
            pemWriter.WriteObject (pemObj);
            pkcs8Out.Close ();
        }
UserImage.jpg
Leon Budde

Hi, 

Im facing the same issue. I have a privatekey that is not in PEM format (it starts with -----BEGIN PRIVATE KEY-----) and that format does not work in the RSA_PrivateKeyFromPEM action.  When i decrypt the key using this openssl command:

openssl rsa -in privatekey.key -out privatkey.pem -outform PEM

The result is a PEM key that begins with this "-----BEGIN RSA PRIVATE KEY-----" and that is accepted by the RSA_PrivateKeyFromPEM action. 

Is there an action in CryptoAPI I can use to replace this openssl command or does anyone know another forge component that does this? 

regards, Leon

2023-04-13 07-10-08
Geertjan Jacobs

Good day,

I will add another kind request for this feature. We're working on a JWT implementation (via JWT component) that requires the keypair to be in a specific PEM format (PKCS#5/PKCS#8). 

It would be nice if besides converting PEM to XML, it's also possible to translate back from XML to PEM. We hope @João Barata finds the time to implement this action too.

Thanks in advance!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.