Alright ive started a new thread to get some order on things...
THe problem i have right now is that i want the user to be able to choose what file to encrypt and decrypt. The code loks like this:
Code:
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main_menu();
int XOR_menu();
int XOR_select();
int XOR_cryption();
int ASCII_menu();
int ASCII_select();
int ASCII_encryption();
int ASCII_decryption();
int about();
int close();
int main_select;
int XOR_menu_select;
int ASCII_menu_select;
char key[14]="ABCDEFGHIJKLM";
int main()
{
main_menu();
}
int main_menu()
{
cout << "Hvilken kyrpterings metode vil du bruge?" << endl << endl;
cout << "1. exclusive-OR (XOR)." << endl;
cout << "2. ASCII forskydning." << endl;
cin >> main_select;
if (main_select == 1)
{
system("cls");
XOR_menu();
}
else
{
system("cls");
ASCII_menu();
}
}
int XOR_menu()
{
cout << "Hvad vil du foretage dig?" << endl << endl;
cout << "1. Anvende kryptering." << endl;
cout << "2. Laes om programmet." << endl;
cout << "3. Afslut programmet." << endl;
cout << "Valg; ";
cin >> XOR_menu_select;
if (XOR_menu_select < 1 || XOR_menu_select > 4)
{
cout << endl << "Ugyldigt menuvalg. Du skal vaelge en menu mellem 1 og 3." << endl;
cout << "Tryk paa en tast for at proeve igen." << endl;
getch();
system("cls");
XOR_menu();
}
else
{
system("cls");
XOR_select();
}
}
int XOR_select()
{
switch(XOR_menu_select)
{
case 1: XOR_cryption();
break;
case 2: about();
break;
case 3: close();
break;
}
}
int XOR_cryption()
{
char file_name;
char ch;
string input;
/*cin >> file_name;
ifstream ch_file(file_name.c_str());*/
ifstream ch_file("hej.txt");
while(1)
{
ch = ch_file.get();
if(ch == EOF)
break;
input.push_back(ch);
}
ofstream cryp_file("hej.txt");
for(int x=0; x<input.size(); x++)
{
input[x] = input[x]^key[x%sizeof(key)];
cryp_file << input[x];
}
cout << "Filen er blevet behandlet og er enten blevet krypteret eller dekrypteret:" << endl;
cout << " - Var filen krypteret er den nu blevet dekrypteret." << endl;
cout << " - Var filen ikke krypteret er den nu blevet krypteret." << endl << endl << endl;
cout << "Tryk paa en tast for at afslutte programmet.";
getch();
close();
}
int ASCII_menu()
{
cout << "Hvad vil du foretage dig?" << endl << endl;
cout << "1. Kryptere en fil." << endl;
cout << "2. Dekryptere en fil." << endl;
cout << "3. Laes om programmet." << endl;
cout << "4. Afslut programmet." << endl;
cin >> ASCII_menu_select;
if (ASCII_menu_select < 1 || ASCII_menu_select > 4)
{
cout << endl << "Ugyldigt menuvalg. Du kan kun vaelge fra menu 1 til 4." << endl;
cout << "Tryk paa en tast for at prøve igen." << endl;
getch();
system("cls");
ASCII_menu();
}
else
{
ASCII_select();
}
}
int ASCII_select()
{
switch (ASCII_menu_select)
{
case 1: ASCII_encryption();
break;
case 2: ASCII_decryption();
break;
case 3: about();
break;
case 4: close();
}
}
int ASCII_encryption()
{
char ch;
ifstream ch_file("hej.txt");
string input;
while(1)
{
ch = ch_file.get();
if(ch == EOF)
break;
input.push_back(ch);
}
ofstream encryp_file("hej.txt");
for (int x=0; x<input.size(); x++)
{
encryp_file << (char)(input[x]+5);
}
cout << "Filen er nu blevet krypteret." << endl << endl;
cout << "Tryk paa en tast for at afslutte programmet." << endl;
getch();
close();
}
int ASCII_decryption()
{
char ch;
ifstream ch_file("hej.txt");
string input;
while(1)
{
ch = ch_file.get();
if(ch == EOF)
break;
input.push_back(ch);
}
ofstream decryp_file("hej.txt");
for (int x=0; x<input.size(); x++)
{
decryp_file << (char)(input[x]-5);
}
decryp_file.close();
cout << "Filen er nu blevet dekrypteret." << endl << endl << endl;
cout << "Tryk paa en tast for at afslutte programmet.";
getch();
close();
}
int about()
{
system ("cls");
cout << "+-------------------------------------+" << endl;
cout << " Lavet af: Mark Jakobsen." << endl;
cout << " Fag: Programmering C." << endl;
cout << " Vejleder: Thomas Brun Kristensen." << endl;
cout << " Afleveret d.: 10/5-05" << endl;
cout << "+-------------------------------------+" << endl;
getch();
close();
}
int close()
{
return 0;
}
Thge language is danish but it is only the screen output that is danish. I prefer to work with english so everything else should be english.
The problem is in the function XOR_cryption(). I tried using
Code:
cin >> file_name;
ifstream ch_file(file_name.c_str());
so that the handled filename is based on user input but i get compiler error saying that
`c_str´ has not been declared
. But i am not supposed to do that am i?
If you would just focus on that it would be great. Ideas on how to make some other parts of the program is always appreaciated but will not be implemented right now...