⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encrypt.cpp

📁 含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种CheckSum校验、多种MAC校验等几十种加密算法的程序
💻 CPP
字号:
/*Encrypt a file using a block cipher in CBC mode. Compresses the plaintextwith Zlib, MACs with HMAC(SHA-1). Stores the block cipher used in the file,so you don't have to specify it when decrypting.What a real application would do (and what this example should do), is test forthe presence of the Zlib module, and use it only if it's available. Then addsome marker to the stream so the other side knows whether or not the plaintextwas compressed. Bonus points for supporting multiple compression schemes.Based on the base64 example, of all thingsWritten by Jack Lloyd (lloyd@randombit.net) on August 5, 2002This file is in the public domain*/#include <fstream>#include <iostream>#include <string>#include <vector>#include <cstring>#include <cstdlib>#include <botan/filters.h>#include <botan/lookup.h>#include <botan/cbc.h>#include <botan/pgp_s2k.h>#if defined(BOTAN_EXT_COMPRESSOR_ZLIB)  #include <botan/zlib.h>#else  #error "You didn't compile the zlib module into Botan"#endifusing namespace Botan;std::string b64_encode(const SecureVector<byte>&);int main(int argc, char* argv[])   {   if(argc < 2)      {      std::cout << "Usage: " << argv[0] << " [-c algo] -p passphrase file\n"                   "   -p : Use this passphrase to encrypt\n"                   "   -c : Encrypt with block cipher 'algo' (default 3DES)\n";      return 1;      }   std::string algo = "TripleDES";   std::string filename, passphrase;   // Holy hell, argument processing is a PITA   for(int j = 1; argv[j] != 0; j++)      {      if(std::strcmp(argv[j], "-c") == 0)         {         if(argv[j+1])            {            algo = argv[j+1];            j++;            }         else            {            std::cout << "No argument for -c option" << std::endl;            return 1;            }         }      else if(std::strcmp(argv[j], "-p") == 0)         {         if(argv[j+1])            {            passphrase = argv[j+1];            j++;            }         else            {            std::cout << "No argument for -p option" << std::endl;            return 1;            }         }      else         {         if(filename != "")            {            std::cout << "You can only specify one file at a time\n";            return 1;            }         filename = argv[j];         }      }   if(passphrase == "")      {      std::cout << "You have to specify a passphrase!" << std::endl;      return 1;      }   std::ifstream in(filename.c_str());   if(!in)      {      std::cout << "ERROR: couldn't open " << filename << std::endl;      return 1;      }   std::string outfile = filename + ".enc";   std::ofstream out(outfile.c_str());   if(!out)      {      std::cout << "ERROR: couldn't open " << outfile << std::endl;      return 1;      }   try {   LibraryInitializer init;   if(!have_block_cipher(algo))      {      std::cout << "Don't know about the block cipher \"" << algo << "\"\n";      return 1;      }   OpenPGP_S2K s2k("SHA-1", 8192, 8);   u32bit key_len = max_keylength_of(algo);   u32bit iv_len = block_size_of(algo);   SymmetricKey bc_key = s2k.derive_key("BLK" + passphrase, key_len);   SymmetricKey mac_key = s2k.derive_key("MAC" + passphrase, key_len);   BlockCipherModeIV iv = s2k.derive_key("IVL" + passphrase, iv_len);   // Just to be all fancy we even write a (simple) header.   out << "-------- ENCRYPTED FILE --------" << std::endl;   out << algo << std::endl;   out << b64_encode(s2k.current_salt()) << std::endl;   Pipe pipe(new Fork(                new Chain(new MAC_Filter("HMAC(SHA-1)", mac_key),                          new Base64_Encoder                   ),                new Chain(new Zlib_Compress, // wrong tense                          new CBC_Encryption(algo, "PKCS7", bc_key, iv),                          new Base64_Encoder(true)                   )                )      );   pipe.start_msg();   in >> pipe;   pipe.end_msg();   out << pipe.read_all_as_string(0) << std::endl;   out << pipe.read_all_as_string(1);   }   catch(Botan::Algorithm_Not_Found)      {      std::cout << "Don't know about the block cipher \"" << algo << "\"\n";      return 1;      }   catch(std::exception& e)      {      std::cout << "Exception caught: " << e.what() << std::endl;      return 1;      }   return 0;   }std::string b64_encode(const SecureVector<byte>& in)   {   Pipe pipe(new Base64_Encoder);   pipe.start_msg();   pipe.write(in, in.size());   pipe.end_msg();   return pipe.read_all_as_string();   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -