Generate DH/DSS OpenPGP key pair
The example below demonstrates how to generate DH/DSS key pair with OpenPGP Library for .NET.
After the generation the private key can be used for decryption and the public key can be send to our partners so they can encrypt data.
Before we generate the key pair we should consider:
- the symmetric algorithms we would like to support
- the hashing algorithms we would like to support
- the compression algorithms we would like to support
- do we want the key to expire on a certain date or should it be valid for ever ( in the example below the key expires after one year )
- the User ID that will be associated with the key. This is usually a composition of name and email for example: “DidiSoft Support <support@didisoft.com>“
Note that the key algorithm is ELGAMAL, in fact ElGamal is an implementation of Diffie Hellman (DH) algorithm.
C#
using System; using DidiSoft; public class GenerateKeyPairDHDSS { public static void Demo() { KeyStore ks = new KeyStore(@"DataFiles\key.store", "changeit"); int keySize = 2048; String keyAlgorithm = KeyAlgorithm.ELGAMAL; String userId = "DH/DSS Key demo@didisoft.com "; String privateKeyPassword = "changeit"; HashAlgorithm[] hashing = { HashAlgorithm.SHA1, HashAlgorithm.SHA256, HashAlgorithm.MD5 }; CompressionAlgorithm[] compression = {CompressionAlgorithm.ZIP, CompressionAlgorithm.ZLIB, CompressionAlgorithm.UNCOMPRESSED}; CypherAlgorithm[] cypher = { CypherAlgorithm.AES_128, CypherAlgorithm.CAST5, CypherAlgorithm.BLOWFISH }; DateTime expirationDate = DateTime.Now.AddYears(1); ks.GenerateKeyPair(keySize, userId, keyAlgorithm, privateKeyPassword, compression, hashing, cypher, expirationDate); } }
VB.NET
Imports System Imports DidiSoft Public Class GenerateKeyPairDHDSS Public Shared Sub Demo() Dim ks As New KeyStore("DataFiles\key.store", "changeit") Dim keySize As Integer = 2048 Dim keyAlgorithm As String = KeyAlgorithm.ELGAMAL Dim userId As String = "DH/DSS Key demo@didisoft.com" Dim privateKeyPassword As String = "changeit" Dim hashing As HashAlgorithm() = {HashAlgorithm.SHA1, _ HashAlgorithm.SHA256, _ HashAlgorithm.MD5} Dim compression As CompressionAlgorithm() = _ {CompressionAlgorithm.ZIP, _ CompressionAlgorithm.ZLIB, _ CompressionAlgorithm.UNCOMPRESSED} Dim cypher As CypherAlgorithm() = {CypherAlgorithm.AES_128, _ CypherAlgorithm.CAST5, _ CypherAlgorithm.BLOWFISH} Dim expirationDate As DateTime = DateTime.Now.AddYears(1) ks.GenerateKeyPair(keySize, _ userId, _ keyAlgorithm, _ privateKeyPassword, _ compression, _ hashing, _ cypher, _ expirationDate) End Sub End Class
After the key pair is generated it can be exported in a standalone file(s). Usually we will send the public key to our partners. Below is a screenshot of the key properties after the key is imported in PGP (r) Desktop version 10.
