【问题标题】:What is the difference between FileSink, StringSink, Filesource, StringSource Crypto++FileSink、StringSink、Filesource、StringSource Crypto++有什么区别
【发布时间】:2014-06-17 11:31:23
【问题描述】:

我正在读取图像,对其进行加密,然后对其进行解密。目标是最终循环并记录该过程完成所需的时间。目前我拥有它读取文件,然后对其进行加密,加密,然后根据恢复的数据创建另一个文件。我不需要用解密的图片制作另一个文件。以前我一直在使用StringSourceStringSink,但这仅适用于文本文件。我在How to read an image to a string for encrypting Crypto++ 获得了一些帮助,并开始使用FileSinkFileSource

FileSinkStringSinkFileSourceStringSource 之间到底有什么区别?

另外,在下面的例子中,为什么需要设置密码?以前我使用StringSource时,我的字符串密码没有初始化,但现在我使用FileSource,需要初始化它才能工作。

int main(int argc, char* argv[])
{
    AutoSeededRandomPool prng_blowfish;

    SecByteBlock key_blowfish(Blowfish::DEFAULT_KEYLENGTH);
    prng_blowfish.GenerateBlock(key_blowfish, key_blowfish.size());

    byte iv_blowfish[Blowfish::BLOCKSIZE];
    prng_blowfish.GenerateBlock(iv_blowfish, sizeof(iv_blowfish));

    string ifilename = "sample_files/1MB.jpg";
    string cipher = "1MB.enc";
    string rfilename = "r1MB.jpg";

    try
    {
        EAX<Blowfish>::Encryption e_blowfish;
        e_blowfish.SetKeyWithIV(key_blowfish, key_blowfish.size(), iv_blowfish, sizeof(iv_blowfish));

        std::ifstream ifile(ifilename.c_str(), ios::binary);
        std::ifstream::pos_type size = ifile.seekg(0, std::ios_base::end).tellg();
        ifile.seekg(0, std::ios_base::beg);

        FileSource fs1(ifilename.c_str(), true, new AuthenticatedEncryptionFilter(e_blowfish, new FileSink(cipher.c_str())));

        EAX<Blowfish>::Decryption d_blowfish;
        d_blowfish.SetKeyWithIV(key_blowfish, key_blowfish.size(), iv_blowfish, sizeof(iv_blowfish));

        FileSource fs2(cipher.c_str(), true, new AuthenticatedDecryptionFilter(d_blowfish, new FileSink(rfilename.c_str()), AuthenticatedDecryptionFilter::THROW_EXCEPTION));
    }
    catch (const Exception& ex)
    {
        cerr << ex.what() << endl;
    }

    return 0;
}

【问题讨论】:

  • @jww 我把你的代码读入了文件!不过我有一些问题。

标签: c++ encryption file-io binaryfiles crypto++


【解决方案1】:

FileSink、StringSink、FileSourcem StringSource到底有什么区别。

Sources、Filters 和 Sinks 是 Crypto++ 中管道设计的一部分。数据从源流出,经过过滤器转换,然后在接收器结束。

所有来源都可以互换。所有过滤器都可以互换。所有的水槽都是可以互换的。例如,要在StringSinkFileSink 之间切换,您需要提供带有FileSink 的文件名。否则,它们的操作相同。作为另一个示例,您可以在HexEncoderBase64Encoder 之间切换而无需更改。作为最后一个示例,SocketSourceSocketSink 将需要 IP 地址和端口。可能需要(或不需要)更改的内容取决于对象。

有很多来源。来自Source Class Reference

  • FileSource
  • StringSource
  • RandomNumberSource
  • WindowPipeSource
  • SocketSource

有许多过滤器。您正在使用其中的两个 - AuthenticatedEncryptionFilterAuthenticatedDecryptionFilter。来自Filter Class ReferenceFilterWithBufferedInput Class Reference

  • HexEncoder
  • HexEncoder
  • Base32Encoder
  • Base32Decoder
  • Base64Encoder
  • Base64Encoder
  • DefaultEncryptor
  • DefaultEncryptorWithMAC
  • DefaultDecryptor
  • DefaultDecryptorWithMAC
  • ...
  • StreamTransformationFilter
  • AuthenticatedEncryptionFilter
  • AuthenticatedDecryptionFilter

有许多水槽。来自Sink Class Reference

  • ArraySink
  • BitBucket
  • RandomNumberSink
  • StringSink
  • FileSink
  • SocketSink
  • ...

有一些高级主题,但我认为它们目前并不重要。例如BufferedTransformation 的作用以及如果Attachable 返回true 意味着什么。答案是过滤器和接收器都是BufferedTransformation 的,Attachable = true 表示它是过滤器(否则它是接收器)。


...在以下示例中,为什么需要将密码设置为...

StringSourceStringSink 不需要任何东西,因为它只是内存中的一个字节数组。 FileSourceFileSink 需要一个文件名,而您使用 cipher 作为文件名。您必须提供文件名,因为对象与文件/流相关。如果您使用的是SocketSourceSocketSink,则需要提供IP 地址和端口(更准确地说,是socket_t)。

这里是来自FileSource Class ReferenceFileSource 构造函数。您在代码中使用了第三个构造函数。

FileSource (BufferedTransformation *attachment=NULL)
FileSource (std::istream &in, bool pumpAll, BufferedTransformation *attachment=NULL)
FileSource (const char *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)

这里是来自FileSink Class ReferenceFileSink 构造函数。您在代码中使用了第二个构造函数。

FileSink (std::ostream &out)
FileSink (const char *filename, bool binary=true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2016-12-01
    • 2012-05-01
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    相关资源
    最近更新 更多