OpenPGP signing in .NET
The OpenPGP signing operation converts the input data in OpenPGP packet format and appends a digital signature produced with the private key of the sender. The opposite command for extracting signed content is verify.
Note that OpenPGP signed data is not encrypted. If the data has to be also encrypted see one pass sign and encrypt.
The examples below demonstrate signing with DidiSoft OpenPGP Library for .NET
Signing a file
1. with private key located in a file
2. with private key located in a KeyStore
Sigining a string message
3. with private key located in a file
4. with private key located in a KeyStore
A. Compression and Hash function
1) Signing a file with private key located in a file
C# example
using System; using DidiSoft; public class SignDemo { public void Demo() { // create an instance of the library PGPLib pgp = new PGPLib(); // is output ASCII or binary bool asciiArmor = true; // sign pgp.SignFile(@"C:\INPUT.txt", @"C:\private_key.asc", "private key passphrase", @"C:\OUTPUT.pgp", asciiArmor); } }
VB.NET example
Imports System Imports DidiSoft Public Class SignDemo Public Sub Demo() ' create an instance of the library Dim pgp As New PGPLib() ' is output ASCII or binary Dim asciiArmor As Boolean = True ' sign pgp.SignFile("C:\INPUT.txt", _ "C:\private_key.asc", _ "private key passphrase", _ "C:\OUTPUT.pgp", _ asciiArmor) End Sub End Class
2) Signing a file with private key located in a KeyStore
This example is equivalent to the above one, except that the signing key resides in a KeyStore file. We specify which key to be used for signing by it’s User Id.
C#
using System; using DidiSoft; class KeyStoreSignFile { public static void Demo() { // initialize the key store KeyStore store = new KeyStore(@"c:\key.store", "changeit"); string signingKeyUserId = "support@didisoft.com"; string signingKeyPassword = "changeit"; // if this key store contains a key with this User Id, // then clear sign, // otherwise notify that there is no such key if (store.ContainsKey(signingKeyUserId)) { // create an instance of the library PGPLib pgp = new PGPLib(); // should the output signed file be ASCII or binary bool asciiArmor = true; // clear text sign pgp.SignFile(@"c:\INPUT.txt", store, signingKeyUserId, signingKeyPassword, @"c:\INPUT.sig.txt", asciiArmor); } else { Console.WriteLine("The key was not found!"); } } }
VB.NET example
Imports System Imports DidiSoft Class KeyStoreSignFile Public Shared Sub Demo() ' initialize the key store Dim store As New KeyStore("c:\key.store", "changeit") Dim signingKeyUserId As String = "support@didisoft.com" Dim signingKeyPassword As String = "changeit" ' if this key store contains a key with this User Id, 'then clear sign, ' otherwise notify that there is no such key If store.ContainsKey(signingKeyUserId) Then ' create an instance of the library Dim pgp As New PGPLib() ' should the output signed file be ASCII or binary Dim asciiArmor As Boolean = True ' clear text sign pgp.SignFile("c:\INPUT.txt", store, _ signingKeyUserId, _ signingKeyPassword, _ "c:\INPUT.sig.txt", _ asciiArmor) Else Console.WriteLine("The key was not found!") End If End Sub End Class
3) Signing a string message with private key located in a file
If we wish to sign a string message directly we should use one of the SignString methods:
C#
using System; using System.IO; using DidiSoft; class SignString { public static String Demo() { // message to be signed String plainString = "Hello World"; // create an instance of the library PGPLib pgp = new PGPLib(); // sign String signedString = pgp.SignString(plainString, new FileInfo(@"c:\private_key.asc"), "private key password"); return signedString; } }
VB.NET example
Imports System Imports System.IO Imports DidiSoft Class SignString Public Shared Function Demo() As String ' message to be signed Dim plainString As String = "Hello World" ' create an instance of the library Dim pgp As New PGPLib() ' sign Dim signedString As String = _ pgp.SignString(plainString, _ New FileInfo("DataFiles\private_key.asc"), _ "private key password") Return signedString End Function End Class
4) Signing a string message with private key located in a KeyStore
If we keep our keys in a KeyStore object we should use the overloaded version of the SignString method that accepts KeyStore in order to sign a String message:
C# example
using System; using DidiSoft; class KeyStoreSignString { public static String Demo() { string signingKeyUserId = "support@didisoft.com"; string signingKeyPassword = "changeit"; // initialize the key store KeyStore ks = new KeyStore(@"DataFiles\key.store", "keystore password"); // if this key store contains the desired key - sign, // otherwise notify that there is no such key if (ks.ContainsKey(signingKeyUserId)) { PGPLib pgp = new PGPLib(); string plainText = "Hello World"; string signedString = pgp.SignString(plainText, ks, signingKeyUserId, signingKeyPassword); return signedString; } else { Console.WriteLine("No key with user Id:" + signingKeyUserId + " was found in this key store."); return null; } } }
VB.NET example
Imports System Imports DidiSoft Class KeyStoreSignString Public Sub Demo() Dim signingKeyUserId As String = "support@didisoft.com" Dim signingKeyPassword As String = "changeit" ' initialize the key store Dim ks As New KeyStore("DataFiles\key.store", "changeit") ' if this key store contains a key with this recipient userId ' then sign, otherwise notify that there is no such key If ks.ContainsKey(signingKeyUserId) Then Dim pgp As New PGPLib() Dim plainText As String = "Hello World" Dim signedString As String = pgp.SignString(plainText, _ ks, _ signingKeyUserId, _ signingKeyPassword) Else Console.WriteLine("No key with user Id:" + _ signingKeyUserId + _ " was found in this key store.") End If End Sub End Class
Compression and signature Hash function
The default compression of the signed file can be changed through the Compression property.
The default signature hash function can be changed through the Hash property.