Quantcast

Pkcs8Generator EC private key format

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Pkcs8Generator EC private key format

sesteve
Hi,

I am trying to create one EC key pair, I am executing this code:

            ECDomainParameters ecDomainParams = TlsEccUtilities.GetParametersForNamedCurve(NamedCurve.secp384r1);
            ECKeyPairGenerator kpg = new ECKeyPairGenerator("ECDSA");
            kpg.Init(new KeyGenerationParameters(new SecureRandom(), 384));
            AsymmetricCipherKeyPair keyPair = kpg.GenerateKeyPair();

            // Creating PEM files
            StreamWriter sw = new StreamWriter(Path.Combine(Settings.Default.DataDir, "dev_cli.key"));
            PemWriter pp = new PemWriter(sw);
            Pkcs8Generator pkcs8 = new Pkcs8Generator((ECPrivateKeyParameters)keyPair.Private);
            pp.WriteObject(pkcs8.Generate());
            pp.Writer.Close();
            sw.Close();


It is created a file with the content:


-----BEGIN PRIVATE KEY-----
MFcCAQAwEAYHKoZIzj0CAQYFK4EEACIEQDA+AgEBBDCCrDnoivfrYJ0XMdPOP9G8
N+oir0WJDZyyZToQC7Bljv2yX1RFNN8V5K4nA2fSOZSgBwYFK4EEACI=
-----END PRIVATE KEY-----


I have troubles when I try to read this content in other application. Moreover, I must execute this command from OpenSsl overwriting the file:


C:\OpenSSL-Win64\bin>openssl ec -text -in c:\Projects\TLS-ECC\SecurityKeyTool\SecurityKeyTool\bin\Debug\dev_cli.key
read EC key
Private-Key: (384 bit)
priv:
    ...
pub:
    ...
ASN1 OID: secp384r1
writing EC key
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDCCrDnoivfrYJ0XMdPOP9G8N+oir0WJDZyyZToQC7Bljv2yX1RFNN8V
5K4nA2fSOZSgBwYFK4EEACKhZANiAAQ3n6oR47TNgwKcZj+7gcAx4b4wNoXS2N5t
92diFEZq4keLXVaicyOrnHL4rwVY+CL36ABctRJZ7Wc8Hk5o97+bLIl53jnMGDsJ
q9JlcEbbg/+jfcOvKfd0bmaVrckeN/I=
-----END EC PRIVATE KEY-----


Then, If I use this other content between "EC PRIVATE KEY" as my key, I have no trouble with it.
The key itself doesn't seem to be modified, because when I get the pub and priv keys from both contents, I get the same keys.

Could someone explain me the difference between both contents of the key file, please? How can I get the second format of the key directly with BouncyCastle, please?

Thanks!
Loading...