c++ - Substitution Cipher, Strings and Functions -
where stuck @ encryptstring function. encryptfile function take .txt file , convert .txt c string equivalent, pass encrpytstring function. when pass in, substitution function work cipher string , encrypt individual characters, spit out new string called encrypted_string. however, cannot function accept argument passed it. guidance?
this program is:
#include <iostream> #include <fstream> #include <string> #include <cstdlib> 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 &cipher_key, string string_to_be_encrypted); string decryptstring(string &cipher_key, string string_to_be_decrypted); void rotatecipherkey(string &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 cipher key. example: abcdef becames bcdefa void rotatecipherkey(string &cipher_key) { rotate(cipher_key.begin(), cipher_key.begin() + 1, cipher_key.end()); } // perform substitution cipher on single character // using specified cipher key char substitutioncipher(string cipher_key, char char_to_encrypt) { (int iii = 0; iii < cipher_key.length(); iii++) { rotatecipherkey(cipher_key); char_to_encrypt = cipher_key[iii]; } return char_to_encrypt; } // perform "reverse" substitution cipher on single character // using specified cipher key char reversesubstitutioncipher(string cipher_key, char char_to_decrypt) { (int iii = 0; iii < cipher_key.length(); iii++) { rotatecipherkey(cipher_key); char_to_decrypt = cipher_key[iii]; } return char_to_decrypt; } // encrypt string , return // use substitutioncipher() function encrypt // individual characters // // note: call rotatecipherkey() after each time encrypt // character. string encryptstring(string &cipher_key, string string_to_be_encrypted) { char *y = string_to_be_encrypted.c_str(); { //substitutioncipher(cipher_key, string_to_be_encrypted); } cout << " " << string_to_be_encrypted; string encrypted_string = string_to_be_encrypted; return encrypted_string; } // decrypt string , return // use reversesubstitutioncipher() function decrypt // individual characters // // note: call rotatecipherkey() after each time encrypt // character. string decryptstring(string &cipher_key, string string_to_be_decrypted) { string decrypted_string = string_to_be_decrypted; return decrypted_string; } // display file specified filname parameter void displayfile(string filename) { string str; ifstream infile; infile.open(filename); infile >> str; while (infile) { cout << " " << str; infile >> str; } cout << endl; } // encrypt specified file using specified cipher key , // write output different file // function 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 << "can not open input file " + filename_from << endl; exit(0); } if (!outfile) { cout << "can not open output file " + filename_to << endl; exit(0); } while (getline(infile, input)) { outfile << encryptstring(cipher_key, input) << endl; } infile.close(); outfile.close(); } // decrypt specified file using specified cipher key , // write output different file // function 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 << "can not open input file " + filename_from << endl; exit(0); } if (!outfile) { cout << "can not open output file " + filename_to << endl; exit(0); } while (getline(infile, input)) { outfile << decryptstring(cipher_key, input) << endl; } infile.close(); outfile.close(); }
edit//
string encryptstring(string &cipher_key, string string_to_be_encrypted) { char new_char; (int iii = 0; iii < 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 << " " << encrypted_string; return encrypted_string; }
ok here new code modifications.
the problem have c_str()
returns pointer const, const char*
, can not assign char*
in line
char* y=string_to_be_encrypted.c_str(); // cannot assign `const char*` `char*`
use directly string perform work. there no other way of getting pointer non-const char
data of std::string
.
Comments
Post a Comment