加密和解密文本文件

时间:2015-07-25 23:04:50

标签: c++ file encryption text

我想在c ++中创建一个有三个选项的程序。

  1. 用户将在控制台中写入文本,此文本将使用某种经过修改的xor密码加密,加密文本将保存为文本文件。
  2. 将导入预先存在的文本文件,以相同方式加密,加密文本将像以前一样保存为文本文件。
  3. 将导入加密的文本文件并将其解密,结果将另存为文本文件。
  4. 每次用户输入文本文件的目的地和名称,即C:\Users\john\Desktop\(需要反斜杠) xor密码的密钥也只是两个字符,即a,b。 我写了一个算法,案例2没有正常工作(或很少工作)。例如,如果我有文件"你好我的名字是约翰!"或者简单的东西,它将被正确地加密和解密,但如果我从互联网上复制粘贴一些文章,那就有问题,它不起作用。它仅解密原始文本的某些行,其余的是随机字符/符号...它必须是换行符的内容?我不知道出了什么问题。

    代码是:

    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        int option,choice;
        char temp1,temp2;
        do {
        cout << "Press:\n1.To create text, encrypt it and save it as a text        file\n2.To import text file and encrypt it\n3.To import text file and decrypt it\n\n\n";
        cin >> choice;
        if (choice==1)  {
            string text,coded,directory1,filename1;
            cout << "Enter the text to be encrypted:\n\n\n";
            fflush(stdin);
            getline(cin,text);
            cout << "Enter the two keys for the encryption (two characters):\n";
            cin >> temp1 >> temp2;
            for (int i=0;i<text.size();i++)   {
                    coded+=text[i] ^ ((int(temp1)+i)%temp2);
            }
            cout << "The encrypted text message is:\n\n\n" << coded << "\n";
            cout << "Enter the path where the encrypted text will be saved (with \\ at the end):\n";
            fflush(stdin);
            getline(cin,directory1);
            cout << "Enter the name of the text file:\n";
            getline(cin,filename1);
            ofstream data((directory1 + filename1 + ".txt").c_str());
            data << coded << "\n";
            data.close();
        }
        else if (choice==2) {
            string directory1,directory2,filename1,filename2;
            cout << "Enter the path where the file to be encrypted will be imported from:\n";
            fflush(stdin);
            getline(cin,directory1);
            cout << "Enter the name of the text file:\n";
            getline(cin,filename1);
            ifstream data_input((directory1 + filename1 + ".txt").c_str());
            cout << "Enter the two keys for the encryption (two characters):\n";
            cin >> temp1 >> temp2;
            cout << "Enter the path where the encrypted text will be saved (with \\ at the end):\n";
            fflush(stdin);
            getline(cin,directory2);
            cout << "Enter the name of the text file:\n";
            getline(cin,filename2);
            ofstream data_output((directory2 + filename2 + ".txt").c_str());
            cout << "The encrypted text message is:\n\n\n";
            while (!data_input.eof())   {
                string text,coded;
                fflush(stdin);
                getline(data_input,text);
                for (int i=0;i<text.size();i++)   {
                        coded+=text[i] ^ ((int(temp1)+i)%temp2);
                }
                cout << coded << endl;
                data_output << coded << endl;
            }
            data_input.close();
            data_output.close();
        }
        else if (choice==3) {
            string directory1,directory2,filename1,filename2;
            cout << "Enter the path where the encrypted text file will be imported from:\n";
            fflush(stdin);
            getline(cin,directory1);
            cout << "Enter the name of the text file:\n";
            getline(cin,filename1);
            ifstream data_input((directory1 + filename1 + ".txt").c_str());
            cout << "Enter the two keys for the encryption (two characters):\n";
            cin >> temp1 >> temp2;
            cout << "Enter the path where the encrypted text will be saved (with \\ at the end):\n";
            fflush(stdin);
            getline(cin,directory2);
            cout << "Enter the name of the text file:\n";
            getline(cin,filename2);
            ofstream data_output((directory2 + filename2 + ".txt").c_str());
            cout << "The decrypted text message is:\n\n\n";
            while (!data_input.eof())   {
                string coded,decoded;
                fflush(stdin);
                getline(data_input,coded);
                for (int i=0;i<coded.size();i++)   {
                    decoded+=coded[i] ^ ((int(temp1)+i)%temp2);
                }
                cout << decoded << endl;;
                data_output << decoded << endl;;
            }
            data_input.close();
            data_output.close();
        }
        cout << "\n\n\nEnter 0 to exit or 1 to return to the main menu..:\n";
        cin >> option;
        } while (option);
        cin.get();
    }
    

    哦,我没有很多c ++的经验,我去年刚开始,所以不要对我很难!

0 个答案:

没有答案