Generate RSA key pair
The example below demonstrates how to generate RSA key pair with DidiSoft OpenPGP Library for .NET. The generated key pair has no expiration date.
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.
C#
using System; using DidiSoft; public class GenerateKeyPairRSA { public void Test ( // initialize the key store where the generated key // will be produced, if the file does not exist // it will be created KeyStore ks = new KeyStore(@"c:\key.store", "key store password"); String userId = "RSA Demo demo@didisoft.com"; // Preferred symmetric key algorithms for this key CypherAlgorithm[] cypher = { CypherAlgorithm.CAST5, CypherAlgorithm.AES_128 }; // Preferred digital signature algorithms for this key HashAlgorithm[] hashing = { HashAlgorithm.SHA1, HashAlgorithm.MD5, HashAlgorithm.SHA256 }; // Preferred compression algorithms for this key CompressionAlgorithm[] compression = { CompressionAlgorithm.ZIP, CompressionAlgorithm.UNCOMPRESSED}; ks.GenerateKeyPair(2048, userId, KeyAlgorithm.RSA, "private key password", compression, hashing, cypher); // Now we can use the key from the // KeyStore object, or export it // to a standalone file ) }
VB.NET
Imports System Imports DidiSoft Public Class GenerateKeyPairRSA Public Sub Test() ' initialize the key store where the generated key ' will be produced, if the file does not exist ' it will be created Dim ks As New KeyStore("c:\key.store", _ "key store password") Dim userId As String = "RSA Demo demo@didisoft.com" ' Preferred symmetric key algorithms for this key Dim cypher As CypherAlgorithm() = {CypherAlgorithm.CAST5, _ CypherAlgorithm.AES_128} ' Preferred digital signature algorithms for this key Dim hashing As HashAlgorithm() = {HashAlgorithm.SHA1, _ HashAlgorithm.MD5, _ HashAlgorithm.SHA256} ' Preferred compression algorithms for this key Dim compression As CompressionAlgorithm() = _ {CompressionAlgorithm.ZIP, _ CompressionAlgorithm.UNCOMPRESSED} ks.GenerateKeyPair(2048, userId, _ KeyAlgorithm.RSA, _ "private key password", _ compression, _ hashing, _ cypher) ' Now we can use the key from the ' KeyStore object, or export it ' to a standalone file End Sub End Class
After the key pair is generated it can be exported in a standalone file (or the public and secret key in two files). 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.
