Generate the OpenPGP key
OpehnPGP standard supports several types of keys. In our days, a typical OpenPGP keypair actually consists of two cryptographic keys - a primary key and a subkey. The primary key is normally used for signing, while the subkey is used for encryption. While it is typical for PGP environments to use a primary key/subkey bundle, this is not a must. Sometimes you may come across standalone keys (primarily, when interacting with older implementations), as well as whole key trees with a bunch of differently-purposed subkeys bound to the same primary key.
Algorithm-wise, OpenPGP keys also differ. Generally speaking, OpenPGP supports the following public key algorithms: RSA, Elgamal (often incorrectly referred to as DH), DSA, ECDH and ECDSA. When it comes to primary key/subkey bundles, DSA/Elgamal, RSA/RSA and ECDSA/ECDH pairs are typically used (however, again, there's no restriction on algorithm bundles, i.e. a ECDSA/Elgamal key bundle is perfectly possible - yet rarely used de facto).
A typical OpenPGP key is associated with some kind of user ID. This is normally represented with a user's e-mail address, while can in theory be assigned with any textual line. The secret part of the OpenPGP keypair is protected with a password.
You can generate the key synchronously or in asynchronous mode. Asynchronous mode lets you generate the key in background (secondary thread is spawned for generation).
To generate your very own PGP key, please follow the below procedure.
int primaryKeyAlg = SBPGPConstants.Unit.SB_PGP_ALGORITHM_PK_DSA; int primaryKeyBits = 1024; int subKeyAlg = SBPGPConstants.Unit.SB_PGP_ALGORITHM_PK_ELGAMAL_ENCRYPT; int subKeyBits = 1024;
string passphrase = "1Robot";
string userID = "Luke Skywalker <email@example.com>";
int expires = 365; // one year long
key.Generate(passphrase, primaryKeyBits, primaryKeyAlg, subKeyBits, subKeyAlg, username, expires );
After the key is generated, you can add this key to the keyring or save to a separate file.
keyring.AddSecretKey(key); keyring.Save("pubring.pkr", "secring.skr");Note that while you are only adding the secret key to the keyring, the public key is implicitly added too as it is a part of the secret key. To access the public key of the generated keypair, use PublicKey property of TElPGPSecretKey class.
To generate a legacy standalone key (which is RSA-only), a simplified Generate() method can be used:
key.Generate(passphrase, bits, SBPGPConstants.Unit.SB_PGP_ALGORITHM_PK_RSA, username, false, expires );