例如需要在C#中BouncyCastle的PGP文件加密 [英] Need example for BouncyCastle PGP File encryption in C#

查看:288
本文介绍了例如需要在C#中BouncyCastle的PGP文件加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的私有密钥(ASCII格式)和任何其他公共密钥(也以ASCII格式)来加密文件。该BouncyCastle的库看起来正确的事情使用,但我找不到C#文档。任何人都可以请帮我用一个例子。谢谢你。

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.

推荐答案

下面是从BouncyCastle的例子一些code。你应该抓住源头code,并期待在单元测试,它们含有的例子。我发现,Java的资源也是很有用的。这个例子可以在源下的加密\测试\ SRC中找到\ OpenPGP的\例子\ PbeFileProcessor.cs

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();
        }
    }

这篇关于例如需要在C#中BouncyCastle的PGP文件加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆