.net core PGP Encryption Decryption

廉价感情. 提交于 2019-12-24 07:49:03

问题


Running into error on the 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
}

I used https://code.msdn.microsoft.com/vstudio/Pretty-Good-Privacy-using-4f473c67 as a reference.

I am confused about the parameters in the PgpEncryptionKeys.

Does anyone have a working example or help? this is my first time encrypting so i a little lost.


回答1:


I'm using by this way:

I think can help you!

Helper:

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

Usage:

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


来源:https://stackoverflow.com/questions/52224818/net-core-pgp-encryption-decryption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!