Need example for BouncyCastle PGP File encryption in C#

元气小坏坏 提交于 2019-12-18 12:01:08

问题


I'm trying to encrypt files using my private key (in ascii format) and any other public key (also in ascii format). The BouncyCastle library looks like the correct thing to use, but I cannot find documentation for C#. Could anyone please assist me with an example. Thank you.


回答1:


Here's some code from the BouncyCastle example. You should grab the source code and look in the unit tests, they contain examples. I've found that Java resources are also useful. The example can be found in the source under 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();
        }
    }


来源:https://stackoverflow.com/questions/2693769/need-example-for-bouncycastle-pgp-file-encryption-in-c-sharp

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