使用管道加密文件 [英] Use of Pipelines to encrypt a file

查看:165
本文介绍了使用管道加密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚启动了使用crypto ++的startet,我有一个关于piplene的问题,以及如何使用它们加密文件。



我想使用AES加密一个文件。



1.)只要做到这一点就够了:

  EAX&AES> ::加密加密器; 
encryptor.SetKeyWithIV(derived.data(),16,ivb,ivb.size());
FileSource f(source,新的AuthenticatedEncryptionFilter(encryptor,new FileSink(deststination)));

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



3。)如果不存在,会自动创建输出文件吗?



编辑:



好的,我得到了我的欢迎。



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



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

解决方案


  EAX< AES> ::加密加密器; 
encryptor.SetKeyWithIV(derived.data(),16,ivb,ivb.size());
FileSource f(source,新的AuthenticatedEncryptionFilter(encryptor,new FileSink(deststination)));


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

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

另请参阅 FileSource 在Crypto ++维基。而手册中的 FileSource类参考也可能会引起关注。 p>





如果我有一个巨大的输入文件,这种方法会自动加密块中的文件吗? / p>

是的。在内部,Crypto ++将在4096字节的IIRC中阻塞或块处理。最近的一个关于这个问题的讨论发生在邮件列表中的 ios在加密期间锁定



允许您进行阻止在帖子中提供。您可以使用它来限制处理,如果需要,可以更新进度条或生成处理器。







如果不存在,会自动创建输出文件吗?


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



再次,这里是维基页面: p>







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


是的。在这种情况下,请使用 bool pumpAll 并将其设置为 false 。然后执行以下操作:

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

size_t remaining =<文件大小> ;;
size_t BLOCK_SIZE = 512;
while(remaining&&fs.SourceExhausted())
{
const unsigned int req = STDMIN(remaining,BLOCK_SIZE);
fs.Pump(req);
fs.Flush(false);

剩余 - = req;
}

或者,您可以:



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

另请参阅 FileSource类参考跳过 BufferedTransformation 的一部分;而$ code> PumpAll 是 Source 的一部分。






还有涵盖EAX模式和认证的{en | de} cryption过滤器的wiki页面。请参阅:





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





< hr>

以下程序使用 CFB_Mode< AES> 它很容易交换另一种密码和模式。它还演示了如何在堆栈中放置对象并将其用于管道中,而不是使用 新建 c

  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<<< 关键:;
// ArraySource as(key.data(),key.size(),true,new HexEncoder(new FileSink(cout)));
// cout<<< ENDL;

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

MeterFilter meter;
StreamTransformationFilter stf(enc);

FileSource源(/ 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(剩余&!source.SourceExhausted())
{
if(剩余%(1024)== 0)
{
cout< < 处理:< meter.GetTotalBytes()<< ENDL;
}

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

剩余 - = req;
}
}
catch(const Exception& ex)
{
cerr<< ex.what()<< ENDL;
}

return 0;
}


I have just startet working with crypto++ and i have a question about the piplenes and how to use them to encrypt a file.

I want to use AES to encrypt a file.

1.)Would it be enough to just do:

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

2.) If i have a huge input file, will this approach automaticly encrypt the files in blocks?

3.) Would this automaticly create the output file if it is not there?

EDIT:

Ok, i got it to wok with my approch.

The 2.) Question remains and i have a new one:

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

解决方案

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

Close. The AuthenticatedEncryptionFilter will be coerced to the bool pumpAll parameter of FileSink. So you need:

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

Also see FileSource on the Crypto++ wiki. And the FileSource Class Reference from the manual might be of interest, too.


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

Yes. Internally, Crypto++ will "block" or "chunk" the processing in 4096-bytes, IIRC. A recent discussion about it occurred on the mailing list at ios locking up during encryption.

A program that allows you to do the blocking is provided in the post. You can use it to throttle the processing, a place to update a progress bar or yield the processor, if needed. Its reproduced below.


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

Yes. The FileSource is just a std::ifstream wrapper, while a FileSink is just a std::ofstream wrapper.

Again, here are the wiki pages:


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

Yes. In this case, use bool pumpAll and set it to false. Then do something like:

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

Or, you can:

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

Also see FileSource Class Reference in the manual. Skip is part of BufferedTransformation; and PumpAll is part of Source.


There are also wiki pages covering EAX mode and the authenticated {en|de}cryption filters. See:

There's even a page on using a Java-like Init/Update/Final at:


The program below uses CFB_Mode<AES>, but its easy enough to swap in another cipher and mode. It also demonstrates how to place objects on the stack and use them in a pipeline rather than creating them on the heap with 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;
}

这篇关于使用管道加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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