【问题标题】:encrypt file with BouncyCastle , output look corrupted使用 BouncyCastle 加密文件,输出看起来已损坏
【发布时间】:2023-03-10 02:42:01
【问题描述】:

所以我使用此代码来加密我的文件

你可以看到 iam 使用公共 PGP

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.2.6 (GNU/Linux)

masdSFVkRBADYxPZYC+nu9nhSVkxcVkVJ5axZKzCRuygqUxka
kZIBy2CAQVKz5dBkRaUkaaksbcyautks7asaov26Fc9cT25Rvnh7
wYIJhcRoIl4cxashdgutasd0qfcOnVB5JVCQDhXclBW7kwCgkoUW
....
...
...
-----END PGP PUBLIC KEY BLOCK-----

代码工作正常,但我认为加密文件的数据已损坏

因为它不是以这种格式出现的(就像钥匙一样)

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.2.6 (GNU/Linux)

masdSFVkRBADYxPZYC+nu9nhSVkxcVkVJ5axZKzCRuygqUxka
kZIBy2CAQVKz5dBkRaUkaaksbcyautks7asaov26Fc9cT25Rvnh7
wYIJhcRoIl4cxashdgutasd0qfcOnVB5JVCQDhXclBW7kwCgkoUW
....
...
...
-----END PGP PUBLIC KEY BLOCK-----

我错了吗?

输出的格式不应该是一样的吗?

using System;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;

using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.IO;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Bcpg;

class CPGPencrypt
{
    private static PgpPublicKey ReadPublicKey(Stream inputStream)
    {

        inputStream = PgpUtilities.GetDecoderStream(inputStream);

        PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);

        //
        // we just loop through the collection till we find a key suitable for encryption, in the real
        // world you would probably want to be a bit smarter about this.
        //

        //
        // iterate through the key rings.
        //

        foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
        {
            foreach (PgpPublicKey k in kRing.GetPublicKeys())
            {
                if (k.IsEncryptionKey)
                {
                    return k;
                }
            }
        }

        throw new ArgumentException("Can't find encryption key in key ring.");
    }

    private static byte[] EncryptFile(byte[] clearData, string fileName, PgpPublicKey encKey, bool withIntegrityCheck)
    {

        MemoryStream bOut = new MemoryStream();

        PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(
            CompressionAlgorithmTag.Zip);

        Stream cos = comData.Open(bOut); // open it with the final destination
        PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator();

        // we want to Generate compressed data. This might be a user option later,
        // in which case we would pass in bOut.
        Stream pOut = lData.Open(
            cos,                    // the compressed output stream
            PgpLiteralData.Binary,
            fileName,               // "filename" to store
            clearData.Length,       // length of clear data
            DateTime.UtcNow         // current time
        );

        pOut.Write(clearData, 0, clearData.Length);

        lData.Close();
        comData.Close();

        PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, new SecureRandom());

        cPk.AddMethod(encKey);

        byte[] bytes = bOut.ToArray();

        MemoryStream encOut = new MemoryStream();
        Stream os = encOut;

        Stream cOut = cPk.Open(os, bytes.Length);
        cOut.Write(bytes, 0, bytes.Length);  // obtain the actual bytes from the compressed stream
        cOut.Close();

        encOut.Close();

        return encOut.ToArray();
    }

    public static string Encrypt(string file_name,string file_to_read)
    {


        try
        {

            byte[] dataBytes = File.ReadAllBytes(file_to_read);
            Stream keyIn = File.OpenRead("pgpdata-public.asc");
            Stream outStream = File.Create(@"myfolder\"+file_name);
            byte[] encrypted = EncryptFile(dataBytes, @"myfolder\"+file_name, ReadPublicKey(keyIn), false);
            outStream.Write(encrypted, 0, encrypted.Length);
            keyIn.Close();
            outStream.Close();
        }
        catch (Exception e)
        {
            return e.Message;
        }
        return file_name;

    }


}

【问题讨论】:

  • 你现在得到了什么作为输出?
  • 类似的东西 ´îØK'ó*Dò+Uþ¢Uûµ"ÍG×H ‚...ø IªvvOõ–ËEAë¹™ÅO3hšÀÅýئ¿º
  • 您正在加密文件,数据正在被压缩并以二进制形式输出。为什么你期望它看起来像一个明文签名块?
  • 我不知道为什么,这是我第一次加密文件,你可以判断这个输出是否是逻辑的......

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


【解决方案1】:

OpenPGP中有不同的编码方案,即

  1. 二进制数据和
  2. ASCII 装甲数据。

特别是对于密钥交换,通常首选 ASCII 装甲格式,因为它更健壮且易于识别。对于邮件交换,这是强制性的(为了 7 位兼容性)。二进制版本也有优势,尤其是在性能和​​存储(带宽)要求方面。

例如,GnuPG 将默认使用二进制编码,除非您使用选项--ascii 或缩写-a 请求ASCII 装甲版本。

看起来您的代码正在输出二进制编码,但一切正常。

您可以通过尝试解密轻松进行测试(例如,使用 GnuPG:gpg --decrypt file.pgp)。或者,您可以使用gpg --list-packets file.pgp 或使用更详细的实用程序pgpdump转储OpenPGP 数据包,该实用程序在大多数(unix)软件包存储库中都可用:pgpdump file.pgp。与gpg --list-packets 不同,它还将数据包和算法标识符解析为人类可读的字符串(gpg --list-packets 只是转储它们的数字 ID)。

【讨论】:

  • 好的,这是我的选项 Binary, Text, utf8 什么与 ASCII 装甲数据匹配?文字
  • 二进制是二进制(你在上面观察到的)。 ASCII 装甲文本是“可读”的纯文本(至少是它的标题),否则只会包含 ASCII 字符。 OpenPGP 输出只有这两种选择。
  • 对于那些偶然发现这里的人来说,Literal Data Packet 的格式,听起来像是 user1310492 所指的,与 PGP 文件的 ASCII 与二进制输出不同。
  • 其实差不多就是base64 with some checksum addition
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多