Discussion:
Generate .PFX from .CER and .KEY in C#
Ignacio Ocampo
2013-01-13 19:31:09 UTC
Permalink
Hi, I need to know, how I can generate a .PFX from .CER and .KEY files in
C#, with BouncyCastle libraries.

Can you help me? Thank you.
--
Ignacio Ocampo Millán
Moschny, Torsten
2013-01-13 21:14:12 UTC
Permalink
Hi,

I'm using a code similar to the following snippet:
// create a PKCS12Store
Pkcs12Store tp12 = new Pkcs12Store ();

// get your private key
AsymmetricKeyParameter privateKey = .. // add your private key here

// add certificate chain
X509CertificateEntry[] xe = new X509CertificateEntry[certChain.Count];

for ( int k = 0; k < certChain.Count; k++ )
{
xe[k] = new X509CertificateEntry ( certChain[k] );
}
tp12.SetKeyEntry ( alias, new AsymmetricKeyEntry ( privateKey ), xe );

byte[] res = null;
try
{
MemoryStream s = new MemoryStream ();
tp12.Save ( s, password, GetRandom ( 16 ) );
res = s.GetBuffer ();
res = Pkcs12Utilities.ConvertToDefiniteLength ( res, password );
}

<<<<<

'res' will finally hold the resulting PKCS#12 file. Hope this helps.

Regards,
Torsten
Post by Ignacio Ocampo
Hi, I need to know, how I can generate a .PFX from .CER and .KEY files
in C#, with BouncyCastle libraries.
Can you help me? Thank you.
--
Ignacio Ocampo Millán
Ignacio Ocampo
2013-01-13 22:04:32 UTC
Permalink
Torsten,

How I can load my privateKey?

Regards.
Post by Moschny, Torsten
// get your private key
AsymmetricKeyParameter privateKey = .. // add your private key here
--
Ignacio Ocampo Millán
Moschny, Torsten
2013-01-13 23:00:23 UTC
Permalink
If you have a PEM encoded key, just use the PEMReader, e.g.:
IPasswordFinder pwd = new Password ( password );
PemReader prd = new PemReader ( trd, pwd );

while ( ( o = prd.ReadObject () ) != null )
{
if ( o is AsymmetricKeyParameter )
{
if ( ( (AsymmetricKeyParameter) o ).IsPrivate )
{
if ( (AsymmetricKeyParameter) o is RsaKeyParameters )
{
// store to as RsaPrivateCrtKeyParameters
}
else // an EC key
{
// store as ECPrivateKeyParameters
}
}
}
}

<<<<<


If it's already a binary file, create the AsymmetricKeyParameter from
your key:
AsymmetricKeyParameter privkey = PrivateKeyFactory.CreateKey (
binaryKeyBytes );

<<<<<

This works with RSA and EC keys.

Regards,
Torsten
Post by Ignacio Ocampo
Torsten,
How I can load my privateKey?
Regards.
// get your private key
AsymmetricKeyParameter privateKey = .. // add your private key here
--
Ignacio Ocampo Millán
Ignacio Ocampo
2013-01-14 00:24:48 UTC
Permalink
Torsen,

Thank you.

Best regards.
Post by Moschny, Torsten
IPasswordFinder pwd = new Password ( password );
PemReader prd = new PemReader ( trd, pwd );
while ( ( o = prd.ReadObject () ) != null )
{
if ( o is AsymmetricKeyParameter )
{
if ( ( (AsymmetricKeyParameter) o ).IsPrivate )
{
if ( (AsymmetricKeyParameter) o is RsaKeyParameters )
{
// store to as RsaPrivateCrtKeyParameters
}
else // an EC key
{
// store as ECPrivateKeyParameters
}
}
}
}
<<<<<
If it's already a binary file, create the AsymmetricKeyParameter from your
AsymmetricKeyParameter privkey = PrivateKeyFactory.CreateKey (
binaryKeyBytes );
<<<<<
This works with RSA and EC keys.
Regards,
Torsten
Post by Ignacio Ocampo
Torsten,
How I can load my privateKey?
Regards.
// get your private key
AsymmetricKeyParameter privateKey = .. // add your private key here
--
Ignacio Ocampo Millán
--
Ignacio Ocampo Millán
Loading...