Repository navigation
Expand file tree
/
Copy pathCreatePemFile.cs
More file actions
57 lines (48 loc) · 1.91 KB
/
Copy pathCreatePemFile.cs
File metadata and controls
57 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System.IO;
namespace CreatePemFiles
{
class Program
{
const string publicKeyFileLocation = @"C:\Users\x\Documents\PublicKey.pem";
const string privateKeyFileLocation = @"C:\Users\x\Documents\PrivateKey.pem";
static void Main(string[] args)
{
//rSAKeyPairGenerator generates the RSA key pair based on the random number and strength of the key required
RsaKeyPairGenerator rSAKeyPair = new RsaKeyPairGenerator();
SecureRandom secureRandom = new SecureRandom();
secureRandom.SetSeed(secureRandom.GenerateSeed(1000));
rSAKeyPair.Init(new KeyGenerationParameters(secureRandom, 2048));
AsymmetricCipherKeyPair keyPair = rSAKeyPair.GenerateKeyPair();
//Extract private/public key from the pair
RsaKeyParameters privateKey = keyPair.Private as RsaKeyParameters;
RsaKeyParameters publicKey = keyPair.Public as RsaKeyParameters;
//print public & private key in pem format
CreatePem(publicKey);
CreatePem(privateKey);
}
static void CreatePem(RsaKeyParameters key)
{
TextWriter textWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(textWriter);
string printKey;
string fileLocation;
pemWriter.WriteObject(key);
pemWriter.Writer.Flush();
printKey = textWriter.ToString();
if (key.IsPrivate)
{
fileLocation = privateKeyFileLocation;
}
else
{
fileLocation = publicKeyFileLocation;
}
File.WriteAllText(fileLocation, printKey);
}
}
}