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, you can either download the PgpCore C# .NET libraries directly from the Github repository or, if you have the NuGet package manager installed, you can grab them automatically.
PM> Install-Package PgpCore
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.8.6.7)
This is intended for usage in projects targeting .NET Standard 2.0.
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@email.com", "password");
}
Encrypt the provided file or stream using a public key.
gpg --output "C:\TEMP\Content\encrypted.pgp" --encrypt "C:\TEMP\Content\content.txt"
using (PGP pgp = new PGP())
{
// Encrypt file
pgp.EncryptFile(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\encrypted.pgp", @"C:\TEMP\Keys\public.asc", true, true);
}
using (PGP pgp = new PGP())
{
// Encrypt file
await pgp.EncryptFileAsync(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\encrypted.pgp", @"C:\TEMP\Keys\public.asc", true, true);
}
using (PGP pgp = new PGP())
{
// Encrypt stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\encrypted.pgp"))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
pgp.EncryptStream(inputFileStream, outputFileStream, publicKeyStream, true, true);
}
using (PGP pgp = new PGP())
{
// Encrypt stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\encrypted.pgp"))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
await pgp.EncryptStreamAsync(inputFileStream, outputFileStream, publicKeyStream, true, true);
}
Sign the provided file or stream using a private key.
gpg --output "C:\TEMP\Content\content.txt" --sign "C:\TEMP\Content\signed.pgp"
using (PGP pgp = new PGP())
{
// Sign file
pgp.SignFile(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\private.asc", "password", true, true);
}
using (PGP pgp = new PGP())
{
// Sign file
await pgp.SignFileAsync(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\private.asc", "password", true, true);
}
using (PGP pgp = new PGP())
{
// Sign stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\signed.pgp"))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
pgp.SignFile(inputFileStream, outputFileStream, privateKeyStream, "password", true, true);
}
using (PGP pgp = new PGP())
{
// Sign stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\signed.pgp"))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
await pgp.SignFileAsync(inputFileStream, outputFileStream, privateKeyStream, "password", true, true);
}
Clear sign the provided file or stream using a private key so that it is still human readable.
gpg --output "C:\TEMP\Content\content.txt" --clearsign "C:\TEMP\Content\clearSigned.pgp"
using (PGP pgp = new PGP())
{
// Clear sign file
pgp.ClearSignFile(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\clearSigned.pgp", @"C:\TEMP\Keys\private.asc", "password", true, true);
}
using (PGP pgp = new PGP())
{
// Clear sign file
await pgp.ClearSignFileAsync(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\clearSigned.pgp", @"C:\TEMP\Keys\private.asc", "password", true, true);
}
using (PGP pgp = new PGP())
{
// Clear sign stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\clearSigned.pgp"))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
pgp.ClearSignStream(inputFileStream, outputFileStream, privateKeyStream, "password");
}
using (PGP pgp = new PGP())
{
// Clear sign stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\clearSigned.pgp"))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
await pgp.ClearSignFileAsync(inputFileStream, outputFileStream, privateKeyStream, "password", true, true);
}
Encrypt the provided file or stream 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"
using (PGP pgp = new PGP())
{
// Encrypt file and sign
pgp.EncryptFileAndSign(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\encryptedAndSigned.pgp", @"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "password", true, true);
}
using (PGP pgp = new PGP())
{
// Encrypt file and sign
await pgp.EncryptFileAndSignAsync(@"C:\TEMP\Content\content.txt", @"C:\TEMP\Content\encryptedAndSigned.pgp", @"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "password", true, true);
}
using (PGP pgp = new PGP())
{
// Encrypt and sign stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\encryptedAndSigned.pgp"))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
pgp.EncryptStreamAndSign(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, "password", true, true);
}
using (PGP pgp = new PGP())
{
// Encrypt and sign stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\encryptedAndSigned.pgp"))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
await pgp.EncryptStreamAndSignAsync(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, "password", true, true);
}
Decrypt the provided file or stream using the matching private key and passphrase.
gpg --output "C:\TEMP\Content\decrypted.txt" --decrypt "C:\TEMP\Content\encrypted.pgp"
using (PGP pgp = new PGP())
{
// Decrypt file
pgp.DecryptFile(@"C:\TEMP\Content\encrypted.pgp", @"C:\TEMP\Content\decrypted.txt", @"C:\TEMP\Keys\private.asc", "password");
}
using (PGP pgp = new PGP())
{
// Decrypt file
await pgp.DecryptFileAsync(@"C:\TEMP\Content\encrypted.pgp", @"C:\TEMP\Content\decrypted.txt", @"C:\TEMP\Keys\private.asc", "password");
}
using (PGP pgp = new PGP())
{
// Decrypt stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encrypted.pgp", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\decrypted.txt"))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
pgp.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, "password");
}
using (PGP pgp = new PGP())
{
// Decrypt stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encrypted.pgp", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\decrypted.txt"))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
await pgp.DecryptStreamAsync(inputFileStream, outputFileStream, privateKeyStream, "password");
}
Verify that the file or stream was signed by the matching private key of the counterparty.
gpg --verify "C:\TEMP\Content\signed.pgp"
using (PGP pgp = new PGP())
{
// Verify file
bool verified = pgp.VerifyFile(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}
using (PGP pgp = new PGP())
{
// Verify file
bool verified = await pgp.VerifyFileAsync(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}
using (PGP pgp = new PGP())
{
// Verify stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
bool verified = pgp.VerifyFile(inputFileStream, publicKeyStream);
}
using (PGP pgp = new PGP())
{
// Verify stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
bool verified = await pgp.VerifyFileAsync(inputFileStream, publicKeyStream);
}
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"
using (PGP pgp = new PGP())
{
// Verify clear file
bool verified = pgp.VerifyClearFile(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}
using (PGP pgp = new PGP())
{
// Verify clear file
bool verified = await pgp.VerifyClearFileAsync(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}
using (PGP pgp = new PGP())
{
// Verify clear stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
bool verified = pgp.VerifyClearFile(inputFileStream, publicKeyStream);
}
using (PGP pgp = new PGP())
{
// Verify clear stream
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\content.txt", FileMode.Open))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
bool verified = await pgp.VerifyClearFileAsync(inputFileStream, publicKeyStream);
}
Decrypt and then verify the provided encrypted and signed file. 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.
using (PGP pgp = new PGP())
{
// Decrypt file and verify
pgp.DecryptFileAndVerify(@"C:\TEMP\Content\encryptedAndSigned.pgp", @"C:\TEMP\Content\decryptedAndVerified.txt", @"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "password");
}
using (PGP pgp = new PGP())
{
// Decrypt file and verify
await pgp.DecryptFileAndVerifyAsync(@"C:\TEMP\Content\encryptedAndSigned.pgp", @"C:\TEMP\Content\decryptedAndVerified.txt", @"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "password");
}
using (PGP pgp = new PGP())
{
// Decrypt stream and verify
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encryptedAndSigned.pgp", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\decryptedAndVerified.txt"))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
pgp.DecryptStreamAndVerify(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, "password");
}
using (PGP pgp = new PGP())
{
// Decrypt stream and verify
using (FileStream inputFileStream = new FileStream(@"C:\TEMP\Content\encryptedAndSigned.pgp", FileMode.Open))
using (Stream outputFileStream = File.Create(@"C:\TEMP\Content\decryptedAndVerified.txt"))
using (Stream publicKeyStream = new FileStream(@"C:\TEMP\Keys\public.asc", FileMode.Open))
using (Stream privateKeyStream = new FileStream(@"C:\TEMP\Keys\private.asc", FileMode.Open))
await pgp.DecryptStreamAndVerifyAsync(inputFileStream, outputFileStream, publicKeyStream, privateKeyStream, "password");
}