替换密码、字符串和函数

Substitution Cipher, Strings and Functions

我卡在的地方是 EncryptString 函数。 EncryptFile 函数将采用 .txt 文件并将 .txt 转换为等效的 C 字符串,然后将其传递给 EncrpytString 函数。当我传入它时,替换函数将使用密码字符串完成它的工作并加密各个字符,然后将其吐回一个名为 encrypted_string 的新字符串。但是,我无法让此函数接受传递给它的参数。有什么指导吗?

这是程序当前所在的位置:

#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 the cipher key. Example: abcdef becames bcdefa

void RotateCipherKey(string &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 < 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 < 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 &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 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 &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 >> str;
    while (infile)
    {
        cout << " " << str;
        infile >> str;
    }
    cout << 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 << "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 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 << "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();
}

编辑//

string EncryptString(string &cipher_key, string string_to_be_encrypted)
{
    char new_char;
    for (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;
}

好的,现在是经过一些修改的新代码。

你遇到的问题是 c_str() returns 一个指向 const const char* 的指针,你不能将其分配给行 [=16] 中的 char* =]

char* y=string_to_be_encrypted.c_str(); // cannot assign a `const char*` to `char*`

直接使用字符串来执行您的工作。没有其他方法可以将指向非常量 char 的指针指向 std::string.

的数据