OpenPGP Library for .NET Examples
Examples using keys stored in Key store
Generate keys
Export keys
Import keys
Delete key pair
Encrypt file
Decrypt file
Examples with keys located in files
Encrypt file
Decrypt file
Sign file
Verify file
Clear sign file
Sign and Encrypt file
Decrypt and Verify file
Encrypt file
This sample shows how to encrypt a file with public key stored in a Key store
C#
using System; using DidiSoft; public class KeyStoreEncryptFile { public static void Demo() { KeyStore keyStore = new KeyStore(@"DataFiles\key.store", "changeit"); PGPLib pgp = new PGPLib(); String publicKeyUserId = "support@didisoft.com"; bool asciiArmor = true; bool integrityCheck = true; pgp.EncryptFile(@"DataFiles\INPUT.txt", keyStore, publicKeyUserId, @"DataFiles\OUTPUT.pgp", asciiArmor, integrityCheck); } }
VB.NET
Imports System Imports DidiSoft Public Class KeyStoreEncryptFile Public Shared Sub Demo() Dim keyStore As New KeyStore("DataFiles\key.store", "changeit") Dim pgp As New PGPLib() Dim publicKeyUserId As String = "support@didisoft.com" Dim asciiArmor As Boolean = True Dim integrityCheck As Boolean = True pgp.EncryptFile("DataFiles\INPUT.txt", _ keyStore, _ publicKeyUserId, _ "DataFiles\OUTPUT.pgp", _ asciiArmor, _ integrityCheck) End Sub End Class
Decrypt File
This sample shows how to decrypt a file with private key stored in a Key store
C#
using System; using DidiSoft; public class KeyStoreDecryptFile { public static void Demo() { KeyStore keyStore = new KeyStore(@"DataFiles\key.store", "changeit"); PGPLib pgp = new PGPLib(); String privateKeyPassword = "changeit"; pgp.DecryptFile(@"DataFiles\OUTPUT.pgp", keyStore, privateKeyPassword, @"DataFiles\OUTPUT.txt"); } }
VB.NET
Imports System Imports DidiSoft Public Class KeyStoreDecryptFile Public Shared Sub Demo() Dim keyStore As New KeyStore("DataFiles\key.store", "changeit") Dim pgp As New PGPLib() Dim privateKeyPassword As String = "changeit" pgp.DecryptFile("DataFiles\OUTPUT.pgp", _ keyStore, _ privateKeyPassword, _ "DataFiles\OUTPUT.txt") End Sub End Class
Encrypt file
This example demonstrates OpenPGP file encryption, providing public key stored directly in a file.
C# Source Code
using System.IO; using DidiSoft; class EncryptDemo { public void Demo() { PGPLib pgp = new PGPLib(); bool armor = true; bool withIntegrityCheck = false; pgp.EncryptFile(@"C:\Test\INPUT.txt", @"C:\Test\public.key", @"C:\Test\OUTPUT.pgp", armor, withIntegrityCheck); } }
VB.NET Source Code
Imports System.IO Imports DidiSoft Class EncryptDemo Public Sub Demo() Dim pgp As New PGPLib() Dim armor As Boolean = True Dim withIntegrityCheck As Boolean = False pgp.EncryptFile("C:\Test\INPUT.txt", "C:\Test\public.key", _ "C:\Test\OUTPUT.pgp", _ armor, _ withIntegrityCheck) End Sub End Class
Decrypt file
This example demonstrates OpenPGP file decryption, providing private key stored directly in a file.
The Private key password is needed, in order the key to be used.
C# Source Code
using System; using DidiSoft; public class DecryptDemo { public void Demo() { PGPLib pgp = new PGPLib(); pgp.DecryptFile(@"C:\Test\OUTPUT.pgp", @"C:\Test\private.key", "changeit", @"C:\Test\INPUT.txt"); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class DecryptDemo Public Sub Demo() Dim pgp As New PGPLib() pgp.DecryptFile("C:\Test\OUTPUT.pgp", _ "C:\Test\private.key", _ "changeit", _ "C:\Test\INPUT.txt") End Sub End Class
This example demonstrates OpenPGP signing.
C# Source Code
using System; using DidiSoft; public class SignDemo { public void Demo() { PGPLib pgp = new PGPLib(); bool armor = true; pgp.SignFile(@"C:\Test\INPUT.txt", @"C:\Test\private.key", "changeit", @"C:\Test\OUTPUT1.signed", armor); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class SignDemo Public Sub Demo() Dim pgp As New PGPLib() Dim asciiArmor As Boolean = True pgp.SignFile("C:\Test\INPUT.txt", _ "C:\Test\private.key", _ "changeit", _ "C:\Test\OUTPUT1.signed", _ asciiArmor) End Sub End Class
This example demonstrates OpenPGP clear signing a file using private key stored directly in a file. Clear signing differs from signing that the signature is appended after the data, so the other party can read the file even if they do not use any OpenPGP software.
C# Source Code
using System; using DidiSoft; class ClearSignFile { public static void Demo() { PGPLib pgp = new PGPLib(); pgp.ClearSignFile(@"DataFiles\INPUT.txt", @"DataFiles\private.key", "changeit", "SHA1", @"DataFiles\OUTPUT.sig.txt"); } }
VB.NET Source Code
Imports System Imports DidiSoft Class ClearSignFile Public Shared Sub Demo() Dim pgp As New PGPLib() pgp.ClearSignFile("DataFiles\INPUT.txt", _ "DataFiles\private.key", _ "changeit", _ "SHA256", _ "DataFiles\OUTPUT.sig.txt") End Sub End Class
This example demonstrates OpenPGP verification.
C# Source Code
using System; using DidiSoft; public class VerifyDemo { public void Demo() { PGPLib pgp = new PGPLib(); bool validSignature = pgp.VerifyFile(@"C:\Test\OUTPUT1.signed", @"C:\Test\public.key"); Console.WriteLine("Valid Signature: " + validSignature); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class VerifyDemo Public Sub Demo() Dim pgp As New PGPLib() Dim validSignature As Boolean = pgp.VerifyFile("C:\Test\OUTPUT1.signed", _ "C:\Test\public.key") Console.WriteLine("Valid Signature: " + validSignature) End Sub End Class
Sign and Encrypt file
This example demonstrates One Pass signing and encryption.
C# Source Code
using System; using DidiSoft; public class SignAndEncrypt { public void Demo() { PGPLib pgp = new PGPLib(); bool asciiArmor = false; bool withIntegrityCheck = false; pgp.SignAndEncryptFile(@"C:\Test\INPUT.txt", @"C:\Test\private.key", "changeit", @"C:\Test\public.key", @"C:\Test\OUTPUT.pgp", asciiArmor, withIntegrityCheck); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class SignAndEncrypt Public Sub Demo() Dim pgp As New PGPLib() Dim asciiArmor As Boolean = False Dim withIntegrityCheck As Boolean = False pgp.SignAndEncryptFile("C:\Test\INPUT.txt", _ "C:\Test\private.key", _ "changeit", _ "C:\Test\public.key", _ "C:\Test\OUTPUT.pgp", _ asciiArmor, _ withIntegrityCheck) End Sub End Class
Decrypt and Verify file
This example demonstrates OpenPGP decryption and verification of one pass signed and encrypted file.
Having the public key of the other party we decrypt and verify that it is the source of this signed file.
C# Source Code
using System; using DidiSoft; public class DecryptAndVerify { public void Demo() { PGPLib pgp = new PGPLib(); bool validSigning = pgp.DecryptAndVerifyFile(@"C:\Test\OUTPUT.pgp", @"C:\Test\private.key", "changeit", @"C:\Test\public.key", @"C:\Test\INPUT.txt"); Console.WriteLine(validSigning); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class DecryptAndVerify Public Sub Demo() Dim pgp As New PGPLib() Dim validSigning As Boolean = pgp.DecryptAndVerifyFile("C:\Test\OUTPUT.pgp", _ "C:\Test\private.key", _ "changeit", _ "C:\Test\public.key", _ "C:\Test\INPUT.txt") Console.WriteLine(validSigning) End Sub End Class
Key pair generation
This example demonstrates OpenPGP Key pair generation. Key pair is stored in the backing file of the KeyStore object, and can be exported with the Export methods.
C# Source Code
using System; using DidiSoft; public class GenerateKeyPair { public void Test() { KeyStore ks = new KeyStore(@"C:\Test\key.store", "changeit"); ks.GenerateKeyPair(1024, "support@didisoft.com", "changeit"); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class GenerateKeyPair Public Sub Test() Dim ks As New KeyStore("C:\Test\key.store", "changeit") ks.GenerateKeyPair(1024, "support@didisoft.com", "changeit") End Sub End Class
Export public and private key
Keys with the specified userId parameter must be generated or imported before executing Export methods.
C# Source Code
using System; using DidiSoft; public class ExportKeys { public void Demo() { KeyStore ks = new KeyStore(@"C:\Test\key.store", "changeit"); bool asciiArmored = true; ks.ExportPrivateKey(@"C:\Test\private_key_exported.pgpkey", "support@didisoft.com", asciiArmored); ks.ExportPublicKey(@"C:\Test\public_key_exported.pgpkey", "support@didisoft.com", asciiArmored); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class ExportKeys Public Sub Demo() Dim ks As New KeyStore("C:\Test\key.store", "changeit") Dim asciiArmored As Boolean = True ks.ExportPrivateKey("C:\Test\private_key_exported.pgpkey", _ "support@didisoft.com", _ asciiArmored) ks.ExportPublicKey("C:\Test\public_key_exported.pgpkey", _ "support@didisoft.com", _ asciiArmored) End Sub End Class
Import public and private keys
This sample shows how to import keys into a Key store.
C# Code
using System; using DidiSoft; public class ImportKeys { public static void Demo() { KeyStore keyStore = new KeyStore("pgp.keystore", "changeit"); keyStore.ImportPublicKey("public.pkr"); String privateKeyPassword = "changeit"; keyStore.ImportPrivateKey("private.skr", privateKeyPassword); } }
VB.NET Code
Imports System Imports DidiSoft Public Class ImportKeys Public Shared Sub Demo() Dim keyStore As New KeyStore("pgp.keystore", "changeit") keyStore.ImportPublicKey("public.key") String privateKeyPassword = "changeit" keyStore.ImportPrivateKey("private.key", privateKeyPassword) End Sub End Class
Delete key pair
This sample shows how to delete key pair from a Key store instance.
C# Source Code
using System; using DidiSoft; public class DeleteKeyPair { public static void Demo() { KeyStore ks = new KeyStore(@"DataFiles\key.store", "changeit"); ks.DeleteKeyPair("demo2@support.com"); } }
VB.NET Source Code
Imports System Imports DidiSoft Public Class DeleteKeyPair Public Shared Sub Demo() Dim ks As New KeyStore("DataFiles\key.store", "changeit") ks.DeleteKeyPair("demo2@didisoft.com") End Sub End Class