替换密码,字符串和函数

时间:2015-03-20 18:35:19

标签: c++ string function encryption

我遇到的是EncryptString函数。 EncryptFile函数将获取.txt文件并将.txt转换为等效的C字符串,然后将其传递给EncrpytString函数。当我传入它时,Substitution函数将使用密码字符串进行工作并加密单个字符,然后将其吐回到名为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();
}

EDIT //

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;
}

好的,这里是新代码,现在有一些修改。

1 个答案:

答案 0 :(得分:0)

您遇到的问题是c_str()会返回指向const const char*的指针,您无法将其指定给行中的char*

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

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