Decrypt and Verify
When we receive a one pass signed and encrypted OpenPGP data we can decrypt it only if we do not wish to verify who is the sender or both decrypt and verify it in one pass. For examples demonstrating the first case when we only want to decrypt the data refer to OpenPGP decryption in .NET. The examples below show how to perform both decryption and verification with DidiSoft OpenPGP Library for .NET:
1) Decrypt and verify file with keys located in files
2) Decrypt and verify file with keys located in a KeyStore
3) Decrypt and verify string message with keys located in files
4) Decrypt and verify string message with keys located in a KeyStore
1) Decrypt and verify file with keys located in files
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#
using System; using DidiSoft; public class DecryptAndVerify { public void Demo() { // create an instance of the library PGPLib pgp = new PGPLib(); bool validSigning = pgp.DecryptAndVerifyFile(@"C:\Test\OUTPUT.pgp", @"C:\Test\private.key", "private key password", @"C:\Test\public.key", @"C:\Test\INPUT.txt"); Console.WriteLine(validSigning); } }
VB.NET
Imports System Imports DidiSoft Public Class DecryptAndVerify Public Sub Demo() ' create an instance of the library Dim pgp As New PGPLib() Dim validSigning As Boolean = _ pgp.DecryptAndVerifyFile("C:\Test\OUTPUT.pgp", _ "C:\Test\private.key", _ "private key password", _ "C:\Test\public.key", _ "C:\Test\INPUT.txt") Console.WriteLine(validSigning) End Sub End Class
2) Decrypt and verify file with keys located in a KeyStore
In this example the public key of the sender is used for verification and our private key is used for decryption and they both are located in a KeyStore file.
C#
using System; using DidiSoft; public class KeyStoreDecryptAndVerifyFile { public static void Demo() { // initialize the KeyStore KeyStore keyStore = new KeyStore(@"c:\key.store", "key store pass"); // create an instance of the library PGPLib pgp = new PGPLib(); // we should provide the private decryption key password too String decryptionKeyPassword = "private key password"; bool validSigning = pgp.DecryptAndVerifyFile(@"c:\INPUT.pgp", keyStore, decryptionKeyPassword, @"c:\OUTPUT.txt"); Console.WriteLine("Signature is valid: "+validSigning); } }
VB.NET
Imports System Imports DidiSoft Public Class KeyStoreDecryptAndVerifyFile Public Shared Sub Demo() ' initialize the KeyStore Dim keyStore As New KeyStore("c:\key.store", "key store pass") ' create an instance of the library Dim pgp As New PGPLib() ' we should provide the private decryption key password too Dim decryptionKeyPassword As String = "private key password" Dim validSigning As Boolean = _ pgp.DecryptAndVerifyFile("c:\OUTPUT.pgp", _ keyStore, _ decryptionKeyPassword, _ "c:\OUTPUT.txt") Console.WriteLine("Signature valid: " & validSigning) End Sub End Class
3) Decrypt and verify string message with keys located in files
If we have an encrypted string message we can decrypt it directly without writing it into a file beforehand.
C#
using System; using System.IO; using DidiSoft; class DecryptAndVerifyString { public void Demo() { // obtain encrypted and signed message String signedAndEncryptedMessage = ... String plainTextExtracted; // create an instance of the library PGPLib pgp = new PGPLib(); // decrypt and verify bool validSigning = pgp.DecryptAndVerifyString( signedAndEncryptedMessage, new FileInfo(@"c:\private.key"), "private key password", new FileInfo(@"c:\public.key"), out plainTextExtracted); // print the results if (validSigning) { Console.WriteLine("Signature is Valid"); } else { Console.WriteLine("Signature is Invalid"); } Console.WriteLine("Extracted message: " + plainTextExtracted); } }
VB.NET
Imports System Imports System.IO Imports DidiSoft Class DecryptAndVerifyString Public Sub Demo() ' obtain encrypted and signed message Dim signedAndEncryptedMessage As String = ... Dim plainTextExtracted As String ' create an instance of the library Dim pgp As New PGPLib() ' decrypt and verify Dim validSigning As Boolean = pgp.DecryptAndVerifyString( _ signedAndEncryptedMessage, _ New FileInfo("c:\private.key"), _ "private key password", _ New FileInfo("c:\public.key"), _ plainTextExtracted) ' print the results If validSigning Then Console.WriteLine("Signature is Valid") Else Console.WriteLine("Signature is Invalid") End If Console.WriteLine("Extracted message: " + plainTextExtracted) End Sub End Class
4) Decrypt and verify string message with keys located in a KeyStore
This example shows to decrypt and verify a signed and encrypted string message with keys stored in a KeyStore file.
C#
using System; using DidiSoft; class KeyStoreDecryptAndVerifyString { public static void Demo() { // obtain an OpenPGP signed and enrypted message String signedAndEncryptedString = ... String privateKeyDecryptionPassword = "private key password"; // Extract the message and check the validity of the signature String plainText; // create an instance of the library PGPLib pgp = new PGPLib(); // initialize the key store KeyStore ks = new KeyStore(@"c:\key.store", "key store password"); bool validSignature = pgp.DecryptAndVerifyString(signedAndEncryptedString, ks, privateKeyDecryptionPassword, out plainText); // Print the results Console.WriteLine("Extracted plain text message is " + plainText); if (validSignature) { Console.WriteLine("Signature is valid"); } else { Console.WriteLine("Signature is invalid"); } } }
VB.NET
Imports System Imports DidiSoft Class KeyStoreDecryptAndVerifyString Public Shared Sub Demo() ' obtain an OpenPGP signed and enrypted message Dim signedAndEncryptedString As String = ... Dim privateKeyDecryptionPassword As String = "private key password" ' Extract the message and check the validity of the signature Dim plainText As String ' create an instance of the library Dim pgp As New PGPLib() ' initialize the key store Dim ks As New KeyStore("c:\key.store", "key store password") Dim validSignature As Boolean = _ pgp.DecryptAndVerifyString(signedAndEncryptedString, _ ks, _ privateKeyDecryptionPassword, _ plainText) ' Print the results Console.WriteLine("Extracted plain text message is " + plainText) If validSignature Then Console.WriteLine("Signature is valid") Else Console.WriteLine("Signature is invalid") End If End Sub End Class