FileSink、StringSink、Filesource、StringSource Crypto++ 之间有什么区别

2024-04-17

我正在读取图像,对其进行加密,然后解密。目标是最终循环并记录该过程完成所需的时间。目前我所拥有的它读取文件,然后加密它,加密它,然后根据恢复的数据创建另一个文件。我不需要用解密的图片创建另一个文件。以前我一直在使用StringSource and StringSink,但这仅适用于文本文件。我在以下位置获得了一些帮助如何将图像读取为字符串以加密 Crypto++ https://stackoverflow.com/questions/23354548/how-to-read-an-image-to-a-string-for-encrypting-crypto/23355279#23355279从并开始使用FileSink and FileSource.

到底有什么区别FileSink, StringSink, FileSource and StringSource ?

另外,在下面的示例中,为什么需要将 cipher 设置为某些内容?之前我刚用的时候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;
}

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

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

所有来源均可互换。所有过滤器均可互换。所有水槽均可互换。例如,要在StringSink and FileSink,您需要提供一个带有FileSink。否则,它们的操作相同。作为另一个例子,您可以在HexEncoder and Base64Encoder没有任何变化。作为最后一个例子,SocketSource or SocketSink将需要 IP 地址和端口。哪些内容可能(或可能不需要)需要更改取决于对象。

有很多来源。从源类参考 http://www.cryptopp.com/docs/ref/class_source.html:

  • FileSource
  • StringSource
  • RandomNumberSource
  • WindowPipeSource
  • SocketSource

有许多过滤器。您正在使用其中两个 -AuthenticatedEncryptionFilter and AuthenticatedDecryptionFilter. From 过滤器类参考 http://www.cryptopp.com/docs/ref/class_filter.html and FilterWithBufferedInput 类参考 http://www.cryptopp.com/docs/ref/class_filter_with_buffered_input.html:

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

有多个水槽。从接收器类参考 http://www.cryptopp.com/docs/ref/class_sink.html:

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

有一些高级主题,但我认为目前它们并不重要。例如,角色BufferedTransformation以及如果Attachable回报true。答案是 Filter 和 Sinks 都是BufferedTransformation's, and Attachable = true意味着它是一个过滤器(否则它是一个接收器)。


...在下面的示例中,为什么需要将 cipher 设置为某些内容...

A StringSource and StringSink不需要任何东西,因为它只是内存中的一组再见。 AFileSource and FileSink需要一个文件名,并且您正在使用cipher为文件名。你have提供文件名,因为对象与文件/流相关。如果您使用的是SocketSource or SocketSink,那么您需要提供 IP 地址和端口(更正确地说,是socket_t).

这里有FileSource构造函数来自文件源类参考 http://www.cryptopp.com/docs/ref/class_file_source.html。您在代码中使用第三个构造函数。

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构造函数来自FileSink 类参考 http://www.cryptopp.com/docs/ref/class_file_sink.html。您在代码中使用第二个构造函数。

FileSink (std::ostream &out)
FileSink (const char *filename, bool binary=true)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FileSink、StringSink、Filesource、StringSource Crypto++ 之间有什么区别 的相关文章

随机推荐