读取文件,尝试显示文件中的加密消息

时间:2016-02-29 16:11:38

标签: c++

#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");
}

我的问题是它没有读取文件如何加密它可能是因为.eof()读取文件的方式不同于程序加密方式,..它没有正确读取文件我是如何工作的在这附近?

如果有人对此感兴趣,请访问Encrypt.h

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

0 个答案:

没有答案