基本加密程序无法从文件中正确读取

时间:2013-11-25 21:27:08

标签: c++ encryption dylan

我正在尝试让程序在一个实例上获取输入以加密用户,在另一个实例中(每当用户需要时)从加密运行中创建的相同文件派生并反转加密但是,它只是给我看起来像错误代码。每次大约6个数字/字母,但这与它应该做的事情完全无关。

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <cstdio>
#include <string>

using namespace std;
//ENCRYPTION AND DECRYPTION
string Decode(string text)
{
    int a;
    for(a=0; a<text.length(); a++){
        text[a]--;
    }
    return text;
}


string Encode (string text)
{
    int a;
    for(a=0; a<text.length(); a++){
        text[a]++;
    }
    return text;
}

//PROMPTS AND MAIN PROGRAM
int main(){
char input;
cout<<"+-+-+-+-+-+-+-+\n";
cout<<"|C|r|y|p|t|e|r|\n";
cout<<"+-+-+-+-+-+-+-+\n\n";
cout<<"Version 1.01 - Revision 2013\n";
cout<<"Created by Dylan Moore\n";
cout<<"____________________\n\n";
cout<<"Hello, would you like to decode or encode an encryption key?\nType 'd' for decode or 'e' for encode.\n\n";
cin>>input;
cin.get();
    switch(input){
        //DECODE
        case 'd':
        {
            string Message;
            ifstream myfile;
            myfile.open ("Key.txt");
                if (myfile.fail())
            {            
                    cout<<"Key.txt not found! Please make sure it is in the same directory as Crypter!\n\n";            
                    cin.get();            
                    return 0;            
                }    
            getline(myfile, Message);
            myfile.close();
            cout<<"Decoded Data:\n"<<Decode;(Message); 
            break;
        }
        //ENCODE
        case 'e':
        {
            string Message;
            ofstream myfile;
                    cout<<"Type the key you wish to be encrypted. When finished, press 'enter' 2 times to confirm.\n\n";
            getline (cin, Message);
            cin.get();
            myfile.open ("Key.txt");
            myfile<<Encode(Message);
            myfile.close();
                    cout<<"Thank you, your message has been saved to 'Key.txt' in this directory.\n";
            break;
        }

    }
    return _getch();
}

1 个答案:

答案 0 :(得分:1)

cout<<"Decoded Data:\n"<<Decode;(Message);
//                             ^ wrong

应该是

cout<<"Decoded Data:\n"<<Decode(Message); 

您当前的代码打印Decode函数的地址,然后执行第二个语句(Message);,该语句无效。

相关问题