title: Substitution Cipher, Strings and Functions
body: <p>Where I am stuck at is the EncryptString function. The EncryptFile function will take a .txt file and convert the .txt to a C string equivalent, then pass that into the EncrpytString function. When I pass it in, the Substitution function will do its work with the cipher string and encrypt the individual characters, then spit that back out into a new string called encrypted_string. However, I cannot get this function to accept the argument passed into it. Any guidance?</p>
  
  <p>This is where the program currently is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  char substitution_cipher(string cipher_key, char char_to_encrypt);
  char reverse_substitution_cipher(string cipher_key, char char_to_encrypt);
  
  string EncryptString(string &amp;cipher_key, string string_to_be_encrypted);
  string DecryptString(string &amp;cipher_key, string string_to_be_decrypted);
  
  void RotateCipherKey(string &amp;cipher_key);
  void DisplayFile(string filename);
  
  
  void EncryptFile(string cipher_key, string filename_from, string filename_to);
  void DecryptFile(string cipher_key, string filename_from, string filename_to);
  
  int main()
  {
      string cipher_key = "qwertyuiopasdfghjklzxcvbnm";
  
      EncryptFile(cipher_key, "test.txt", "test-encrypted.txt");
      DecryptFile(cipher_key, "test-encrypted.txt", "test-ed.txt");
  
      DisplayFile("test.txt");
      DisplayFile("test-encrypted.txt");
      DisplayFile("test-ed.txt");
  
      system("PAUSE");
      return 0;
  }
  
  
  //  Rotate the cipher key. Example: abcdef becames bcdefa
  
  void RotateCipherKey(string &amp;cipher_key)
  {
      rotate(cipher_key.begin(), cipher_key.begin() + 1, cipher_key.end());
  }
  
  // Perform a substitution cipher on a single character 
  // using the specified cipher key
  char SubstitutionCipher(string cipher_key, char char_to_encrypt)
  {
      for (int iii = 0; iii &lt; cipher_key.length(); iii++)
      {
          RotateCipherKey(cipher_key);
          char_to_encrypt = cipher_key[iii];
      }
      return char_to_encrypt;
  }
  
  // Perform a "reverse" substitution cipher on a single character 
  // using the specified cipher key
  char ReverseSubstitutionCipher(string cipher_key, char char_to_decrypt)
  {
      for (int iii = 0; iii &lt; cipher_key.length(); iii++)
      {
          RotateCipherKey(cipher_key);
          char_to_decrypt = cipher_key[iii];
      }
      return char_to_decrypt;
  }
  
  // Encrypt String and return it 
  // You will use the SubstitutionCipher() function to encrypt the
  // individual characters
  // 
  // Note: We will call RotateCipherKey() after each time we encrypt 
  // a character.
  string EncryptString(string &amp;cipher_key, string string_to_be_encrypted)
  {
      char *y = string_to_be_encrypted.c_str();
      {
          //SubstitutionCipher(cipher_key, string_to_be_encrypted);
      }
      cout &lt;&lt; " " &lt;&lt; string_to_be_encrypted;
      string encrypted_string = string_to_be_encrypted;
      return encrypted_string;
  }
  
  // Decrypt String and return it 
  // You will use the ReverseSubstitutionCipher() function to decrypt the
  // individual characters
  //
  // Note: We will call RotateCipherKey() after each time we encrypt 
  // a character.
  string DecryptString(string &amp;cipher_key, string string_to_be_decrypted)
  {
      string decrypted_string = string_to_be_decrypted;
  
      return decrypted_string;
  }
  
  // Display file specified by the filname parameter
  
  void DisplayFile(string filename)
  {
      string str;
      ifstream infile;
      infile.open(filename);
      infile &gt;&gt; str;
      while (infile)
      {
          cout &lt;&lt; " " &lt;&lt; str;
          infile &gt;&gt; str;
      }
      cout &lt;&lt; endl;
  }
  
  
  // Encrypt the specified file using the specified cipher key and 
  // write the output to a different file
  // This function is complete
  void EncryptFile(string cipher_key, string filename_from, string filename_to)
  {
      string input;
      ifstream infile;
      ofstream outfile;
  
      infile.open(filename_from.c_str());
      outfile.open(filename_to.c_str());
  
      if (!infile)
      {
          cout &lt;&lt; "Can not open input file " + filename_from &lt;&lt; endl;
          exit(0);
      }
  
      if (!outfile)
      {
          cout &lt;&lt; "Can not open Output file " + filename_to &lt;&lt; endl;
          exit(0);
      }
  
  
      while (getline(infile, input))
      {
          outfile &lt;&lt; EncryptString(cipher_key, input) &lt;&lt; endl;
      }
      infile.close();
      outfile.close();
  }
  
  // Decrypt the specified file using the specified cipher key and 
  // write the output to a different file
  // This function is complete
  void DecryptFile(string cipher_key, string filename_from, string filename_to)
  {
      string input;
      ifstream infile;
      ofstream outfile;
  
      infile.open(filename_from.c_str());
      outfile.open(filename_to.c_str());
  
      if (!infile)
      {
          cout &lt;&lt; "Can not open input file " + filename_from &lt;&lt; endl;
          exit(0);
      }
  
      if (!outfile)
      {
          cout &lt;&lt; "Can not open Output file " + filename_to &lt;&lt; endl;
          exit(0);
      }
  
  
      while (getline(infile, input))
      {
          outfile &lt;&lt; DecryptString(cipher_key, input) &lt;&lt; endl;
      }
      infile.close();
      outfile.close();
  }
  </code></pre>
  
  <h2>EDIT//</h2>
  
  <pre><code>string EncryptString(string &amp;cipher_key, string string_to_be_encrypted)
  {
      char new_char;
      for (int iii = 0; iii &lt; string_to_be_encrypted.length(); iii++)
      {
          new_char = SubstitutionCipher(cipher_key, string_to_be_encrypted[iii]);
          RotateCipherKey(cipher_key);
      }
      string encrypted_string = string_to_be_encrypted;
      cout &lt;&lt; " " &lt;&lt; encrypted_string;
      return encrypted_string;
  }
  </code></pre>
  
  <p>Ok here is new code now with some modifications.</p>