Advertisement
Boucherwayne78

Bouch's Paste

Dec 7th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 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 - offset) + (key - offset)) % alphabet) + offset;
  82.     return cipher;
  83. }
  84.  
  85. //Deciphers an enciphered character with a key and enciphered text.
  86. //preconditions: char key, char cipher (enciphered text)
  87. //postconditions: char plain (deciphered text)
  88. char decipher(char key, char cipher)
  89. {
  90.     char plain;
  91.     int offset = ' ';
  92.     int alphabet = 95;
  93.     plain = (((cipher - offset) - (key - offset)) % alphabet) + offset;
  94.     return plain;
  95. }
  96.  
  97. //Ends program, no pre/post conditions.
  98. void pressEnterToContinue(void)
  99. {
  100.     cout << "Press enter to continue... ";
  101.     cin.ignore(1024, '/n');
  102.     cin.clear();
  103.     exit(1);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement