Skip to content

Instantly share code, notes, and snippets.

@sejr
Created April 15, 2014 18:41
Show Gist options
  • Select an option

  • Save sejr/10757679 to your computer and use it in GitHub Desktop.

Select an option

Save sejr/10757679 to your computer and use it in GitHub Desktop.
This program utilizes the command line to encrypt / decrypt text files using the Vigenëre cipher - commonly referred to as the Caesar cipher.
// caesar.cpp ~ 6 December 2012
// SAMUEL ROTH ~ ECCS 1611
// This program utilizes the command line to encrypt / decrypt text files using
// the VigenËre cipher - commonly referred to as the Caesar cipher.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// FUNCTION PROTOTYPE //
void pressEnterToContinue ( void );
// Functions pertaining to the ciphering process are held within this class.
class Vigenere {
public:
// Calling for the keyword that will be used to encrypt/decrypt.
string key;
// Vingenere( string key )
// Input: the keyword for the cipher, as submitted by the user.
// Output: A 'standardized' keyword, so it can work with ASCII operations.
// Notes: This program doesn't interact with the user. It simplifies the
// operation (seen below) of accurately encrypting and decrypting
// the text file in question.
Vigenere( string key ) {
for( int i = 0; i < key.size(); ++i ) {
if( key[i] >= 'A' && key[i] <= 'Z')
this->key += key[i];
else if( key[i] >= 'a' && key[i] <= 'z' )
this->key += key[i] + 'A' - 'a';
}
}
// string encipher( string text )
// Input: The name of the text file to be encrypted.
// Output: An encrypted version of the text file, under the file name
// that was specified by the user.
// Notes: This process is done entirely with ASCII. Each character
// has a corresponding ASCII number that can be manipulated
// with simple math (addition and subtraction), etc.
string encipher( string text ) {
string out;
for( int i = 0, j = 0; i < text.length(); ++i ) {
char c = text[i];
if(c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if( c < 'A' || c > 'Z' ) {
if (c == '.')
out += '&';
else if (c == ',')
out += '%';
else if (c == ' ')
out += '#';
continue;
}
out += (c + key[j] - 2*'A') % 26 + 'A';
j = (j + 1) % key.length();
}
return out;
}
// string decipher( string text )
// Input: The name of the text file to be decrypted.
// Output: A decrypted version of the text file, under the file name
// that was specified by the user.
// Notes: This process is done entirely with ASCII. Each character
// has a corresponding ASCII number that can be manipulated
// with simple math (addition and subtraction), etc.
string decipher( string text ) {
string out;
string space = " ";
for( int i = 0, j = 0; i < text.length(); ++i ) {
char c = text[i];
if(c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if(c < 'A' || c > 'Z') {
if (c == '&')
out += ". ";
if (c == '%')
out += ", ";
if (c == '#')
out += space;
continue;
}
out += (c - key[j] + 26) % 26 + 'A';
j = (j + 1) % key.length();
}
return out;
}
};
int main( int argc, char *argv[] ) {
// Because this function is operated through the command line,
// the user must begin with the proper parameters, as follows:
//
// mp5 -e InputFileName.txt OutputFileName.txt
//
// When the parameters are properly called for, the function
// can perform properly.
// 1. Opening the input/output streams to access external files.
ifstream inputStream;
ofstream outputStream;
// 2. Establishing which parameters represent which text files.
string original = argv[2];
string result = argv[3];
// 3. Calling for the keyword, to be specified by the user.
string key;
// Gathering the contents of the input file (the original).
ifstream in( original );
string contents((std::istreambuf_iterator<char>(in)),
istreambuf_iterator<char>());
// Prompting user for the keyword, then storing it.
cout << "Please enter the keyword: ";
cin >> key;
// Converting the keyword to manipulatable format (ASCII).
Vigenere cipher(key);
// Determining whether the input text file is to be encrypted or decrypted.
if ( argv[1][1] == 'e' ) {
string enciphered = cipher.encipher(contents); // Encrypting.
outputStream.open (result); // Opening specified output file (or creating it.)
outputStream << enciphered; // Writing results in this file.
outputStream.close(); // Closing this file.
} else if ( argv[1][1] == 'd' ) {
string deciphered = cipher.decipher(contents); // Decrypting.
outputStream.open (result); // Opening specified output file (or creating it.)
outputStream << deciphered; // Writing results in this file.
outputStream.close(); // Closing this file.
} else {
// In the event that the user makes a typing error...
cout << "Invalid parameters. Please try again." << endl;
pressEnterToContinue();
}
// Confirming to the user that the encryption/decryption has been completed.
if ( argv[1][1] == 'e' )
cout << "The text file has been encrypted.";
else
cout << "The text file has been decrypted.";
// Allowing the user to continue at his or her own discretion.
pressEnterToContinue();
return 0;
}
// pressEnterToContinue( void )
// Input: N/A
// Output: Text, prompting the user to continue at his/her own accord.
// Notes: This action can really be completed with any key.
void pressEnterToContinue ( void ) {
cout << endl << "Press enter to continue . . .";
cin.ignore();
cin.get();
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment