Cleartext signing
Introduction to OpenPGP clearsigning
The clear text signed OpenPGP format is designed for text data. The digital signature is appended after the original message; this way the recipient can still read it without special software. The examples below show how to clear sign with OpenPGP Library for .NET
Verification and extraction of clear signed data is done through the verification methods, just like for ordinal signed data
List of examples
Clear sign text file with private key located in a file
Clear sign text file with private key located in a KeyStore
Clear sign string message with private key located in a file
Clear sign string message with private key located in a KeyStore
Clear sign text file with private key located in a file
C# example
using System.Text; using DidiSoft; class ClearSignFile { public static void Demo() { // create an instance of the library PGPLib pgp = new PGPLib(); // clear text sign pgp.ClearSignFile(@"DataFiles\INPUT.txt", @"DataFiles\private.key", "changeit", "SHA256", @"DataFiles\OUTPUT.sig.txt"); } }
VB.NET example
Imports System Imports DidiSoft Class ClearSignFile Public Shared Sub Demo() ' create an instance of the library Dim pgp As New PGPLib() ' clear text sign pgp.ClearSignFile("DataFiles\INPUT.txt", _ "DataFiles\private.key", _ "changeit", _ "SHA256", _ "DataFiles\OUTPUT.sig.txt") End Sub End Class
Clear sign text file with private key located in a KeyStore
In this example the signing key is located in a KeyStore file. The key is specified through it’s User ID, but can be specified also with its Key ID.
C# example
using System; using DidiSoft; class KeyStoreClearSignFile { public static void Demo() { // initialize the key store KeyStore ks = new KeyStore(@"DataFiles\key.store", "changeit"); string signingKeyUserId = "support@didisoft.com"; string signingKeyPassword = "changeit"; // if this key store contains a key with // the desired recipient userId, then clear sign, // otherwise notify that there is no such key if (ks.ContainsKey(signingKeyUserId)) { // create an instance of the library PGPLib pgp = new PGPLib(); // clear text sign pgp.ClearSignFile(@"DataFiles\INPUT.txt", ks, signingKeyUserId, signingKeyPassword, HashAlgorithm.SHA1, @"DataFiles\OUTPUT.sig.txt"); } else { Console.WriteLine("No key with user Id:" + signingKeyUserId + " was found in this key store."); } } }
VB.NET example
Imports System Imports DidiSoft Class KeyStoreClearSignFile Public Shared Sub Demo() ' initialize the key store Dim ks As New KeyStore("DataFiles\key.store", "changeit") Dim signingKeyUserId As String = "support@didisoft.com" Dim signingKeyPassword As String = "changeit" ' if this key store contains a key with ' the desired recipient userId, then clear sign, ' otherwise notify that there is no such key If ks.ContainsKey(signingKeyUserId) Then ' create an instance of the library Dim pgp As New PGPLib() ' clear text sign pgp.ClearSignFile("DataFiles\INPUT.txt", ks, _ signingKeyUserId, _ signingKeyPassword, _ HashAlgorithm.SHA1, _ "DataFiles\OUTPUT.sig.txt") Else Console.WriteLine("No key with user Id:" + signingKeyUserId + " was found in this key store.") End If End Sub End Class
Clear sign string message with private key located in a file
C# example
using System; using System.IO; using DidiSoft; class ClearSignString { public static String Demo() { String plainString = "Hello World"; PGPLib pgp = new PGPLib(); String clearSignedString = pgp.ClearSignString(plainString, new FileInfo(@"DataFiles\private.key"), "changeit", HashAlgorithm.SHA1); return clearSignedString; } }
VB.NET example
Imports System Imports System.IO Imports DidiSoft Class ClearSignString Public Shared Function Demo() As String Dim plainString As String = "Hello World" ' create an instance of the library Dim pgp As New PGPLib() ' clear text sign Dim clearSignedString As String = _ pgp.ClearSignString(plainString, _ New FileInfo("DataFiles\private.key"), _ "changeit", _ HashAlgorithm.SHA1) Return clearSignedString End Function End Class
Clear sign string message with private key located in a KeyStore
C# example
using System; using DidiSoft; class KeyStoreClearSignString { public static String Demo() { // initialize the key store KeyStore ks = new KeyStore(@"DataFiles\key.store", "changeit"); string signingKeyUserId = "support@didisoft.com"; string signingKeyPassword = "changeit"; // if this key store contains a key with the desired recipient userId - clear sign, // otherwise notify that there is no such key if (ks.ContainsKey(signingKeyUserId)) { // create an instance of the library PGPLib pgp = new PGPLib(); string plainText = "Hello World"; // clear text sign string clearSignedString = pgp.ClearSignString( plainText, ks, signingKeyUserId, signingKeyPassword, HashAlgorithm.SHA1); return clearSignedString; } 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 KeyStoreClearSignString Public Shared Function Demo() As String 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 the desired recipient userId - clear 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 clearSignedString As String = _ pgp.ClearSignString(plainText, ks, _ signingKeyUserId, _ signingKeyPassword, _ HashAlgorithm.SHA1) Return clearSignedString Else Console.WriteLine("No key with user Id:" + _ signingKeyUserId + _ " was found in this key store.") Return Nothing End If End Function End Class
Summary
This chapter presented OpenPGP clear text signing in C# and VB.NET. The class PGPLib that provides those methods is located in the namespace DidiSoft.Pgp. The methods that perform clear signing are:
PGPLib.ClearSignFile
PGPLib.ClearSignString
PGPLib.ClearSignStream