Skip to content

.NET Core class library for using PGP

License

Notifications You must be signed in to change notification settings

SKumarSpace/PgpCore

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PgpCore

CircleCI

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.

Installation

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;

Dependencies

  • Portable.BouncyCastle (>= 1.8.6.7)

Usage

This is intended for usage in projects targeting .NET Standard 2.0.

Methods

Generate Key

Generate a new public and private key for the provided username and password.

gpg --gen-key

GenerateKey

using (PGP pgp = new PGP())
{
	// Generate keys
	pgp.GenerateKey(@"C:\TEMP\Keys\public.asc", @"C:\TEMP\Keys\private.asc", "email@email.com", "password");
}

Encrypt

Encrypt the provided file or stream using a public key.

gpg --output "C:\TEMP\Content\encrypted.pgp" --encrypt "C:\TEMP\Content\content.txt"

EncryptFile

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);
}

EncryptFileAsync

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);
}

EncryptStream

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);
}

EncryptStreamAsync

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

Sign the provided file or stream using a private key.

gpg --output "C:\TEMP\Content\content.txt" --sign "C:\TEMP\Content\signed.pgp"

SignFile

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);
}

SignFileAsync

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);
}

SignStream

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);
}

SignStreamAsync

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

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"

ClearSignFile

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);
}

ClearSignFileAsync

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);
}

ClearSignStream

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");
}

ClearSignStreamAsync

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 and Sign

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"

EncryptFileAndSign

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);
}

EncryptFileAndSignAsync

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);
}

EncryptStreamAndSign

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);
}

EncryptStreamAndSignAsync

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

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"

DecryptFile

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");
}

DecryptFileAsync

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");
}

DecryptStream

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");
}

DecryptStreamAsync

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

Verify that the file or stream was signed by the matching private key of the counterparty.

gpg --verify "C:\TEMP\Content\signed.pgp"

VerifyFile

using (PGP pgp = new PGP())
{
	// Verify file
	bool verified = pgp.VerifyFile(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}

VerifyFileAsync

using (PGP pgp = new PGP())
{
	// Verify file
	bool verified = await pgp.VerifyFileAsync(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}

VerifyStream

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);
}

VerifyStreamAsync

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 Clear

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"

VerifyClearFile

using (PGP pgp = new PGP())
{
	// Verify clear file
	bool verified = pgp.VerifyClearFile(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}

VerifyClearFileAsync

using (PGP pgp = new PGP())
{
	// Verify clear file
	bool verified = await pgp.VerifyClearFileAsync(@"C:\TEMP\Content\signed.pgp", @"C:\TEMP\Keys\public.asc");
}

VerifyClearStream

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);
}

VerifyClearStreamAsync

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 Verify

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.

gpg --output "C:\TEMP\Content\encryptedAndSigned.pgp" --decrypt "C:\TEMP\Content\decryptedAndVerified.txt"

DecryptFileAndVerify

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");
}

DecryptFileAndVerifyAsync

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");
}

DecryptStreamAndVerify

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");
}

DecryptStreamAndVerifyAsync

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");
}

About

.NET Core class library for using PGP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%