Advertisement
Phr0zen_Penguin

Simple Crypto

Oct 4th, 2014
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.64 KB | None | 0 0
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [simpleCrypto.cpp]
  4.   A thorough demonstration of how the not-so-simplest method of cryptography works.
  5.  
  6.     NOTE:
  7.     This particular method employs the substitution cipher technique, using (only) the Extended ASCII
  8.     Character set.
  9.  
  10.     The ASCII chart can be obtained at: http://www.asciitable.com
  11.  
  12.         (c) Damion 'Phr0z3n.dev' Tapper, 2014.
  13.         Email: Phr0z3n.Dev@Gmail.com
  14.   --------------------------------------------------------------------------------------------------
  15.   ==================================================================================================*/
  16.  
  17. /**
  18.  * Activating either of the following directives will determine which environment (Windows or POSIX)
  19.  * the program will function on... flawlessly.
  20.  **/
  21. #define _MY_ENV _WINDOWS
  22. //#define   _MY_ENV _NIX
  23.  
  24.  
  25. #include <iostream>
  26. #include <fstream>
  27. #include <string>
  28.  
  29. /**
  30.  * It is sections like these that justify the previous act...
  31.  **/
  32. #if _MY_ENV == _WINDOWS
  33.     #include <windows.h>
  34.  
  35.     #define CSBI    CONSOLE_SCREEN_BUFFER_INFO
  36. #elif _MY_ENV == _NIX
  37.     #include <cstdlib>
  38.     #include <termios.h>
  39.     #include <unistd.h>
  40.     #include <term.h>
  41. #endif
  42.  
  43. #include "simpleCrypto.hpp"
  44.  
  45. /*===============================================--=================================================
  46.                                         THE MAIN FUNCTION
  47.   --------------------------------------------------------------------------------------------------*/
  48. int main(void)
  49. {
  50.     /**
  51.      * The instantiation of the CCrypt0 class.  'The books' compellingly state, "pointers are used to
  52.      * reduce pushing and popping on the stack (aka 'stack labour')."  (Ultimately, eventually, being
  53.      * that pointers are allocated on 'the heap').
  54.      *
  55.      * Other books also fundamentally urged that, "pointers are your friend."  Semantically, owing to
  56.      * this sole concept, pay attention to the cleanness of the code (the class was instantiated once,
  57.      * and only once).
  58.      **/
  59.     CCrypt0 *ccrypto(new CCrypt0());
  60.  
  61.         if(ccrypto->mainMenu() == 0)
  62.         {
  63.             /**
  64.              * Tidy coding techniques is one of the most fundamental paradigms that separates (good)
  65.              * programmers from (crappy) ones.
  66.              *
  67.              * NOTE:
  68.              * There are coders that fail to adhere to this 'thumb rule' only because their belief
  69.              * systems revolve around the concept that 'only the stack needs cleaning up' before
  70.              * program termination.  If only there was only 'stack overflow'...
  71.              **/
  72.             delete ccrypto;
  73.  
  74.             exit(EXIT_SUCCESS);
  75.         }
  76.  
  77.     delete ccrypto;
  78.  
  79.     return 0;
  80. }
  81.  
  82. /*===============================================--=================================================
  83.                                         THE MAIN MENU FUNCTION
  84.   --------------------------------------------------------------------------------------------------*/
  85. int CCrypt0::mainMenu(void)
  86. {
  87.     std::string *mainMenuOptBuffer(new std::string); // Why 'string' and not 'char'?
  88.  
  89.         /**
  90.          * std::cin.get() (not pertaining to certain overloaded versions), accepts (and store) only one
  91.          * character at a time (ONLY when ENTER is pressed).  This bit of logic does not hinder the fact
  92.          * that more than one characters can still be entered at an std::cin.get() prompt.
  93.          *
  94.          * In its true sense, expecting a '1' menu option and even accepting a '1abc' option instead,
  95.          * is in no way, logical.  std::cin.getline() will store all options entered, and the relative
  96.          * logic implemented, will evaluate and differentiate between '1' and '1abc'
  97.          * (&| an option of length 1 || > 1).
  98.          *
  99.          * It is the latter-mentioned logic (among other radical coding conventions), which is actually
  100.          * implemented in this code, that makes this program foolproof.
  101.          **/
  102.         while(mainMenuOptBuffer->length() != 1)
  103.         {
  104.             /**
  105.              * Console menu recursion without logic to clear the screen is redundantly messy.
  106.              * No pun intended.
  107.              **/
  108.             CCrypt0::clearScreen();
  109.  
  110.             std::cout << "SELECT METHOD (1-4)" << std::endl << std::endl;
  111.             std::cout << "[ENCRYPT]" << std::endl;
  112.             std::cout << "1. " << "Text" << std::endl;
  113.             std::cout << "2. " << "File Text" << std::endl << std::endl;
  114.  
  115.             std::cout << "[DECRYPT]" << std::endl;
  116.             std::cout << "3. " << "Text" << std::endl;
  117.             std::cout << "4. " << "File Text" << std::endl << std::endl;
  118.  
  119.             std::cout << "5. " << "Exit" << std::endl << std::endl;
  120.             std::cout << ">: ";
  121.  
  122.             std::getline(std::cin, *mainMenuOptBuffer, '\n');
  123.         }
  124.  
  125.     CCrypt0::setMainMenuOpt(mainMenuOptBuffer->data()[0]);
  126.  
  127.     delete mainMenuOptBuffer;
  128.  
  129.         switch(CCrypt0::getMainMenuOpt())
  130.         {
  131.             case '1':
  132.             case '3':
  133.                 CCrypt0::encryptDecryptText();
  134.             case '2':
  135.             case '4':
  136.                 CCrypt0::encryptDecryptFileText();
  137.             case '5':
  138.                 return 0;
  139.             default:
  140.                 main();
  141.         }
  142.  
  143.     return 0;
  144. }
  145.  
  146. /*===============================================--=================================================
  147.                                     THE ENCRYPT/DECRYPT TEXT FUNCTION
  148.   --------------------------------------------------------------------------------------------------*/
  149. void CCrypt0::encryptDecryptText(void)
  150. {
  151.     std::string *_cryptBuffer(new std::string);
  152.  
  153.         while(_cryptBuffer->length() == 0)
  154.         {
  155.             CCrypt0::clearScreen();
  156.  
  157.                 switch(CCrypt0::getMainMenuOpt())
  158.                 {
  159.                     case '1':
  160.                         std::cout << "[ENCRYPT TEXT]" << std::endl << std::endl;
  161.                         break;
  162.                     case '3':
  163.                         std::cout << "[DECRYPT TEXT]" << std::endl << std::endl;
  164.                         break;
  165.                 }
  166.  
  167.             std::cout << "Enter The Text:" << std::endl;
  168.  
  169.             std::getline(std::cin, *_cryptBuffer, '\n');
  170.         }
  171.  
  172.     CCrypt0::setCryptBuffer(_cryptBuffer);
  173.  
  174.     delete _cryptBuffer;
  175.  
  176.         switch(CCrypt0::getMainMenuOpt())
  177.         {
  178.             case '1':
  179.                 CCrypt0::doEncrypt();
  180.                 break;
  181.             case '3':
  182.                 CCrypt0::doDecrypt();
  183.                 break;
  184.         }
  185.  
  186.     CCrypt0::outputMenu();
  187. }
  188. void CCrypt0::encryptDecryptFileText(void)
  189. {
  190.     CCrypt0::fileNameEntry(_FILE_READ);
  191.  
  192.     /**
  193.      * READ FILE CONTENTS...
  194.      **/
  195.     CCrypt0::setFileFlag(_FILE_READ_FLAG);
  196.     CCrypt0::readWriteText();
  197.  
  198.         /**
  199.          * ENCRYPT/DECRYPT FILE CONTENTS...
  200.          **/
  201.         switch(CCrypt0::getMainMenuOpt())
  202.         {
  203.             case '2':
  204.                 CCrypt0::doEncrypt();
  205.                 break;
  206.             case '4':
  207.                 CCrypt0::doDecrypt();
  208.                 break;
  209.         }
  210.  
  211.     CCrypt0::outputMenu();
  212. }
  213.  
  214. /*===============================================--=================================================
  215.                                         THE OUTPUT MENU FUNCTION
  216.   --------------------------------------------------------------------------------------------------*/
  217. void CCrypt0::outputMenu(void)
  218. {
  219.     std::string *outputMenuOptBuffer(new std::string);
  220.  
  221.         while(outputMenuOptBuffer->length() != 1)
  222.         {
  223.             CCrypt0::clearScreen();
  224.  
  225.                 switch(CCrypt0::getMainMenuOpt())
  226.                 {
  227.                     case '1':
  228.                         std::cout << "Text successfully encrypted..." << std::endl;
  229.                         break;
  230.                     case '2':
  231.                         std::cout << "File text successfully encrypted..." << std::endl;
  232.                         break;
  233.                     case '3':
  234.                         std::cout << "Text successfully decrypted..." << std::endl;
  235.                         break;
  236.                     case '4':
  237.                         std::cout << "File text successfully decrypted..." << std::endl;
  238.                         break;
  239.                 }
  240.  
  241.             std::cout << std::endl;
  242.             std::cout << "[OUTPUT METHOD]" << std::endl;
  243.             std::cout << "1. " << "Text" << std::endl;
  244.             std::cout << "2. " << "File" << std::endl;
  245.             std::cout << "3. " << "Text & File" << std::endl;
  246.             std::cout << "4. " << "Discard" << std::endl << std::endl;
  247.             std::cout << ">: ";
  248.  
  249.             std::getline(std::cin, *outputMenuOptBuffer, '\n');
  250.         }
  251.  
  252.         switch(outputMenuOptBuffer->data()[0])
  253.         {
  254.             case '1':
  255.                 CCrypt0::displayText();
  256.                 break;
  257.             case '2':
  258.                 CCrypt0::fileNameEntry(_FILE_WRITE);
  259.                 CCrypt0::setFileFlag(_FILE_WRITE_FLAG);
  260.                 CCrypt0::readWriteText();
  261.                 CCrypt0::clearScreen();
  262.                 std::cout << '\'' << CCrypt0::getFileName() << '\'' << " successfully saved." << std::endl;
  263.                 break;
  264.             case '3':
  265.                 CCrypt0::fileNameEntry(_FILE_WRITE);
  266.                 CCrypt0::setFileFlag(_FILE_WRITE_FLAG);
  267.                 CCrypt0::readWriteText();
  268.                 CCrypt0::displayText();
  269.                 std::cout << '\'' << CCrypt0::getFileName() << '\'' << " successfully saved." << std::endl;
  270.                 break;
  271.             case '4':
  272.                 CCrypt0::setCryptBuffer(new std::string(""));
  273.                 delete outputMenuOptBuffer;
  274.                 main();
  275.         }
  276.  
  277.     delete outputMenuOptBuffer;
  278.  
  279.     std::cout << "Press ENTER to continue...";
  280.  
  281.     /**
  282.      * Tricky stuff.  Definitely don't take this with a grain of salt.
  283.      **/
  284.     SetStdinEcho(false);
  285.     std::cin.get();
  286.     SetStdinEcho(true);
  287.  
  288.     main();
  289. }
  290.  
  291. /*===============================================--=================================================
  292.                                         THE DISPLAY TEXT FUNCTION
  293.   --------------------------------------------------------------------------------------------------*/
  294. void CCrypt0::displayText(void)
  295. {
  296.     CCrypt0::clearScreen();
  297.  
  298.         switch(CCrypt0::getMainMenuOpt())
  299.         {
  300.             case '1':
  301.                 std::cout << "[ENCRYPTED TEXT]";
  302.                 break;
  303.             case '2':
  304.                 std::cout << "[ENCRYPTED FILE TEXT]";
  305.                 break;
  306.             case '3':
  307.                 std::cout << "[DECRYPTED TEXT]";
  308.                 break;
  309.             case '4':
  310.                 std::cout << "[DECRYPTED FILE TEXT]";
  311.                 break;
  312.         }
  313.  
  314.     std::cout << std::endl << std::endl << CCrypt0::getCryptBuffer() << std::endl << std::endl;
  315. }
  316.  
  317. /*===============================================--=================================================
  318.                                     THE TEXT ENCRYPTION FUNCTION
  319.   --------------------------------------------------------------------------------------------------*/
  320. inline void CCrypt0::doEncrypt(void)
  321. {
  322.     /**
  323.      * This function does a nifty little thing.  It changes a variable passed into it using a pointer.
  324.      * Plus no return value is utilized.  The code is technically implemented in the function that calls it.
  325.      * Its only just I do it justice with the 'inline' keyword.
  326.      **/
  327.  
  328.     /**
  329.      * NOTE:
  330.      * Elite coding techniques entail implementing 'cholesterol-free' code that produce un-bloated apps.
  331.      * With a known min/max range (0-255), over-sized variables types would be inefficient.
  332.      **/
  333.     static  unsigned    char    testVar;
  334.     static  unsigned    char    cryptVar;
  335.  
  336.     std::string *_cryptBuffer(new std::string);
  337.  
  338.     *_cryptBuffer = CCrypt0::getCryptBuffer();
  339.  
  340.     for(unsigned short bufferPos = 0; _cryptBuffer->data()[bufferPos] != '\0' ; bufferPos++)
  341.         /**
  342.          * NOTE:
  343.          * This is a 'very interesting' loop where the re-initialization of both variables utilized
  344.          * (upon each iteration), is paramount to the code functioning.
  345.          **/
  346.         for(testVar = 32, cryptVar = 255; testVar < 127; testVar++, cryptVar--)
  347.             /**
  348.              * NOTE:
  349.              * Based on experience, I have managed to draw one particular conclusion: "C++ can be a
  350.              * very 'peculiar' language", hence, the type of casting used.
  351.              **/
  352.             if(_cryptBuffer->data()[bufferPos] == static_cast<char>(testVar))
  353.                 const_cast<char *>(_cryptBuffer->data())[bufferPos] = static_cast<char>(cryptVar);
  354.  
  355.     CCrypt0::setCryptBuffer(_cryptBuffer);
  356.  
  357.     delete _cryptBuffer;
  358.  
  359.     /**
  360.      * NOTE:
  361.      * The algorithm implemented in this process is similar to the one implemented in the MySQL MD5
  362.      * encryption function.
  363.      **/
  364. }
  365.  
  366. /*===============================================--=================================================
  367.                                     THE TEXT DECRYPTION FUNCTION
  368.   --------------------------------------------------------------------------------------------------*/
  369. inline void CCrypt0::doDecrypt(void)
  370. {
  371.     static  unsigned    char    testVar;
  372.     static  unsigned    char    cryptVar;
  373.  
  374.     std::string *_cryptBuffer(new std::string);
  375.  
  376.     *_cryptBuffer = CCrypt0::getCryptBuffer();
  377.  
  378.     /**
  379.      * NOTE:
  380.      * Like key verification, if the algorithms are not 'oppositely similar' (;)), the decrypted text
  381.      * will be wrong.
  382.      **/
  383.     for(unsigned short bufferPos = 0; _cryptBuffer->data()[bufferPos] != '\0' ; bufferPos++)
  384.         for(testVar = 32, cryptVar = 255; testVar < 127; testVar++, cryptVar--)
  385.             if(_cryptBuffer->data()[bufferPos] == static_cast<char>(cryptVar))
  386.                 const_cast<char *>(_cryptBuffer->data())[bufferPos] = static_cast<char>(testVar);
  387.  
  388.     CCrypt0::setCryptBuffer(_cryptBuffer);
  389.  
  390.     delete _cryptBuffer;
  391. }
  392.  
  393. /*===============================================--=================================================
  394.                                     THE FILENAME ENTRY FUNCTION
  395.   --------------------------------------------------------------------------------------------------*/
  396. inline void CCrypt0::fileNameEntry(char fnEntryType)
  397. {
  398.     std::string *_fileNameBuffer(new std::string);
  399.  
  400.         /**
  401.          * FILE NAME ENTRY...
  402.          **/
  403.         while(_fileNameBuffer->length() == 0)
  404.         {
  405.             CCrypt0::clearScreen();
  406.  
  407.             if(fnEntryType == _FILE_READ)
  408.                 switch(CCrypt0::getMainMenuOpt())
  409.                 {
  410.                     case '2':
  411.                         std::cout << "[ENCRYPT FILE TEXT]" << std::endl << std::endl;
  412.                         break;
  413.                     case '4':
  414.                         std::cout << "[DECRYPT FILE TEXT]" << std::endl << std::endl;
  415.                         break;
  416.                 }
  417.             else if(fnEntryType == _FILE_WRITE)
  418.                 std::cout << "[SAVE FILE]" << std::endl << std::endl;
  419.  
  420.             std::cout << "Enter The File Name (eg: file.txt):" << std::endl;
  421.             std::cout << ">: ";
  422.  
  423.             std::getline(std::cin, *_fileNameBuffer, '\n');
  424.         }
  425.  
  426.     std::cout << std::endl;
  427.  
  428.     CCrypt0::setFileName(_fileNameBuffer);
  429.  
  430.     delete _fileNameBuffer;
  431. }
  432.  
  433. /*===============================================--=================================================
  434.                                     THE TEXT FILE READ/WRITE FUNCTION
  435.   --------------------------------------------------------------------------------------------------*/
  436. void CCrypt0::readWriteText(void)
  437. {
  438.     std::filebuf    *fileBuffer(new std::filebuf);
  439.     std::ostream    writeFile(fileBuffer);
  440.     std::istream    readFile(fileBuffer);
  441.  
  442.         switch(CCrypt0::getFileFlag())
  443.         {
  444.             case _FILE_READ_FLAG:
  445.  
  446.                 fileBuffer->open(CCrypt0::getFileName().data(), CCrypt0::getFileFlag()); //try 1
  447.  
  448.                 /**
  449.                  * 'The books' state a lot of things.  Another highly regarded one (stopped short of)
  450.                  * utterly despising exception handling, in C++, in the try/catch formation, with sound
  451.                  * logic: the technique encourages lazy coding implementations, which lead to the program
  452.                  * attempting to do too much, with too much (catch(...) for instance), and an end product
  453.                  * with (possibly) more holes with than without, or a 'cure is worse than the disease'
  454.                  * scenario.
  455.                  *
  456.                  * I decided to implement my own overly extensive error-handling techniques... and my
  457.                  * code works still.
  458.                  **/
  459.                 if(fileBuffer->is_open())
  460.                 {
  461.                     /**
  462.                      * I have discovered the use of the block to overcome those little annoying coding
  463.                      * hurdles, such as declaring a 'once-used' variable, anywhere but the top of your
  464.                      * code, in C.
  465.                      *
  466.                      * In this case, declaring an std::string type after an std::ios_base::opmode type
  467.                      * (among others), without the use of a block (the 'if-else' one, in this case),
  468.                      * causes violent compiler panic...
  469.                      * ...for whatever reason(s)...
  470.                      **/
  471.                     std::string *_cryptBuffer(new std::string);
  472.  
  473.                     /**
  474.                      * Go to the beginning of the file.
  475.                      **/
  476.                     readFile.seekg(0);
  477.  
  478.                         /**
  479.                          * Read the file (one character at a time): old school.
  480.                          *
  481.                          * NOTE:
  482.                          * This method is perfect in more than one ways.  One scenario is where a mixture
  483.                          * of Unicode and non-Unicode characters for instance, need to be inspected (and
  484.                          * manipulated (replaced, for example)), on-the-fly.
  485.                          **/
  486.                         for(char readChar = '\0'; readFile.get(readChar);) //try 2
  487.                             _cryptBuffer->append(1, readChar);
  488.  
  489.                         if(_cryptBuffer->length() == 0)//catch 2, etc.
  490.                         {
  491.                             fileBuffer->close();
  492.  
  493.                             CCrypt0::setFileWarningError(2);
  494.                             CCrypt0::printFileWarningError();
  495.                         }
  496.  
  497.                     CCrypt0::setCryptBuffer(_cryptBuffer);
  498.  
  499.                     delete _cryptBuffer;
  500.                 }
  501.                 else //catch 1
  502.                 {
  503.                     CCrypt0::setFileWarningError(1);
  504.                     CCrypt0::printFileWarningError();
  505.                 }
  506.                 break;
  507.  
  508.             case _FILE_WRITE_FLAG:
  509.  
  510.                 /**
  511.                  * Test if the file specified already exists...
  512.                  **/
  513.                 fileBuffer->open(CCrypt0::getFileName().data(), _FILE_READ_FLAG);
  514.  
  515.                     /**
  516.                      * If so, prompt for relative actions...
  517.                      **/
  518.                     if(fileBuffer->is_open())
  519.                     {
  520.                         fileBuffer->close();
  521.  
  522.                         CCrypt0::setFileWarningError(10);
  523.                         CCrypt0::printFileWarningError();
  524.                     }
  525.  
  526.  
  527.                 /**
  528.                  * If not, try creating a new file to write to...
  529.                  **/
  530.                 fileBuffer->open(CCrypt0::getFileName().data(), CCrypt0::getFileFlag());
  531.  
  532.                     /**
  533.                      * If success, attempt file write...
  534.                      **/
  535.                     if(fileBuffer->is_open())
  536.                     {
  537.                         writeFile << CCrypt0::getCryptBuffer();
  538.  
  539.                             /**
  540.                              * If write fails, alert accordingly...
  541.                              **/
  542.                             if(writeFile.fail())
  543.                             {
  544.                                 fileBuffer->close();
  545.  
  546.                                 CCrypt0::setFileWarningError(4);
  547.                                 CCrypt0::printFileWarningError();
  548.                             }
  549.                     }
  550.                     else
  551.                     {
  552.                         CCrypt0::setFileWarningError(3);
  553.                         CCrypt0::printFileWarningError();
  554.                     }
  555.                 break;
  556.         }
  557.  
  558.     fileBuffer->close();
  559. }
  560.  
  561. /*===============================================--=================================================
  562.                                     THE FILE WARNING/ERROR FUNCTION
  563.   --------------------------------------------------------------------------------------------------*/
  564. void CCrypt0::printFileWarningError(void)
  565. {
  566.     if(CCrypt0::getFileWarningError() == 1 || CCrypt0::getFileWarningError() == 2)
  567.     {
  568.         CCrypt0::clearScreen();
  569.  
  570.         switch(CCrypt0::getFileWarningError())
  571.         {
  572.             case 1:
  573.                 std::cout << "[FILE OPEN ERROR!]" << std::endl << std::endl;
  574.                 std::cout << "Verify that '" << CCrypt0::getFileName() << "' exists in the path specified," << std::endl;
  575.                 break;
  576.             case 2:
  577.                 std::cout << "[FILE READ ERROR!]" << std::endl << std::endl;
  578.  
  579.                     switch(CCrypt0::getMainMenuOpt())
  580.                     {
  581.                         case 2:
  582.                             std::cout << "Nothing to encrypt." << std::endl;
  583.                             break;
  584.                         case 4:
  585.                             std::cout << "Nothing to decrypt." << std::endl;
  586.                             break;
  587.                     }
  588.  
  589.                 std::cout << "Verify that '" << CCrypt0::getFileName() << "' contains text," << std::endl;
  590.                 break;
  591.         }
  592.  
  593.         std::cout << "and/or is not being used by another process." << std::endl << std::endl;
  594.         std::cout << "Press any key to continue...";
  595.  
  596.         std::cin.get();
  597.         main();
  598.     }
  599.     else if(CCrypt0::getFileWarningError() == 3 || CCrypt0::getFileWarningError() == 4)
  600.     {
  601.         std::string *fileWarningMenuOptBuffer(new std::string);
  602.  
  603.             while(fileWarningMenuOptBuffer->length() != 1)
  604.             {
  605.                 CCrypt0::clearScreen();
  606.  
  607.                     switch(CCrypt0::getFileWarningError())
  608.                     {
  609.                         case 3:
  610.                             std::cout << "[FILE CREATION ERROR!]" << std::endl << std::endl;
  611.                             std::cout << "Error creating '"  << CCrypt0::getFileName() << "' for saving." << std::endl << std::endl;
  612.                             break;
  613.                         case 4:
  614.                             std::cout << "[FILE SAVE ERROR!]" << std::endl << std::endl;
  615.                             std::cout << "Error saving to '"  << CCrypt0::getFileName() << "'." << std::endl << std::endl;
  616.                             break;
  617.                     }
  618.  
  619.                 std::cout << "[OPTION]" << std::endl;
  620.                 std::cout << "1. " << "Output Menu" << std::endl;
  621.                 std::cout << "2. " << "Main Menu" << std::endl << std::endl;
  622.                 std::cout << ">: ";
  623.  
  624.                 std::getline(std::cin, *fileWarningMenuOptBuffer, '\n');
  625.             }
  626.  
  627.             switch(fileWarningMenuOptBuffer->data()[0])
  628.             {
  629.                 case '1':
  630.                     CCrypt0::outputMenu();
  631.                     return;
  632.                 case '2':
  633.                     CCrypt0::setCryptBuffer(new std::string);
  634.                     delete fileWarningMenuOptBuffer;
  635.                     main();
  636.             }
  637.  
  638.     }
  639.     else if(CCrypt0::getFileWarningError() == 10)
  640.     {
  641.         std::string *fileWarningMenuOptBuffer(new std::string);
  642.  
  643.             while(fileWarningMenuOptBuffer->length() != 1)
  644.             {
  645.                 CCrypt0::clearScreen();
  646.  
  647.                 std::cout << "[FILE WARNING!]" << std::endl << std::endl;
  648.                 std::cout << '\'' << CCrypt0::getFileName() << "' already exists in the path specified:" << std::endl << std::endl;
  649.  
  650.                 std::cout << "[OPTION]" << std::endl;
  651.                 std::cout << "1. " << "Overwrite File" << std::endl;
  652.                 std::cout << "2. " << "Output Menu" << std::endl;
  653.                 std::cout << "3. " << "Main Menu" << std::endl << std::endl;
  654.                 std::cout << ">: ";
  655.  
  656.                 std::getline(std::cin, *fileWarningMenuOptBuffer, '\n');
  657.             }
  658.  
  659.             switch(fileWarningMenuOptBuffer->data()[0])
  660.             {
  661.                 case '1':
  662.                     delete fileWarningMenuOptBuffer;
  663.                     return;
  664.                 case '2':
  665.                     delete fileWarningMenuOptBuffer;
  666.                     CCrypt0::outputMenu();
  667.                 case '3':
  668.                     CCrypt0::setCryptBuffer(new std::string);
  669.                     delete fileWarningMenuOptBuffer;
  670.                     main();
  671.             }
  672.     }
  673. }
  674.  
  675. /*===============================================--=================================================
  676.                                     THE CLEAR SCREEN FUNCTION (REUSABLE)
  677.   --------------------------------------------------------------------------------------------------*/
  678. void CCrypt0::clearScreen(void)
  679. {
  680. #if _MY_ENV == _WINDOWS
  681.     /**
  682.      * NOTE:
  683.      * This particular function is a nifty little snippet anyone can use, with minimal alterations
  684.      * (function declaration), to clear the console screen on both Windows and POSIX environments,
  685.      * without having to depend on the predefined non-standard ones in header files that accompany
  686.      * particular IDEs.  All that is required is the inclusion of the 'windows.h' header file
  687.      * (for Windows) or some other relative few for POSIX.
  688.      **/
  689.     COORD   coordScreen = {0, 0}; /* Home for the cursor. */
  690.     DWORD   cCharsWritten;
  691.  
  692.     /**
  693.      * NOTE:
  694.      * CSBI is originally defined as CONSOLE_SCREEN_BUFFER_INFO in (windows.h) 'wincon.h' but I suspected
  695.      * that the abbreviated redefinition (in 'simpleCrypto.hpp') would make the code neater.
  696.      **/
  697.     CSBI    csbi;
  698.     DWORD   dwConSize;
  699.     HANDLE  hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  700.  
  701.     /* Get the number of character cells in the current buffer. */
  702.     if(!GetConsoleScreenBufferInfo(hConsoleOut, &csbi))
  703.         return;
  704.  
  705.     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  706.  
  707.     /* Fill the entire screen with blanks. */
  708.     if(!FillConsoleOutputCharacter(hConsoleOut, ' ', dwConSize, coordScreen, &cCharsWritten))
  709.         return;
  710.  
  711.     /* Get the current text attribute. */
  712.     if(!GetConsoleScreenBufferInfo(hConsoleOut, &csbi))
  713.         return;
  714.  
  715.     /* Set the buffer's attributes accordingly. */
  716.     if(!FillConsoleOutputAttribute(hConsoleOut, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
  717.         return;
  718.  
  719.     /* Put the cursor at its home coordinates. */
  720.     SetConsoleCursorPosition(hConsoleOut, coordScreen);
  721. #elif _MY_ENV == _NIX
  722.         if(!cur_term)
  723.         {
  724.             int result;
  725.  
  726.             setupterm(NULL, STDOUT_FILENO, &result);
  727.  
  728.             if(result <= 0) return;
  729.         }
  730.  
  731.     putp(tigetstr("clear"));
  732. #endif
  733. }
  734.  
  735. /*===============================================--=================================================
  736.                             THE STDIN ECHO ENABLE/DISABLE FUNCTION (REUSABLE)
  737.   --------------------------------------------------------------------------------------------------*/
  738. void CCrypt0::SetStdinEcho(bool enable = true)
  739. {
  740.     /**
  741.      * This function is used to 'pull a rabbit out of the hat', in order to emulate non-standard C
  742.      * functionalities in C++.
  743.      **/
  744. #if _MY_ENV == _WINDOWS
  745.     HANDLE  hStdin = GetStdHandle(STD_INPUT_HANDLE);
  746.     DWORD   mode;
  747.  
  748.     GetConsoleMode(hStdin, &mode);
  749.  
  750.         if(!enable)
  751.             mode &= ~ENABLE_ECHO_INPUT;
  752.         else
  753.             mode |= ENABLE_ECHO_INPUT;
  754.  
  755.     SetConsoleMode(hStdin, mode);
  756. #elif _MY_ENV == _NIX
  757.     struct  termios tty;
  758.  
  759.     tcgetattr(STDIN_FILENO, &tty);
  760.  
  761.         if(!enable)
  762.             tty.c_lflag &= ~ECHO;
  763.         else
  764.             tty.c_lflag |= ECHO;
  765.  
  766.     (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty);
  767. #endif
  768. }
  769.  
  770. /*===============================================--=================================================
  771.                                         THE CCRYPT0 CONSTRUCTOR
  772.   --------------------------------------------------------------------------------------------------*/
  773. CCrypt0::CCrypt0(void)
  774. : mainMenuOpt('\0'),
  775.   fileName(""),
  776.   fileWarningError(0),
  777.   cryptBuffer("")
  778. {
  779.     /**
  780.      * Class variables are initialized to a 'clean' state.  This is superior programming practice.
  781.      * Note that most variables are initialized to 'nothing' upon creation while some are not.
  782.      * Its always good practice to look over your shoulders in programming, being that there are
  783.      * always malicious people around who are aching to exploit your code.
  784.      **/
  785.  
  786. }
  787.  
  788. /*===============================================--=================================================
  789.                                         THE CCRYPT0 DESTRUCTOR
  790.   --------------------------------------------------------------------------------------------------*/
  791. CCrypt0::~CCrypt0()
  792. {
  793.     /**
  794.      * Notice how the destructor of the class performs the same tasks as its constructor counterpart.
  795.      * Erasing what was stored in memory should be an utmost priority when exiting a program.
  796.      * In cases where faulty coding leads to memory leaks, you do not want such a scenario to apply
  797.      * to a mission-critical password/database-utilising program, for instance.
  798.      **/
  799.     CCrypt0::setMainMenuOpt('\0');
  800.     CCrypt0::setFileName(new std::string);
  801.     CCrypt0::setFileWarningError(0);
  802.     CCrypt0::setCryptBuffer(new std::string);
  803. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement