Advertisement
Guest User

encrypt+compress issue

a guest
Aug 8th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. //  Boost.Crypto
  2. #include <boost/crypto/rc4_cipher.hpp>
  3.  
  4. //  Boost
  5. #include <boost/filesystem/fstream.hpp>
  6. #include <boost/iostreams/filter/zlib.hpp>
  7. #include <boost/iostreams/copy.hpp>
  8. #include <boost/iostreams/filtering_stream.hpp>
  9.  
  10.  
  11. template <typename StreamCipherT>
  12. class StreamCipherFilterBase
  13. {
  14. public:
  15.     typedef char        char_type;
  16.  
  17.     StreamCipherFilterBase(const std::string& key)
  18.     {
  19.         m_cipher.set_key(key.data(), static_cast<unsigned int>(key.length()));
  20.     }
  21. protected:
  22.     char                m_buffer[1024 * 2]; //  2k internal buffer size
  23.     StreamCipherT       m_cipher;
  24. };
  25.  
  26. template <typename StreamCipherT>
  27. class StreamCipherEncryptFilter
  28.     : public StreamCipherFilterBase<StreamCipherT>
  29. {
  30. public:
  31.     typedef boost::iostreams::multichar_output_filter_tag   category;
  32.  
  33.     StreamCipherEncryptFilter(const std::string& key)
  34.         : StreamCipherFilterBase<StreamCipherT>(key)
  35.     {
  36.     }
  37.  
  38.     template<typename Sink>
  39.     std::streamsize write(Sink& snk, const char* s, std::streamsize n)
  40.     {
  41.         std::streamsize written = 0;
  42.         std::streamsize remain  = n;
  43.         std::streamsize eat;
  44.         while(remain) {
  45.             eat = remain < sizeof(StreamCipherFilterBase<StreamCipherT>::m_buffer) ?
  46.                 remain : sizeof(StreamCipherFilterBase<StreamCipherT>::m_buffer);
  47.             StreamCipherFilterBase<StreamCipherT>::m_cipher.encrypt(
  48.                 s,
  49.                 StreamCipherFilterBase<StreamCipherT>::m_buffer,
  50.                 eat);
  51.             written += boost::iostreams::write(snk, StreamCipherFilterBase<StreamCipherT>::m_buffer, eat);
  52.             remain -= eat;
  53.             s += eat;
  54.         }
  55.         return written;
  56.     }
  57. };
  58.  
  59. template <typename StreamCipherT>
  60. class StreamCipherDecryptFilter
  61.     : public StreamCipherFilterBase<StreamCipherT>
  62. {
  63. public:
  64.     typedef boost::iostreams::multichar_input_filter_tag    category;
  65.  
  66.     StreamCipherDecryptFilter(const std::string& key)
  67.         : StreamCipherFilterBase<StreamCipherT>(key)
  68.     {
  69.     }
  70.  
  71.     template<typename Source>
  72.     std::streamsize read(Source& src, char* s, std::streamsize n)
  73.     {
  74.         const std::streamsize read = boost::iostreams::read(src, s, n);
  75.         if(EOF == read) {
  76.             return EOF;
  77.         }
  78.         StreamCipherFilterBase<StreamCipherT>::m_cipher.decrypt(s, s, read);
  79.         return read;
  80.     }
  81. };
  82.  
  83. const std::string KEY = "Kitteh";
  84. typedef boost::crypto::rc4_cipher   CipherT;
  85.  
  86. void Encrypt(
  87.     const std::string& in, const std::string& out)
  88. {
  89.     //  error handling omitted
  90.     boost::filesystem::ifstream inf(in, std::ios_base::binary | std::ios_base::in);
  91.     boost::filesystem::ofstream outf(out, std::ios_base::binary | std::ios_base::out);
  92.  
  93.     StreamCipherEncryptFilter<CipherT> outCipherFilter(KEY);
  94.     boost::iostreams::filtering_ostreambuf outs;
  95.     outs.push(boost::iostreams::zlib_compressor()); //  compress
  96.     outs.push(outCipherFilter);                     //  then encrypt
  97.     outs.push(outf);                                //  write to file
  98.  
  99.     boost::iostreams::copy(inf, outs);
  100. }
  101.  
  102. void Decrypt(
  103.     const std::string& in, const std::string& out)
  104. {
  105.     //  error handling omitted
  106.     boost::filesystem::ifstream inf(in, std::ios_base::binary | std::ios_base::in);
  107.     boost::filesystem::ofstream outf(out, std::ios_base::binary | std::ios_base::out);
  108.  
  109.     StreamCipherDecryptFilter<CipherT> inCipherFilter(KEY);
  110.     boost::iostreams::filtering_istreambuf ins;
  111.     ins.push(boost::iostreams::zlib_decompressor());
  112.     ins.push(inCipherFilter);
  113.     ins.push(inf);
  114.  
  115.     boost::iostreams::copy(ins, outf);  //  <-- exception thrown here with some files
  116. }
  117.  
  118. int main()
  119. {
  120.     //  compress+encrypt input.txt -> output.encrypt
  121.     Encrypt("input.txt", "output.encrypt");
  122.  
  123.     //  decrypt+decompress output.encrypt -> input.read.txt
  124.     Decrypt("output.encrypt", "input.read.txt");
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement