Create .pem file for public key RSA encryption C# .net
Asked Answered
P

2

7

I want to create .pem file for the public key generated by this method

public static Tuple<string, string> CreateKeyPair()
{
    CspParameters cspParams = 
        new CspParameters { 
            ProviderType = 1 /* PROV_RSA_FULL */ 
        };

    RSACryptoServiceProvider rsaProvider = 
        new RSACryptoServiceProvider(1024, cspParams);

    string publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
    string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));

    return new Tuple<string, string>(privateKey, publicKey);
}

Because I was generating this key for mobile application and they can not read it, they requested .pem file instead of public key as string

Please advice,

Pe answered 10/10, 2013 at 11:22 Comment(0)
W
10

First off, a so-called .pem file is not really a fixed specification or format. Several different kinds of distinct file formats are generally described as "PEM" files. When the SSLeay (now OpenSSL) project needed to produce a base64 encoded output file containing key information they borrowed formatting concepts from the old Privacy-Enhanced Mail RFCs 1421-1424 and they added the extension .pem to the end of these files. But such a file may contain public keys, private keys, certificate requests, certificates, certificate lists, and so on. Each is different. So if all you're told is to produce a .pem file you're going to have to guess what's really needed.

The easiest way to write such files is to use the Bouncycastle C# library. The package Org.BouncyCastle.OpenSsl contains a number of utilities including a PemWriter class that should help you.

Woolfolk answered 10/10, 2013 at 13:22 Comment(3)
Don't use Org.BouncyCastle.OpenSsl, because it has no examples and very bad documentation for C#! I worked under the same problem, and this library really helps me: https://github.com/jrnker/CSharp-easy-RSA-PEM. It has good examples and you can solve your problem using only one line (instead of implement some interfaces from BouncyCastle)!Schapira
@V.Panchenko: Thanks for your comment. Please consider writing an answer to this question with the one-liner as an example.Woolfolk
CSharp-easy-RSA-PEM is GPL3 licensed, so you can't use it unless your project is GPL too. BouncyCastle on the other hand is MITIbnsaud
S
12

Recently I need to save PublicKey and PrivateKey generated in my C# application to file, and works with it later. I use for this purpose such library as CSharp-easy-RSA-PEM.

It is very simple and quick solution, so I will recommend this library to other guys.

I use following code to get PublicKey as string (and save it to pem file in format Base64):

string publicKeyStr = Crypto.ExportPublicKeyToX509PEM(_cryptoServiceProvider);

it returns something like this:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxnBvS8cdsnAev2sRDRYWxznm1
QxZzaypfNXLvK7CDGk8TR7K+Pzsa+tpJfoyN/Z4B6xdlpsERo2Cu6AzolvrDLx5w
ZoI0kgdfaBMbUkdOB1m97zFYjKWoPeTskFzWZ3GHcQ3EXT0NJXXFXAskY45vEpbc
5qFgEhcPy3BMqHRibwIDAQAB
-----END PUBLIC KEY-----

And I use following code to get PrivateKey as string:

string privateKeyStr = Crypto.ExportPrivateKeyToRSAPEM(_cryptoServiceProvider);

it returns something like this:

-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCxnBvS8cdsnAev2sRDRYWxznm1QxZzaypfNXLvK7CDGk8TR7K+
Pzsa+tpJfoyN/Z4B6xdlpsERo2Cu6AzolvrDLx5wZoI0kgdfaBMbUkdOB1m97zFY
jKWoPeTskFzWZ3GHcQ3EXT0NJXXFXAskY45vEpbc5qFgEhcPy3BMqHRibwIDAQAB
AoGAAdwpqm7fxh0S3jOYpJULeQ45gL11dGX7Pp4CWHYzq1vQ14SDtFxYfnLWwGLz
499zvSoSHP1pvjPgz6lxy9Rw8dUxCgvh8VQydMQzaug2XD1tkmtcSWInwFKBAfQ7
rceleyD0aK8JHJiuzM1p+yIJ/ImGK0Zk2U/svqrdJrNR4EkCQQDo3d5iWcjd3OLD
38k1GALEuN17KNpJqLvJcIEJl0pcHtOiNnyy2MR/XUghDpuxwhrhudB/TvX4tuI0
MUeVo5fjAkEAw0D6m9jkwE5uuEYN/l/84rbQ79p2I7r5Sk6zbMyBOvgl6CDlJyxY
434DDm6XW7c55ALrnlratEW5HPiPxuHZBQJANnE4vtGy7nvn4Fd/mRQmAYwe695f
On1iefP9lxpx3huu6uvGN6IKPqS2alQZ/nMdCc0Be+IgC6fmNsGWtNtsdQJAJvB4
ikgxJqD9t8ZQ2CAwgM5Q0OTSlsGdIdKcOeB3DVmbxbV5vdw8RfJFjcVEbkgWRYDH
mKcp4rXc+wgfNFyqOQJATZ1I5ER8AZAn5JMMH9zK+6oFvhLUgKyWO18W+dbcFrBd
AzlTB+HHYEIyTmaDtXWAwgBvJNIHk4BbM1meCH4QnA==
-----END RSA PRIVATE KEY-----

Then you can use

RSACryptoServiceProvider publicX509key = Crypto.DecodeX509PublicKey(publicKeyStr);
RSACryptoServiceProvider privateRSAkey = Crypto.DecodeRsaPrivateKey(privateKeyStr);

to restore saved keys back to RSACryptoServiceProvider.

So, if someone need to resolve similar issue, you can just download this library, go to Solution Explorer -> (Right click on your project) -> Add -> Reference -> Overview in your Visual Studio to add this library in your project, and add using CSharp_easy_RSA_PEM; where you need it :)

Schapira answered 9/6, 2017 at 14:44 Comment(2)
Looks like a nice solution.Woolfolk
I'd give it an upvote if it had a liberal license, but alas, it is GPL.Timmi
W
10

First off, a so-called .pem file is not really a fixed specification or format. Several different kinds of distinct file formats are generally described as "PEM" files. When the SSLeay (now OpenSSL) project needed to produce a base64 encoded output file containing key information they borrowed formatting concepts from the old Privacy-Enhanced Mail RFCs 1421-1424 and they added the extension .pem to the end of these files. But such a file may contain public keys, private keys, certificate requests, certificates, certificate lists, and so on. Each is different. So if all you're told is to produce a .pem file you're going to have to guess what's really needed.

The easiest way to write such files is to use the Bouncycastle C# library. The package Org.BouncyCastle.OpenSsl contains a number of utilities including a PemWriter class that should help you.

Woolfolk answered 10/10, 2013 at 13:22 Comment(3)
Don't use Org.BouncyCastle.OpenSsl, because it has no examples and very bad documentation for C#! I worked under the same problem, and this library really helps me: https://github.com/jrnker/CSharp-easy-RSA-PEM. It has good examples and you can solve your problem using only one line (instead of implement some interfaces from BouncyCastle)!Schapira
@V.Panchenko: Thanks for your comment. Please consider writing an answer to this question with the one-liner as an example.Woolfolk
CSharp-easy-RSA-PEM is GPL3 licensed, so you can't use it unless your project is GPL too. BouncyCastle on the other hand is MITIbnsaud

© 2022 - 2024 — McMap. All rights reserved.