Advertisement
Boucherwayne78

Bouch's Paste

Dec 7th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. // Wayne Geissinger, MP4, Encoder/Decoder, 12/5/16
  2. //This program takes in four parameters upon startup.
  3. //argv one through four are as follows:
  4. //1) flag to encipher or decipher 2) keyword
  5. //3) input file name              4) output file name
  6. //Program enciphers or deciphers text by pulling from
  7. //input file and dumping into output file.
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <sstream>
  13.  
  14. using namespace std;
  15.  
  16. //function prototypes
  17. char encipher(char key, char plain);
  18. char decipher(char key, char cipher);
  19. void pressEnterToContinue(void);
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.     //declarations
  24.     string keyWord = string(argv[2]);
  25.     char plainChar;
  26.     char cipherChar;
  27.     string inputFileName = argv[3];
  28.     string outputFileName = argv[4];
  29.  
  30.     //opening input and output streams for files
  31.     ifstream inStream;
  32.     inStream.open(inputFileName);
  33.     ofstream outStream;
  34.     outStream.open(outputFileName);
  35.  
  36.     //checks if the "encode" flag (-e) is used
  37.     if (argv[1][0] == '-' && argv[1][1] == 'e')
  38.     {
  39.         //repeats drawing new character, enciphering it, and dumping to output file.
  40.         int i = 0;
  41.         while (true)
  42.         {
  43.             i++;
  44.             if (i == keyWord.length()) i = 0;
  45.             inStream.get(plainChar);
  46.             if (inStream.eof()) break;
  47.             char ch = encipher(keyWord[i], plainChar);
  48.             outStream << ch;
  49.         }
  50.     }
  51.  
  52.  
  53.     //checks if the "decode" flag (-d) is used
  54.     else if (argv[1][0] == '-' && argv[1][1] == 'd')
  55.     {
  56.         //repeats drawing new character, enciphering it, and dumping to output file.
  57.         int i = 0;
  58.         while (true)
  59.         {
  60.             i++;
  61.             if (i == keyWord.length()) i = 0;
  62.             inStream.get(cipherChar);
  63.             if (inStream.eof()) break;
  64.             char ch = decipher(keyWord[i], cipherChar);
  65.             outStream << ch;
  66.         }
  67.     }
  68.  
  69.     //if neither flag is used, throws incorrect input.
  70.     else cout << "incorrect flag input. " << endl;
  71.  
  72.     //closing program
  73.     inStream.close();
  74.     outStream.close();
  75.     cout << "Done!" << endl;
  76.     pressEnterToContinue();
  77. }
  78.  
  79. //Encipheres a character with a key and plain text.
  80. //preconditions: char key, char plain
  81. //postconditions: cipher
  82. char encipher(char key, char plain)
  83. {
  84.     char cipher;
  85.     int offset = ' ';
  86.     int alphabet = 95; // The ASCII printable alphabet is 95 characters.
  87.     cipher = (((plain - offset) + (key - offset)) % alphabet) + offset;
  88.     return cipher;
  89. }
  90.  
  91. //Deciphers an enciphered character with a key and enciphered text.
  92. //preconditions: char key, char cipher (enciphered text)
  93. //postconditions: char plain (deciphered text)
  94. char decipher(char key, char cipher)
  95. {
  96.     char plain;
  97.     int offset = ' ';
  98.     int alphabet = 95; // The ASCII printable alphabet is 95 characters.
  99.     plain = ((cipher - offset) - (key - offset)) + offset;
  100.     if (plain < 0) plain += alphabet;
  101.     return plain;
  102. }
  103.  
  104. //Ends program, no pre/post conditions.
  105. void pressEnterToContinue(void)
  106. {
  107.     cout << "Press enter to continue... ";
  108.     cin.ignore(1024, '/n');
  109.     cin.clear();
  110.     exit(1);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement