【问题标题】:Need example for BouncyCastle PGP File encryption in C#需要 C# 中的 BouncyCastle PGP 文件加密示例
【发布时间】:2010-04-22 19:20:35
【问题描述】:

我正在尝试使用我的私钥(ascii 格式)和任何其他公钥(也是 ascii 格式)加密文件。 BouncyCastle 库看起来是正确的使用方法,但我找不到 C# 的文档。谁能帮我举个例子。谢谢。

【问题讨论】:

  • 也许我没有正确理解 PGP,当您加密文件时,您是否只使用第 3 方的公钥?在此过程中没有使用您的私钥的任何部分,对吗?如果是这样,我需要将我的问题更改为“示例:如何使用 3rd 方公钥加密并使用私钥签名”
  • 任何非对称加密都是如此——您使用公钥进行加密和签名验证,而私钥用于签名和解密。

标签: c# .net bouncycastle pgp


【解决方案1】:

这是 BouncyCastle 示例中的一些代码。您应该获取源代码并查看单元测试,它们包含示例。我发现 Java 资源也很有用。该示例可以在 crypto\test\src\openpgp\examples\PbeFileProcessor.cs 下的源代码中找到

private static void EncryptFile(
        Stream  outputStream,
        string  fileName,
        char[]  passPhrase,
        bool    armor,
        bool    withIntegrityCheck)
    {
        if (armor)
        {
            outputStream = new ArmoredOutputStream(outputStream);
        }

        MemoryStream bOut = new MemoryStream();

        PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(
            CompressionAlgorithmTag.Zip);

        PgpUtilities.WriteFileToLiteralData(
            comData.Open(bOut),
            PgpLiteralData.Binary,
            new FileInfo(fileName));

        comData.Close();

        byte[] bytes = bOut.ToArray();

        PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(
            SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());

        cPk.AddMethod(passPhrase);

        Stream cOut = cPk.Open(outputStream, bytes.Length);

        cOut.Write(bytes, 0, bytes.Length);

        cOut.Close();

        if (armor)
        {
            outputStream.Close();
        }
    }

【讨论】:

  • ++ 谢谢。我在查找示例时遇到问题。
猜你喜欢
  • 2011-10-22
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 2013-10-10
  • 2011-08-05
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多