Bouncy Castle Pgp Key Generation

Posted on by
Bouncy Castle Pgp Key Generation Rating: 3,6/5 4109 votes

Jan 25, 2013 The first post is about generating RSA keys. For better key management, you should generally use separate keys for signing and encryption. This code shows how you can generate a public key that uses two RSA keys for signing and encryption, and how to add signatures and cryptographic preferences. Open PGP Lib based on Bouncy Castle for.NET for file encryption and decryption based on PGP keys. brenoriba/OpenPgpLib. Open PGP Lib based on Bouncy Castle for.NET for file encryption and decryption based on PGP keys. brenoriba/OpenPgpLib. Open PGP Lib for C#.NET based on Bouncy Castle. Keys generated on PGP Key Generator.

Skip to end of metadataGo to start of metadata
  • Using the Bouncy Castle Specific APIs
    • Key Pair Generation
    • Using a KeyFactory
  • Using the JDK APIs
    • Key Pair Generation
    • Using a KeyFactory

Key pair generation in elliptic curve follows the same principles as the other algorithms, the main difference being that, unlike algorithms such as RSA, elliptic curve keys exist only in the context of a particular elliptic curve and require to have curve parameters associated with them to be of any use.

Having said that, there is one anomaly with elliptic curve over other algorithms in that there are two APIs supported by the provider for using them. The reason for this is that JDK elliptic curve support was only introduced with the release of JDK 1.5. Prior to that providers supporting elliptic curve had to include some provider specific classes to allow it to be used, and as Bouncy Castle has supported elliptic curve since release 1.04 it had to provide it's own API.

Other than differences in parameters the generation of elliptic curve keys is identical for both Fp and F2m.

Like other asymmetric algorithms, elliptic curve private keys produce DER encodings of PKCS8 PrivateKeyInfo objects and elliptic curve public keys produce DER encodings of X.509 SubjectPublicKeyInfo objects.

The following example shows a simple case of copying a key pair using the getEncoded() method on the public and private keys and the X509EncodedKeySpec and PKCS8EncodedKeySpec classes.

The Bouncy Castle API for elliptic curve consists of a collection of interfaces and classes defined in org.bouncycastle.jce, org.bouncycastle.jce.interfaces, and org.bouncycastle.jce.spec packages which provide provider specific support for elliptic curve keys, parameters, and named curve handling.

Key Pair Generation

Key pair generation can be done using explicitly created parameters or by retrieving a named curve from a lookup table.

From Explicit Parameters

An org.bouncycastle.jce.ECParameterSpec is required to construct an elliptic curve key. The long way of creating one of these is to create the ECParameterSpec object from a Bouncy Castle ECCurve object and an associated base point and order.

Normally you'd only do this if the curve you want is not already present in one of the named curve tables (see below), but if you had a set of parameters you wanted to use it would look something like this:

As you can see it is a two step process. First you need to create the curve and then you need to associate the curve with a base point and an order using an ECParameterSpec which is then used to initialise the KeyPairGenerator object.

From Named Curves

Named curves are handled in the Bouncy Castle provider by associating a parameter set with a name using an extension of ECParameterSpec, ECNamedCurveParameterSpec, which can be found in org.bouncycastle.jce.spec. Normally you would not create one of these parameter spec objects directly, but you would retrieve it from one of the two lookup tables in org.bouncycastle.jce - ECNamedCurveTable if you are using ECDSA, or ECGOST3410NamedCurveTable if you are using GOST310-2001. How is a bitcoin address private key generated. Both classes support a getNames() method which will tell you what named curves are currently supported.

Assuming you were wanting to use the X9.62 curve prime192v1, the code would look something like this:

Using a KeyFactory

From Explicit Parameters

The Bouncy Castle provider also supports key spec objects for cases where the key material is already available and the use of a KeyPairGenerator is not required. In this case the regular KeyFactory class is used and the Bouncy Castle specific classes ECPublicKeySpec and ECPrivateKeySpec are used to hold the material for the public and private keys respectively.

As you can see the first step is identical to that used for the KeyGenerator, except this time the ECParameterSpec is used to create an ECPrivateKeySpec containing the private value and the parameters, and an ECPublicKeySpec containing the public point and the curve parameters.

These can then be passed to a KeyFactory as follows:

and the resulting keys can then be used as the ones produced by the KeyPairGenerator were.

With Named Curves

As with the key pair generation example, if you know the curve associated with the keys you have been given is for a named curve, you can replace the construction of the ECParmeterSpec above with a named curve lookup using one of the named curve tables from org.bouncycastle.jce.

If you are using JDK 1.5 or later there is local support in the JDK for generation of elliptic curve keys.

Key Pair Generation

With Explicit Parameters

If you're using explicit parameters to generate keys:

With Named Curves

The JDK also supports the use of Named Curves using the ECGenParameterSpec, which simply passes the name of the curve to the provider for interpretation. For example to use the X9.62 curve prime192v1 with the Bouncy Castle provider to generate an Elliptic Curve key pair the code would look something like the following:

Using a KeyFactory

With Explicit Parameters

As can be seen in the following code, the explicit parameters case for JDK 1.5 follows the same steps as for the Bouncy Castle provider as can be seen in the following code:

Bouncy Castle Pgp Decrypt

The one difference of note is the use of the ECPointUtil class to handle an encoded point. The is a Bouncy Castle specific class which can be used to convert point encodings into JDK ECPoint objects. In the case where the point would have been added from its base BigInteger objects the following code could replace the call the ECPointUtil:

With Named Curves

Bouncy Castle Encryption

This case isn't actually directly supported in the JDK. Bouncy Castle does provide a helper class org.bouncycastle.jce.spec.ECNamedCurveSpec which can be used to wrap the return value from the named curve tables provided in org.bouncycastle.jce: