I have a .cnf file that I want to create public and private keys and CSR using this file.
I have a sample code, but it does not fully use all the fields of the .cnf file!
private static void GenerateKeyPair(string commonName, string serialNumber, string organization, string organizationalUnit3, string organizationalUnit2, string organizationalUnit1, string country)
{
CspParameters cspParameters = new CspParameters
{
KeyContainerName = $"{commonName}_{country}_{serialNumber}_KeyContainer",
Flags = CspProviderFlags.UseMachineKeyStore
};
// Create RSACryptoServiceProvider with the specified key container
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048, cspParameters))
{
// Export public key
// Remove XML tags
publicKey = RemoveXmlTags(rsa.ToXmlString(false));
// Export private key
// Remove XML tags
PrivateKey = RemoveXmlTags(rsa.ToXmlString(true));
}
}
Sample .cnf file:
[req]
prompt = no
distinguished_name = dn
[dn]
CN = companyName [Stamp]
serialNumber = Economical number
O = Non-Governmental
3.OU = companyName
2.OU = companyName
1.OU = companyName
C = IR
I expected to be able to create public and private keys, but I couldn't!