【问题标题】:.net core PGP Encryption Decryption.net core PGP加密解密
【发布时间】:2019-02-12 22:36:20
【问题描述】:

在void Encryption() 上遇到错误。

public void Encryption()
{
    #region PGP Encryption 

    PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(@"C:\Keys\PGPPublicKey.asc", @"C:\Keys\PGPPrivateKey.asc", "password");
    PgpEncrypt encrypter = new PgpEncrypt(encryptionKeys);
    using (Stream outputStream = File.Create("C:\\Keys\\EncryptData.txt"))
    {
        encrypter.EncryptAndSign(outputStream, new FileInfo(@"D:\Keys\PlainText.txt"));
    }
    Console.WriteLine("Encryption Done !");

    #endregion
}

我用https://code.msdn.microsoft.com/vstudio/Pretty-Good-Privacy-using-4f473c67 作为参考。

我对@9​​87654324@中的参数感到困惑。

有没有人有一个可行的例子或帮助?这是我第一次加密,所以我有点迷失。

【问题讨论】:

    标签: c# encryption bouncycastle public-key-encryption pgp


    【解决方案1】:

    我是这样用的:

    我想可以帮到你!

    助手:

    public static void EncryptPgpFile(string inputFile, string outputFile, string publicKeyFile, bool armor, bool withIntegrityCheck)
    {
        using (Stream publicKeyStream = File.OpenRead(publicKeyFile))
        {
            PgpPublicKey pubKey = ReadPublicKey(publicKeyStream);
    
            using (MemoryStream outputBytes = new MemoryStream())
            {
                PgpCompressedDataGenerator dataCompressor = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
                PgpUtilities.WriteFileToLiteralData(dataCompressor.Open(outputBytes), PgpLiteralData.Binary, new FileInfo(inputFile));
    
                dataCompressor.Close();
                PgpEncryptedDataGenerator dataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
    
                dataGenerator.AddMethod(pubKey);
                byte[] dataBytes = outputBytes.ToArray();
    
                using (Stream outputStream = File.Create(outputFile))
                {
                    if (armor)
                    {
                        using (ArmoredOutputStream armoredStream = new ArmoredOutputStream(outputStream))                   
                        using (Stream outputStream = dataGenerator.Open(armoredStream, dataBytes.Length))
                            outputStream.Write(dataBytes, 0, dataBytes.Length);
                    }
                    else
                    {
                        using (Stream outputStream = dataGenerator.Open(armoredStream, dataBytes.Length))
                            outputStream.Write(dataBytes, 0, dataBytes.Length);
                    }
                }
            }
        }
    }
    
    private static PgpPublicKey ReadPublicKey(Stream inputStream)
    {
        inputStream = PgpUtilities.GetDecoderStream(inputStream);
        PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);
    
        foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
        {
            foreach (PgpPublicKey key in keyRing.GetPublicKeys())
            {
                if (key.IsEncryptionKey)
                    return key;
            }
        }
    
        throw new ArgumentException("Can't find encryption key in key ring.");
    }
    

    用法:

    EncryptPgpFile(inputFile, outputFile, publicKeyPath, true, true);
    

    【讨论】:

    • 我将 PgpCore (1.3.1) 添加到我的项目中。 PgpUtilities.WriteFileToLiteralData 来自哪里?上面的代码无法解决我的问题。此外,我更改了 else dataGenerator.Open 以使用 File.Create 中的 outputStream。而且...我将 if 和 else 流重命名为 outStream,因为它与外部范围 outputStream.. 发生冲突。或者这是故意的吗?
    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2021-09-04
    • 2012-05-06
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多