Advertisement
Boucherwayne78

Bouch's Paste

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