Contact Us Forum Customers Area

DidiSoft Ltd.

OpenPGP sign and encrypt in .NET

One pass signed and encrypted data is both protected and the origin of the message can be verified. The public key of the receiver is used to encrypt the message and the private (signing) key of the sender is used for creating the digital signature.

The examples below show how to sign and encrypt with DidiSoft OpenPGP Library for .NET

1) Sign and encrypt file with keys located in files
2) Sign and encrypt file with keys located in a KeyStore

3) Sign and encrypt string with keys located in files
4) Sign and encrypt string with keys located in a KeyStore

1) Sign and encrypt file with keys located in files

This example shows how to sign and encrypt a file when our OpenPGP keys are stored in files on the file system:

C#

using System;
using DidiSoft;
 
public class SignAndEncrypt
{
  public void Demo()
  {
    // create an instance of the library
    PGPLib pgp = new PGPLib();
    // is output ASCII or binary
    bool asciiArmor = false;
    // should integrity check information be added
    bool withIntegrityCheck = false;
    // sign and encrypt
    pgp.SignAndEncryptFile(@"C:\Test\INPUT.txt",
                           @"C:\Test\private.key",
                           "changeit",
                           @"C:\Test\public.key",
                           @"C:\Test\OUTPUT.pgp",
                           asciiArmor,
                           withIntegrityCheck);
    }
}

VB.NET

Imports System
Imports DidiSoft
 
Public Class SignAndEncrypt
  Public Sub Demo()
    ' create an instance of the library
    Dim pgp As New PGPLib()
    ' is output ASCII or binary
    Dim asciiArmor As Boolean = False
    ' should integrity check information be added
    Dim withIntegrityCheck As Boolean = False
    ' sign and encrypt
    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

2) Sign and encrypt file with keys located in a KeyStore

In this example we sign and encrypt a file with OpenPGP keys stored in KeyStore object. We know either the User Id of the key owner or the key Id prior calling the SignAndEncryptFile method (For more KeyStore examples see the KeyTool project in the Trial or Production version download package).

C#

using System;
using DidiSoft;
 
public class KeyStoreSignAndEncryptFile
{
  public static void Demo()
  {
    // initialize the Key store
    KeyStore keyStore = new KeyStore(@"c:\key.store",
					"keystore pass");
    // create an instance of the library
    PGPLib pgp = new PGPLib();
 
    // is output ASCII or binary
    bool asciiArmor = true;
    // should integrity check information be added
    bool withIntegrityCheck = true;
 
    String signUserId = "support@didisoft.com";
    String signKeyPassword = "changeit";
    String encUserId = "rsa_demo@didisoft.com";
    // sign and encrypt
    pgp.SignAndEncryptFile(@"DataFiles\INPUT.txt",
			keyStore,
			signUserId,
			signKeyPassword,
			encUserId,
			@"DataFiles\OUTPUT.pgp",
			asciiArmor,
			withIntegrityCheck);
  }
}

VB.NET

Imports System
Imports DidiSoft
 
Public Class KeyStoreSignAndEncryptFile
 Public Shared Sub Demo()
   ' initialize the key store
   Dim keyStore As New KeyStore("DataFiles\key.store", _
				"keystore password")
   ' create an instance of the library
   Dim pgp As New PGPLib()
 
   ' is output ASCII or binary
   Dim asciiArmor As Boolean = True
   ' should integrity check information be added
   Dim withIntegrityCheck As Boolean = True
 
   Dim signUserId As String = "support@didisoft.com"
   Dim signKeyPassword As String = "changeit"
   Dim encUserId As String = "rsa_demo@didisoft.com"
 
   pgp.SignAndEncryptFile("DataFiles\INPUT.txt", _
			 keyStore, _
			 signUserId, _
			 signKeyPassword, _
			 encUserId, _
			"DataFiles\OUTPUT.pgp", _
                         asciiArmor, _
                         withIntegrityCheck)
 End Sub
End Class

3) Sign and encrypt string with keys located in files

If we wish to sign and encrypt in one pass a string message we should use the SignAndEncryptString method:

C#

using System;
using System.IO;
using DidiSoft;
 
class SignAndEncryptString
{
 public static String Demo()
 {
   String plainText = "Hello World";
 
   // create an instance of the library
   PGPLib pgp = new PGPLib();
 
   // sign and enrypt
   String encryptedAndSignedString =
        pgp.SignAndEncryptString(plainText,
			   new FileInfo(@"c:\private.key"),
			   "private key password",
			   new FileInfo(@"c:\public.key"));
 
   return encryptedAndSignedString;
 }
}

VB.NET

Imports System
Imports System.IO
Imports DidiSoft
 
Class SignAndEncryptString
 Public Shared Function Demo() As String
 
   Dim plainText As String = "Hello World"
 
   ' create an instance of the library
   Dim pgp As New PGPLib()
   ' sign and enrypt
   Dim encryptedAndSignedString As String = _
             pgp.SignAndEncryptString(plainText, _
                            New FileInfo("c:\private.key"), _
			    "private key password", _
			    New FileInfo("c:\public.key"))
 
   Return encryptedAndSignedString
 End Function
End Class

4) Sign and encrypt string with keys located in a KeyStore

If our keys are located in a KeyStore object and we want to sign and encrypt a string message we should use one of the overloaded versions of SignAndEncryptString:

C#

using System;
using DidiSoft;
 
class KeyStoreSignAndEncryptString
{
 public static String Demo()
 {
   // our private key User Id
   string signingKeyUserId = "support@didisoft.com";
   string signingKeyPassword = "changeit";
 
   // recipient public key User Id
   string encryptionKeyUserId = "rsa_demo@didisoft.com";
 
   // initialize the key store
   KeyStore ks = new KeyStore(@"DataFiles\key.store", "changeit");
 
   // if this key store contains a key with the desired recipient
   // userId then sign, otherwise notify that there is no such key
   if (ks.ContainsKey(encryptionKeyUserId))
   {
     PGPLib pgp = new PGPLib();
 
     string plainText = "Hello World";
 
     string signedString =
           pgp.SignAndEncryptString(plainText, ks,
				 signingKeyUserId,
				 signingKeyPassword,
				 encryptionKeyUserId);
 
     return signedString;
   }
   else
   {
     Console.WriteLine("No key with user Id:" +
		          encryptionKeyUserId +
			  " was found in this key store.");
     return String.Empty;
   }
 }
}

VB.NET

Imports System
Imports DidiSoft
 
Class KeyStoreSignAndEncryptString
 Public Shared Function Demo() As String
  ' our private key User Id
  Dim signingKeyUserId As String = "support@didisoft.com"
  Dim signingKeyPassword As String = "changeit"
 
  ' recipient public key User Id
  Dim encryptionKeyUserId As String = "rsa_demo@didisoft.com"
 
  ' initialize the key store
  Dim ks As New KeyStore("DataFiles\key.store", "changeit")
 
  ' if this key store contains a key with the desired recipient
  ' userId then sign, otherwise notify that there is no such key
  If ks.ContainsKey(encryptionKeyUserId) Then
    Dim pgp As New PGPLib()
 
    Dim plainText As String = "Hello World"
 
    Dim signedString As String = _
    pgp.SignAndEncryptString(plainText, _
		             ks, _
		             signingKeyUserId, _
		             signingKeyPassword, _
		             encryptionKeyUserId)
 
    Return signedString
   Else
    Console.WriteLine("No key with user Id:" + _
	                  encryptionKeyUserId + _
	                  " was found in this key store.")
    Return String.Empty
   End If
 End Function
End Class