My encryption/decryption function will not run correctly. I am able to grab the command (encrypt or decrypt) and grab the string of character/words to work with but it never calls my functions after that. What am I missing?
#include <iostream>
#include <string>
#include <fstream>
const std::string alphabet = "abcdefghijklmnopqrstuvwxyz" ;
// encryption is symmetric
// ie. the same algorithm is used for both encryption and de-encryption
// ie. encrypt( encrypt(plaintext) ) => plaintext
std::string encrypt( std::string str )
{
std::string result ;
for( char c : str ) // for each character in the string
{
const std::size_t pos = alphabet.find(c) ; // try to locate it in the alphabet
// if it was found, map a => z, b => y, ... z => a
// alphabet.rbegin()[pos]: the same position, but counting from the back
// in the reverse direction ie. at the same position in "zyxwvutsrqponmlkjihgfedcba"
// eg. "abcdefghijklmnopqrstuvwxyz"[5] == 'f'
// "zyxwvutsrqponmlkjihgfedcba"[5] == 'u'
if( pos != std::string::npos ) result += alphabet.rbegin()[pos] ;
else result += c ; // not found; add it verbatim to the result
}
return result ;
}
void encrypt_file( std::string in_file_name, std::string out_file_name )
{
std::ifstream fin(in_file_name) ;
std::ofstream fout(out_file_name) ;
std::string line ;
while( std::getline( fin, line ) ) fout << encrypt(line) << '\n' ;
}
int main()
{
// write the encrypted contents of this file to file "encrypted.txt"
encrypt_file( __FILE__, "encrypted.txt" ) ;
// write the encrypted contents of file "encrypted.txt" to fle "copy_of_this_file.cpp"
// essentially de-encrypt contents of file "encrypted.txt"
encrypt_file( "encrypted.txt", "copy_of_this_file.cpp" ) ;
// dump on stdout the result of: first encrypt, then de-encrypt result of encryption
// to verify that we got back the original after de-encryption
std::cout << "contents of file copy_of_this_file.cpp\n------------------------------\n"
<< std::ifstream( "copy_of_this_file.cpp" ).rdbuf() ;
}