当前位置: 首页 > 知识库问答 >
问题:

C#中的BouncyCastle内存PGP加密

华献
2023-03-14

我一直在尝试使用BouncyCastle库来进行PGP加密/解密。我有一些代码需要修改,以便只使用流-不使用文件。

我尝试移除pgputilities.writeFileToliteralData(),然后让它返回一个流,但没有成功(输出流为空)。

这里更明确的是方法应该是什么:

public static Stream EncryptFile(MemoryStream inputStream, PgpPublicKey encKey, bool withIntegrityCheck)

下面是我需要修改的代码:

private static void EncryptFile(Stream outputStream, string fileName, PgpPublicKey encKey, bool armor, bool withIntegrityCheck)
{

    if (armor)
        outputStream = new ArmoredOutputStream(outputStream);

    try
    {
        MemoryStream bOut = new MemoryStream();
        PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(
        CompressionAlgorithmTag.Zip);
        PgpUtilities.WriteFileToLiteralData(
        comData.Open(bOut),
        PgpLiteralData.Binary,
        new FileInfo(fileName));
        comData.Close();
        PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(
        SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
        cPk.AddMethod(encKey);
        byte[] bytes = bOut.ToArray();
        Stream cOut = cPk.Open(outputStream, bytes.Length);
        cOut.Write(bytes, 0, bytes.Length);
        cOut.Close();
        if (armor)
            outputStream.Close();
    }

    catch (PgpException e)
    {

        Console.Error.WriteLine(e);
        Exception underlyingException = e.InnerException;
        if (underlyingException != null)
        {

            Console.Error.WriteLine(underlyingException.Message);
            Console.Error.WriteLine(underlyingException.StackTrace);

        }
    }
}

public void EncryptFile(string filePath, string publicKeyFile, string pathToSaveFile)
{
    Stream keyIn, fos;
    keyIn = File.OpenRead(publicKeyFile);
    string[] fileSplit = filePath.Split('\\');
    string fileName = fileSplit[fileSplit.Length - 1];
    fos = File.Create(pathToSaveFile + fileName + ".asc");
    EncryptFile(fos, filePath, ReadPublicKey(keyIn), true, true);
    keyIn.Close();
    fos.Close();
}

共有1个答案

严阳成
2023-03-14

公认的解决方案将在使用弹性城堡时起作用。然而,对于任何寻找更简单(?!)解决方案-看看PgpCore nuget。这是一个包装围绕弹力城堡,只是使它更容易,而不需要知道它的内部。

// Encrypting a stream using PgpCore
using(PGP pgp = new PGP())
{
    using (FileStream inputFileStream = new FileStream(@"C:\TEMP\keys\content.txt", FileMode.Open))
    using (Stream outputFileStream = File.Create(@"C:\TEMP\keys\content__encrypted2.pgp"))
    using (Stream publicKeyStream = new FileStream(@"C:\TEMP\keys\public.asc", FileMode.Open))
        pgp.EncryptStream(inputFileStream, outputFileStream, publicKeyStream, true, true);
}
 类似资料:
  • 在SSO的实现中,我使用了bouncycastle(JAVA)进行签名、加密、解密和签名的验证。我有原始PGP公钥和私钥,我需要将它们存储在Java Keystore中。这些PGP公钥没有证书。 我知道对于公钥(根据Keystore的javadoc:http://docs.oracle.com/javase/6/docs/api/java/security/keystore.html),我必须创建

  • 我试图使用java BouncyCastle库解密和验证PGP消息,但遇到了一些问题,抱怨PartialInputStream过早结束。 我知道encrypt工作得很好,因为我可以在命令行上使用gpg解密和验证使用encrypt函数创建的消息。

  • 我目前正在用java编写一个加密消息传递服务,我使用的是bouncycastle PGP库。我编写了一个生成密钥对的测试程序,并对消息进行加密/解密。这已经工作了一段时间,但它最近在解密阶段停止了,给了我一个InvalidKeyException。 我做了一些研究,下载了JCE.jar文件,并将它们导入到我的项目中(通过Eclipse Project->Properties->add extern

  • 代码在这里,使用BouncyCastle实现: 我希望能够读取此签名并使用公钥验证它。如何将其转换回BouncyCastle对象?

  • 我正在尝试编写一个加密文件,它将使用gpg解密,并将以增量方式写入行,而不是在一个块中。我已经在GnuPG中生成了密钥,并且正在使用公钥加密。下面是我正在使用的加密方法: 我有一个小的原型测试类来使用这种方法: 我怎样才能修改这段代码,使它既能加密两行,又能让GnuPG解密它们呢?