Encryption/Decryption Function

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?

Thanks!

My code is below:


#include "encrypt.h"

void encryptDecrypt( string inputfile, string outputfile);
string encrypt( string stringVariable);
string decrypt( string stringVariable);

void encryptDecrypt (string inputfile, string outputfile)
{
string command;
string stringVariable;

ifstream in (inputfile);
ofstream out (outputfile);

in >> command;
cout << command << endl;

while( !in.fail() )
{
if (command=="encrypt")
{
in.ignore(1000, ' ');
getline( in, stringVariable);
cout << stringVariable << endl;
cout << "encrypting" << endl;
string encrypt( string stringVariable);
}
else if (command=="decrypt")
{
in.ignore(1000, ' ');
getline( in, stringVariable);
cout << stringVariable << endl;
cout << "decrypting" << endl;
string decrypt( string stringVariable);
}
else
{
out << "FAILED" << endl;
}

in >> command;
cout << command << endl;
}
in.close();
out.close();
}

string encrypt( string stringVariable)
{
string results = "";
for ( int i=0; i<stringVariable.length(); i++ )
{
if ( stringVariable[i] == 'a' )
{
results = results + "z";
}
else if ( stringVariable[i] == 'b' )
{
results = results + "y";
}
else if (stringVariable[i] == 'c' )
{
results = results + "x";
}
else if (stringVariable[i] == 'd' )
{
results = results + "w";
}
else if (stringVariable[i] == 'e' )
{
results = results + "v";
}
else if (stringVariable[i] == 'f' )
{
results = results + "u";
}
else if (stringVariable[i] == 'g' )
{
results = results + "t";
}
else if (stringVariable[i] == 'h' )
{
results = results + "s";
}
else if (stringVariable[i] == 'i' )
{
results = results + "r";
}
else if (stringVariable[i] == 'j' )
{
results = results + "q";
}
else if (stringVariable[i] == 'k' )
{
results = results + "p";
}
else if (stringVariable[i] == 'l' )
{
results = results + "o";
}
else if (stringVariable[i] == 'm' )
{
results = results + "n";
}
else if (stringVariable[i] == 'n' )
{
results = results + "m";
}
else if (stringVariable[i] == 'o' )
{
results = results + "l";
}
else if (stringVariable[i] == 'p' )
{
results = results + "k";
}
else if (stringVariable[i] == 'q' )
{
results = results + "j";
}
else if (stringVariable[i] == 'r' )
{
results = results + "i";
}
else if (stringVariable[i] == 's' )
{
results = results + "h";
}
else if (stringVariable[i] == 't' )
{
results = results + "g";
}
else if (stringVariable[i] == 'u' )
{
results = results + "f";
}
else if (stringVariable[i] == 'v' )
{
results = results + "e";
}
else if (stringVariable[i] == 'w' )
{
results = results + "d";
}
else if (stringVariable[i] == 'x' )
{
results = results + "c";
}
else if (stringVariable[i] == 'y' )
{
results = results + "b";
}
else if (stringVariable[i] == 'z' )
{
results = results + "a";
}
}
return results;
cout << results << endl;
}

string decrypt( string stringVariable)
{
string results = "";
for ( int i=0; i<stringVariable.length(); i++ )
{
if ( stringVariable[i] == 'z' )
{
results = results + "a";
}
else if ( stringVariable[i] == 'y' )
{
results = results + "b";
}
else if (stringVariable[i] == 'x' )
{
results = results + "c";
}
else if (stringVariable[i] == 'w' )
{
results = results + "d";
}
else if (stringVariable[i] == 'v' )
{
results = results + "e";
}
else if (stringVariable[i] == 'u' )
{
results = results + "f";
}
else if (stringVariable[i] == 't' )
{
results = results + "g";
}
else if (stringVariable[i] == 's' )
{
results = results + "h";
}
else if (stringVariable[i] == 'r' )
{
results = results + "i";
}
else if (stringVariable[i] == 'q' )
{
results = results + "j";
}
else if (stringVariable[i] == 'p' )
{
results = results + "k";
}
else if (stringVariable[i] == 'o' )
{
results = results + "l";
}
else if (stringVariable[i] == 'n' )
{
results = results + "m";
}
else if (stringVariable[i] == 'm' )
{
results = results + "n";
}
else if (stringVariable[i] == 'l' )
{
results = results + "o";
}
else if (stringVariable[i] == 'k' )
{
results = results + "p";
}
else if (stringVariable[i] == 'j' )
{
results = results + "q";
}
else if (stringVariable[i] == 'i' )
{
results = results + "r";
}
else if (stringVariable[i] == 'h' )
{
results = results + "s";
}
else if (stringVariable[i] == 'g' )
{
results = results + "t";
}
else if (stringVariable[i] == 'f' )
{
results = results + "u";
}
else if (stringVariable[i] == 'e' )
{
results = results + "v";
}
else if (stringVariable[i] == 'd' )
{
results = results + "w";
}
else if (stringVariable[i] == 'c' )
{
results = results + "x";
}
else if (stringVariable[i] == 'b' )
{
results = results + "y";
}
else if (stringVariable[i] == 'a' )
{
results = results + "z";
}
}
return results;
cout << results << endl;
}
Cout << encrypt(stringVariable) << endl ;
This declares a function: string encrypt( string stringVariable );

This calls the function:
1
2
string str = "abcd" ; 
encrypt(str) ; // call the function, passing str as the argument 


This calls the function and places the result that was returned into a variable :
1
2
3
string str = "abcd" ; 
string result = encrypt(str) ;
// use result 


We don't really need those huge sequences of if - else-if constructs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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() ;
}
Got it, thanks for all the help.
Topic archived. No new replies allowed.