C++ 正在读取文件,试图显示文件中的加密消息 #包括 #包括 #包括 #包括“Encrypt.h” int main(){ std::字符串选择; std::cout此外,请定义您的问题,而不是“不工作”和“有问题”。这里有大量关于eof和文件阅读的问题,我建议

C++ 正在读取文件,试图显示文件中的加密消息 #包括 #包括 #包括 #包括“Encrypt.h” int main(){ std::字符串选择; std::cout此外,请定义您的问题,而不是“不工作”和“有问题”。这里有大量关于eof和文件阅读的问题,我建议,c++,C++,正在读取文件,试图显示文件中的加密消息 #包括 #包括 #包括 #包括“Encrypt.h” int main(){ std::字符串选择; std::cout此外,请定义您的问题,而不是“不工作”和“有问题”。这里有大量关于eof和文件阅读的问题,我建议您先进行搜索。“(仅小写):”它对开发人员友好,而不是用户友好。只需将用户输入转换为小写,您不需要显示需求,这将更方便用户。 #include <iostream> #include <string> #include

正在读取文件,试图显示文件中的加密消息
#包括
#包括
#包括
#包括“Encrypt.h”
int main(){
std::字符串选择;

std::cout此外,请定义您的问题,而不是“不工作”和“有问题”。这里有大量关于eof和文件阅读的问题,我建议您先进行搜索。“(仅小写):”它对开发人员友好,而不是用户友好。只需将用户输入转换为小写,您不需要显示需求,这将更方便用户。
#include <iostream>
#include <string>
#include <fstream>

#include "Encrypt.h"

int main() {
std::string Choose;
std::cout << "Please enter write or read(Lower Case Only): ";
std::getline(std::cin, Choose);
if (Choose == "write") {
    std::string Sentence;
    std::string SecurityKey;
    std::string TextFile;

    std::cout << "Enter Your sentence that you wish to be encrupted" << std::endl << "Sentence: ";
    std::getline(std::cin, Sentence);
    std::cout << std::endl << "Secuirty Key Disclaimer Never forget what secruity \nkey you used otherwise your message will be lost forever" << std::endl;
    std::cout << "Secuirty Key: ";
    std::getline(std::cin, SecurityKey);

    std::string message = encrypt(Sentence, SecurityKey);
    std::cout << "Encrypted: " << std::endl << message;
    std::cout << "\nDecrypted: " << decrypt(message, SecurityKey) << std::endl;

    std::cout << "Enter a title for text Document: ";
    std::getline(std::cin, TextFile);
    TextFile = TextFile + ".txt";
    std::ofstream out(TextFile);
    out << message;
    out.close();
}
else if (Choose == "read") {
    std::string NameOfFile;
    std::getline(std::cin, NameOfFile);
    NameOfFile = NameOfFile + ".txt";
    std::string STRING;
    std::ifstream infile;
    infile.open(NameOfFile);
    while (!infile.eof)
    {
        getline(infile, STRING);
    }
    infile.close();
    std::string S_Key;
    std::getline(std::cin, S_Key);
    std::cout << "\nDecrypted: " << decrypt(STRING, S_Key) << std::endl;
}
else {
    std::cout << "There are only 2 Options.... (lower Case Only)" << std::endl;
}


system("PAUSE");
}
std::string encrypt(std::string msg, std::string key) {

std::string tmp(key);
while (key.size() < msg.size())
    key += tmp;


for (std::string::size_type i = 0; i < msg.size(); ++i)
    msg[i] ^= key[i];
return msg;
}
std::string decrypt(std::string msg, std::string key) {
return encrypt(msg, key);
}