【问题标题】:Substitution Cipher, Strings and Functions替换密码、字符串和函数
【发布时间】:2023-10-04 21:32:01
【问题描述】:

我遇到的问题是 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;
}

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

【问题讨论】:

  • 如何调用函数?你得到什么错误?
  • 函数 SubstitutionCipher 在 EncryptString 中被调用,EncryptString 由 EncryptFile 调用。错误在行 char y=string_to_be_encrypted.c_str();我得到的错误是 const char 无法初始化类型 char。我相信我需要做的是将传递给 EncryptString 的字符串转换为字符数组,但我不确定。

标签: c++ string function encryption


【解决方案1】:

您遇到的问题是 c_str() 返回指向 const 的指针 const char*,您不能将其分配给行中的 char*

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

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

【讨论】:

  • 好的。因此,如果我将字符串传递给 SubstitutionCipher,例如 SubstitutionCipher(cipher_key, encrypted_string);我将收到无法从字符串转换为字符的错误。如何将该字符串传递给函数?
  • @jokersaidit 直接传递char,例如string_to_be_encripted[10] 用于string_to_be_encripted 中的第10 个char
  • 好的,在原始帖子中进行了编辑。现在我了解将 char 传递给函数的逻辑。我确实有一个后续问题要问你。现在我已经将字符串的索引传递给了 SubstitutionCipher 函数,它将遍历循环并每次返回一个新字母。我怎样才能把所有这些新字母塞进一个新字符串中?
  • 从一个空字符串开始,然后使用 push_back,或者为字符串预分配内存并使用典型的 [ ] 访问来添加元素
  • 添加元素工作正常。差不多好了。现在需要修复间距和标点符号才能真正让它看起来加密。这是示例:这是一些随机的乱码。请检查所有行是否正确加密和解​​密。希望你能做到这一点。 zoac fn qbvh tgezyt qrbnejozp。 zjaiqf fzkjm krt utaek ujx xbcbfw jqjugus xom wrelqlb. fcbfhybnd, huf waj lipx qzbkjvj.