Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Wayne Geissinger, MP4, Encoder/Decoder, 12/5/16
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- using namespace std;
- //function prototypes
- char encipher(char key, char plain);
- char decipher(char key, char cipher);
- void pressEnterToContinue(void);
- int main(int argc, char *argv[])
- {
- //declarations
- string keyWord = string(argv[2]);
- char plainChar;
- char cipherChar;
- string inputFileName = argv[3];
- string outputFileName = argv[4];
- //opening input and output streams for files
- ifstream inStream;
- inStream.open(inputFileName);
- ofstream outStream;
- outStream.open(outputFileName);
- //checks if the "encode" flag (-e) is used
- if (argv[1][0] == '-' && argv[1][1] == 'e')
- {
- //repeats drawing new character, enciphering it, and dumping to output file.
- int i = 0;
- while(true)
- {
- i++;
- if (i == keyWord.length()) i = 0;
- inStream.get(plainChar);
- if (inStream.eof()) break;
- char ch = encipher(keyWord[i], plainChar);
- outStream << ch;
- }
- }
- //checks if the "decode" flag (-d) is used
- else if (argv[1][0] == '-' && argv[1][1] == 'd')
- {
- //repeats drawing new character, enciphering it, and dumping to output file.
- int i = 0;
- while(true)
- {
- i++;
- if (i == keyWord.length()) i = 0;
- inStream.get(cipherChar);
- if (inStream.eof()) break;
- char ch = decipher(keyWord[i], cipherChar);
- outStream << ch;
- }
- }
- //if neither flag is used, throws incorrect input.
- else cout << "incorrect flag input. " << endl;
- //closing program
- inStream.close();
- outStream.close();
- cout << "Done!" << endl;
- pressEnterToContinue();
- }
- //Encipheres a character with a key and plain text.
- //preconditions: char key, char plain
- //postconditions: cipher
- char encipher(char key, char plain)
- {
- char cipher;
- int offset = ' ';
- int alphabet = 95; // The ASCII printable alphabet is 95 characters.
- cipher = (((plain - offset) + (key - offset)) % alphabet) + offset;
- return cipher;
- }
- //Deciphers an enciphered character with a key and enciphered text.
- //preconditions: char key, char cipher (enciphered text)
- //postconditions: char plain (deciphered text)
- char decipher(char key, char cipher)
- {
- char plain;
- int offset = ' ';
- int alphabet = 95; // The ASCII printable alphabet is 95 characters.
- plain = (((cipher - offset) - (key - offset)) % alphabet) + offset;
- return plain;
- }
- //Ends program, no pre/post conditions.
- void pressEnterToContinue(void)
- {
- cout << "Press enter to continue... ";
- cin.ignore(1024, '/n');
- cin.clear();
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement