c++ - 使用管道加密文件

标签 c++ encryption crypto++

我刚刚开始使用 crypto++,我对 piplenes 以及如何使用它们加密文件有疑问。

我想使用 AES 加密文件。

1.)仅仅做就够了吗:

EAX<AES>::Encryption encryptor;
encryptor.SetKeyWithIV(derived.data(), 16, ivb, ivb.size());
FileSource f("source", new AuthenticatedEncryptionFilter(encryptor,new FileSink("deststination")));

2.) 如果我有一个巨大的输入文件,这种方法会自动加密 block 中的文件吗?

3.) 如果不存在,是否会自动创建输出文件?

编辑:

好的,我按照我的方法开始了。

2.) 问题仍然存在,我有一个新问题:

我可以告诉它跳过文件的前 24 个字节吗?

最佳答案

EAX<AES>::Encryption encryptor;
encryptor.SetKeyWithIV(derived.data(), 16, ivb, ivb.size());
FileSource f("source", new AuthenticatedEncryptionFilter(encryptor,new FileSink("deststination")));

关闭。 AuthenticatedEncryptionFilter将被强制到 bool pumpAll FileSink 的参数.所以你需要:

FileSource f("source", true, new AuthenticatedEncryptionFilter(encryptor,new FileSink("deststination")));

另见 FileSource在 Crypto++ 维基上。和 FileSource Class Reference手册中的内容也可能很有趣。


If i have a huge input file, will this approach automatically encrypt the files in blocks?

是的。在内部,Crypto++ 将以 4096 字节 IIRC 为单位“阻止”或“分块”处理。最近关于它的讨论发生在 ios locking up during encryption 的邮件列表中。 .

allows you to do the blocking 的程序在帖子中提供。如果需要,您可以使用它来限制处理,更新进度条或让出处理器的地方。其转载如下。


Would this automatically create the output file if it is not there?

是的。 FileSource只是一个std::ifstream包装器,而一个 FileSink只是一个std::ofstream包装器。

同样,这里是 wiki 页面:


Can I tell it to skip the first 24 bytes of the file?

是的。在这种情况下,使用 bool pumpAll并将其设置为 false .然后做类似的事情:

FileSource fs("source", false, new AuthenticatedEncryptionFilter(...));
fs.Skip(24);

size_t remaining = <size of file>;
size_t BLOCK_SIZE = 512;
while(remaining && !fs.SourceExhausted())
{    
    const unsigned int req = STDMIN(remaining, BLOCK_SIZE);
    fs.Pump(req);
    fs.Flush(false);

    remaining -= req;
}

或者,您可以:

FileSource fs("source", false, new AuthenticatedEncryptionFilter(...));
fs.Skip(24);
fs.PumpAll();

另见 FileSource Class Reference在手册中。 SkipBufferedTransformation 的一部分;和 PumpAllSource 的一部分.


还有一些 wiki 页面涵盖了 EAX 模式和经过身份验证的{en|de}加密过滤器。见:

甚至还有一个关于使用类似 Java 的 Init/Update/Final 的页面:


下面的程序使用 CFB_Mode<AES> ,但很容易换成另一种密码和模式。它还演示了如何将对象放在堆栈上并在管道中使用它们,而不是使用 new 在堆上创建它们。

int main(int argc, char* argv[])
{
  static const unsigned int BIG_SIZE = 2U * 1024U * 1024U;    
  static const unsigned int BLOCK_SIZE = 4096U;

  try
    {
      SecByteBlock key(32);
      OS_GenerateRandomBlock(false, key.data(), key.size());

      // cout << "Key: ";
      // ArraySource as(key.data(), key.size(), true, new HexEncoder(new FileSink(cout)));
      // cout << endl;

      CFB_Mode<AES>::Encryption enc;
      enc.SetKeyWithIV(key.data(), key.size(), key.data());

      MeterFilter meter;
      StreamTransformationFilter stf(enc);

      FileSource source("/dev/zero", false);
      FileSink sink("zero.enc");

      source.Attach(new Redirector(stf));
      stf.Attach(new Redirector(meter));
      meter.Attach(new Redirector(sink));    

      unsigned int remaining = BIG_SIZE;
      while(remaining && !source.SourceExhausted())
      {
        if(remaining % (1024) == 0)
        {
          cout << "Processed: " << meter.GetTotalBytes() << endl;    
        }

        const unsigned int req = STDMIN(remaining, BLOCK_SIZE);
        source.Pump(req);
        source.Flush(false);

        remaining -= req;
    }
  }
  catch(const Exception& ex)
  {
    cerr << ex.what() << endl;
  }

  return 0;
}

关于c++ - 使用管道加密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35241696/

相关文章:

c++ - 矩阵在任何运算符后变得相同

C++ else 语句仅当宏被定义时

Python 3 XChaCha20 测试向量可用于加密,但解密阶段失败

c++ - 在 Visual Studio 2013 下为/MD 版本重建 Crypto++ 5.6.2

javascript - 深拷贝 NAPI::Value 对象

c++ - 登录 预处理号码

php - 2 路加密密码?

python - 连接到 rabbitmq SSLError : [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl. c:1108)

android - RSA::PublicKey 加载函数段错误 ART(Android 运行时)

c++ - std::string 的解密有额外的填充字节?