A .NET Core class library for using PGP.
This is based on ChoPGP but updated to .NET Standard and to add in a missing utilities class.
To use PgpCore in your C# project download it from NuGet.
Once you have the PgpCore libraries properly referenced in your project, you can include calls to them in your code.
Add the following namespaces to use the library:
using PgpCore;
- Portable.BouncyCastle (>= 1.9.0)
This is intended for usage in projects targeting .NET Standard 2.0.
If you want a (basic) example of how you can use an Azure Function to encrypt/decrypt from Azure Blob Storage I've created a sample project here.
Generate a new public and private key for the provided username and password.
using (PGP pgp = new PGP())
{
// Generate keys
pgp.GenerateKey(@"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "[email protected]", "password");
}
Encrypt the provided file, stream or string using a public key.
gpg --output "C:\TEMP\Content\encrypted.pgp" --encrypt "C:\TEMP\Content\content.txt"
// Load keys
FileInfo publicKey = new FileInfo(@"C:\TEMP\Keys\public.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey);
// Reference input/output files
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\content.txt");
FileInfo encryptedFile = new FileInfo(@"C:\TEMP\Content\encrypted.pgp");
// Encrypt
PGP pgp = new PGP(encryptionKeys);
await pgp.EncryptFileAsync(inputFile, encryptedFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(publicKeyStream);
PGP pgp = new PGP(encryptionKeys);
// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\encrypted.pgp"))
// Encrypt
await pgp.EncryptStreamAsync(inputFileStream, outputFileStream);
// Load keys
string publicKey = File.ReadAllText(@"C:\TEMP\Keys\public.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey);
// Encrypt
PGP pgp = new PGP(encryptionKeys);
string encryptedContent = await pgp.EncryptArmoredStringAsync("String to encrypt");
Sign the provided file or stream using a private key.
gpg --output "C:\TEMP\Content\content.txt" --sign "C:\TEMP\Content\signed.pgp"
// Load keys
FileInfo privateKey = new FileInfo(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(privateKey, "password");
// Reference input/output files
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\content.txt");
FileInfo signedFile = new FileInfo(@"C:\TEMP\Content\signed.pgp");
// Sign
PGP pgp = new PGP(encryptionKeys);
await pgp.SignFileAsync(inputFile, signedFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(privateKeyStream, "password");
PGP pgp = new PGP(encryptionKeys);
// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\signed.pgp"))
// Sign
await pgp.SignStreamAsync(inputFileStream, outputFileStream);
// Load keys
string privateKey = File.ReadAllText(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(privateKey, "password");
PGP pgp = new PGP(encryptionKeys);
// Sign
string signedContent = await pgp.SignArmoredStringAsync("String to sign");
Clear sign the provided file, stream, or string using a private key so that it is still human readable.
gpg --output "C:\TEMP\Content\content.txt" --clearsign "C:\TEMP\Content\clearSigned.pgp"
// Load keys
FileInfo privateKey = new FileInfo(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(privateKey, "password");
// Reference input/output files
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\content.txt");
FileInfo signedFile = new FileInfo(@"C:\TEMP\Content\signed.pgp");
// Sign
PGP pgp = new PGP(encryptionKeys);
await pgp.ClearSignFileAsync(inputFile, signedFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(privateKeyStream, "password");
PGP pgp = new PGP(encryptionKeys);
// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\signed.pgp"))
// Sign
await pgp.ClearSignStreamAsync(inputFileStream, outputFileStream);
// Load keys
string privateKey = File.ReadAllText(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(privateKey, "password");
PGP pgp = new PGP(encryptionKeys);
// Sign
string signedContent = await pgp.ClearSignArmoredStringAsync("String to sign");
Encrypt the provided file, stream or string using a public key and sign using your private key. You usually encrypt with the public key of your counterparty so they can decrypt with their private key and sign with your private key so they can verify with your public key.
gpg --encrypt --sign --recipient 'some user ID value' "C:\TEMP\keys\content.txt"
// Load keys
FileInfo publicKey = new FileInfo(@"C:\TEMP\Keys\public.asc");
FileInfo privateKey = new FileInfo(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey, privateKey, "password");
// Reference input/output files
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\content.txt");
FileInfo encryptedSignedFile = new FileInfo(@"C:\TEMP\Content\encryptedSigned.pgp");
// Encrypt and Sign
PGP pgp = new PGP(encryptionKeys);
await pgp.EncryptFileAndSignAsync(inputFile, encryptedSignedFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(publicKeyStream, privateKeyStream, "password");
PGP pgp = new PGP(encryptionKeys);
// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\signed.pgp"))
// Encrypt and Sign
await pgp.EncryptStreamAndSignAsync(inputFileStream, outputFileStream);
// Load keys
string publicKey = File.ReadAllText(@"C:\TEMP\Keys\public.asc");
string privateKey = File.ReadAllText(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey, privateKey, "password");
PGP pgp = new PGP(encryptionKeys);
// Encrypt and Sign
string encryptedSignedContent = await pgp.EncryptArmoredStringAndSignAsync("String to encrypt and sign");
Decrypt the provided file, stream or string using the matching private key and passphrase.
gpg --output "C:\TEMP\Content\decrypted.txt" --decrypt "C:\TEMP\Content\encrypted.pgp"
// Load keys
FileInfo privateKey = new FileInfo(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(privateKey, "password");
// Reference input/output files
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\encryptedContent.pgp");
FileInfo decryptedFile = new FileInfo(@"C:\TEMP\Content\decrypted.txt");
// Decrypt
PGP pgp = new PGP(encryptionKeys);
await pgp.DecryptFileAsync(inputFile, decryptedFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(privateKeyStream, "password");
PGP pgp = new PGP(encryptionKeys);
// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encryptedContent.pgp", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\decrypted.txt"))
// Decrypt
await pgp.DecryptStreamAsync(inputFileStream, outputFileStream);
// Load keys
string privateKey = File.ReadAllText(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(privateKey, "password");
PGP pgp = new PGP(encryptionKeys);
// Decrypt
string decryptedContent = await pgp.DecryptArmoredStringAsync("String to decrypt");
Verify that the file, stream or string was signed by the matching private key of the counterparty.
gpg --verify "C:\TEMP\Content\signed.pgp"
// Load keys
FileInfo publicKey = new FileInfo(@"C:\TEMP\Keys\public.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey);
// Reference input
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\signedContent.pgp");
// Verify
PGP pgp = new PGP(encryptionKeys);
bool verified = await pgp.VerifyFileAsync(inputFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(publicKeyStream);
PGP pgp = new PGP(encryptionKeys);
// Reference input file
bool verified;
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encryptedContent.pgp", FileMode.Open))
// Verify
verified = await pgp.VerifyStreamAsync(inputFileStream);
// Load keys
string publicKey = File.ReadAllText(@"C:\TEMP\Keys\public.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey);
PGP pgp = new PGP(encryptionKeys);
// Verify
bool verified = await pgp.VerifyArmoredStringAsync("String to verify");
Verify that the clear signed file or stream was signed by the matching private key of the counterparty.
gpg --verify "C:\TEMP\Content\clearSigned.pgp"
// Load keys
FileInfo publicKey = new FileInfo(@"C:\TEMP\Keys\public.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey);
// Reference input
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\signedContent.pgp");
// Verify
PGP pgp = new PGP(encryptionKeys);
bool verified = await pgp.VerifyClearFileAsync(inputFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(publicKeyStream);
PGP pgp = new PGP(encryptionKeys);
// Reference input file
bool verified;
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encryptedContent.pgp", FileMode.Open))
// Verify
verified = await pgp.VerifyClearStreamAsync(inputFileStream);
// Load keys
string publicKey = File.ReadAllText(@"C:\TEMP\Keys\public.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey);
PGP pgp = new PGP(encryptionKeys);
// Verify
bool verified = await pgp.VerifyClearArmoredStringAsync("String to verify");
Decrypt and then verify the provided encrypted and signed file, stream or string. Usually your counterparty will encrypt with your public key and sign with their private key so you can decrypt with your private key and verify with their public key.
// Load keys
FileInfo publicKey = new FileInfo(@"C:\TEMP\Keys\public.asc");
FileInfo privateKey = new FileInfo(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey, privateKey, "password");
// Reference input/output files
FileInfo inputFile = new FileInfo(@"C:\TEMP\Content\encryptedSigned.pgp");
FileInfo encryptedSignedFile = new FileInfo(@"C:\TEMP\Content\content.txt");
// Decrypt and Verify
PGP pgp = new PGP(encryptionKeys);
await pgp.DecryptFileAndVerifyAsync(inputFile, encryptedSignedFile);
// Load keys
EncryptionKeys encryptionKeys;
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
encryptionKeys = new EncryptionKeys(publicKeyStream, privateKeyStream, "password");
PGP pgp = new PGP(encryptionKeys);
// Reference input/output files
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encryptedSigned.pgp", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\content.txtp"))
// Decrypt and Verify
await pgp.DecryptStreamAndVerifyAsync(inputFileStream, outputFileStream);
// Load keys
string publicKey = File.ReadAllText(@"C:\TEMP\Keys\public.asc");
string privateKey = File.ReadAllText(@"C:\TEMP\Keys\private.asc");
EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey, privateKey, "password");
PGP pgp = new PGP(encryptionKeys);
// Decrypt and Verify
string encryptedSignedContent = await pgp.DecryptArmoredStringAndVerifyAsync("String to decrypt and verify");