Cinchoo PGP
.NET 的简单 PGP 包装库
目录
1. 引言
ChoPGP 库是一个开源的 .NET PGP 库。它是一个简单的 PGP 包装库,基于 Bouncy Castle 库。它包含了 PGP 加密、解密、解密并验证、密钥生成等方法,支持密钥库和文件密钥。100% 托管代码。
本文将介绍如何使用该库来加密、签名、解密或验证 OpenPGP 消息。ChoPGP 被开发为 OpenPGP 标准的开源实现。OpenPGP 是用于加密、解密、签名和验证数据的加密标准的。OpenPGP 协议被广泛使用,据说非常安全。
幸运的是,有一个免费的 OpenPGP 实现可用,那就是 Bouncy Castle。这是一个功能齐全且稳定的 OpenPGP 协议实现。
这个包装库简化了它的使用,可以快速地 PGP 加密和解密文件。
2. 假设
本文假设您对公钥密码学有一定的基本了解。
3. 特性
ChoPGP 提供的一些特性包括:
- 使用一行代码加密/解密 PGP 文件和流
- 同步或异步操作
- 公钥/私钥环文件生成
- ASCII armor,数据完整性包等。
Nuget 命令:Install-Package ChoPGP
4. 使用代码
该库公开了 ChoPGPEncryptDecrypt
类来执行 PGP 加密和解密过程。
public class ChoPGPEncryptDecrypt : IDisposable
{
//PGP DecryptFile overloads
public void DecryptFile(string inputFilePath, string outputFilePath,
string privateKeyFilePath, string passPhrase);
public Task DecryptFileAsync(string inputFilePath, string outputFilePath,
string privateKeyFilePath, string passPhrase);
//PGP EncryptFile overloads
public void EncryptFile(string inputFilePath, string outputFilePath,
string publicKeyFilePath, bool armor, bool withIntegrityCheck);
public Task EncryptFileAsync(string inputFilePath, string outputFilePath,
string publicKeyFilePath, bool armor, bool withIntegrityCheck);
//PGP EncryptFileAndSign overloads
public void EncryptFileAndSign(string inputFilePath, string outputFilePath,
string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor);
public Task EncryptFileAndSignAsync(string inputFilePath, string outputFilePath,
string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor);
//PGP GenerateKey overloads
public void GenerateKey(Stream publicKeyStream, Stream privateKeyStream,
string username = null, string password = null, int strength = 1024, int certainty = 8);
public void GenerateKey(string publicKeyFilePath, string privateKeyFilePath,
string username = null, string password = null, int strength = 1024, int certainty = 8);
public Task GenerateKeyAsync(string publicKeyFilePath, string privateKeyFilePath,
string username = null, string password = null, int strength = 1024, int certainty = 8);
}
4.1 生成密钥
在本节中,您将学习如何使用 ChoPGP
程序集来生成 PGP 密钥环。
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
pgp.GenerateKey("pub.asc", "pri.asc", "mark@gmail.com", "Test123");
其中
publicKeyFilePath
- 用于存储 PGP 公钥信息的文件的路径。将此密钥放在您的网站或电子邮件的签名底部。任何希望与您私下联系的人都可以使用您的 PGP 公钥向您发送加密消息。privateKeyFilePath
- 用于存储 PGP 私钥信息的文件的路径。将您的 PGP 私钥保存在计算机上的文件中,并尽可能保密。userName
- 建议使用您的电子邮件地址来生成 PGP 密钥。
您的电子邮件地址将作为 *公开信息* 包含在您的 PGP 公钥中,因此您的公钥可以被任何第三方 PGP 软件轻松导入。如果您不提供电子邮件地址,您的 PGP 解密软件可能无法将您的电子邮件地址与您的 PGP 公钥关联起来,因此无法自动加密/解密电子邮件消息。结果,您每次收到 PGP 加密消息时都必须手动解密。password
- 选择一个密码来保护您的 PGP 私钥。
如果有人设法 *窃取* 了您的 PGP 公钥,此密码将提供额外的保护层。strength
- 密钥强度。默认值为 1024。certainty
- 素数评估的确定性。Bouncy Castle 使用此数字生成随机数,并使用素数测试算法检查它们是否是素数。默认值为 8。
4.2 PGP 加密文件
在本节中,您将学习如何使用 ChoPGP
程序集来加密数据。
同步 PGP 加密文件
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.EncryptFile("SampleData.txt", "SampleData.PGP", "Pub.asc", true, false);
}
其中
inputFilePath
- 要加密的明文数据文件路径outputFilePath
- 输出的 PGP 加密文件路径publicKeyFilePath
- PGP 公钥文件路径armor
-True
表示将二进制数据表示为纯文本 ASCII。否则为false
withIntegrityCheck
-True
对输入文件执行完整性包检查。否则为false
异步 PGP 加密文件
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.EncryptFileAsync("SampleData.txt", "SampleData.PGP", "Pub.asc", true, false);
}
4.3 PGP 加密并签名文件
同步 PGP 加密并签名文件
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.EncryptFileAndSign("SampleData.txt", "SampleData.PGP", "Pub.asc", "Pri.asc",
"Test123", true);
}
其中
inputFilePath
- 要加密的明文数据文件路径outputFilePath
- 输出的 PGP 加密文件路径publicKeyFilePath
- PGP 公钥文件路径privateKeyFilePath
- PGP 私钥文件路径password
- PGP 私钥密码armor
-True
表示将二进制数据表示为纯文本 ASCII。否则为false
异步 PGP 加密并签名文件
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.EncryptFileAndSignAsync("SampleData.txt", "SampleData.PGP", "Pub.asc", "Pri.asc",
"Test123", true);
}
4.4 PGP 解密文件
在本节中,您将学习如何使用 ChoPGP
程序集来解密数据。
同步 PGP 解密文件
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.DecryptFile("SampleData.PGP", "SampleData.OUT", "Pri.asc", "Test123");
}
其中
inputFilePath
- PGP 加密数据文件outputFilePath
- 解密后的文件路径privateKeyFilePath
- PGP 私钥文件路径password
- PGP 私钥密码
异步 PGP 解密文件
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.DecryptFileAsync("SampleData.PGP", "SampleData.OUT", "Pri.asc", "Test123");
}