XOR-enxcryption problems

This is a discussion on XOR-enxcryption problems within the C++ Programming forums, part of the General Programming Boards category; Alright ive started a new thread to get some order on things... THe problem i have right now is that ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    XOR-enxcryption problems

    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...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > ifstream ch_file(file_name.c_str());
    You need
    string file_name;

    not
    char file_name;

    > XOR_menu();
    Having a function call itself like this means you should have used a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    > ifstream ch_file(file_name.c_str());
    You need
    string file_name;
    Oh right ofcourse! Thanks.

    > XOR_menu();
    Having a function call itself like this means you should have used a loop.
    Hmm right. Thanks.

    Well it works now. Or that is, if there are no spaces on the filename. If there is a space, it doesnt work. I suppose i can use cin.getline to get the entire filename, but i dont know how to use it on strings, only arrays. I need something more than
    Code:
    cin.getline (file_name)
    but what?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > but what?
    >cin.getline (file_name)
    Close, try:
    getline (cin, file_name);

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Well now it doesnt even wait for input from the user...

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Any time you do a cin of the form:
    Code:
        cin >> XOR_menu_select;
    a newline will be left in the input buffer. So the next time you get input from the user the newline is still there. So getline() always stops reading when it sees this newline. So you should probably put something in your code to eat the newline, whenever you use:
    cin >> something;
    A cin.ignore() will eat the newline, so you could do this:
    Code:
        cin >> XOR_menu_select;
        cin.ignore(80,'\n');

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    I tied putting cin.ignore in under cin >> XOR_menu_select, like you suggested, and also right above the getline, like this
    Code:
        cin.ignore(80,'/n');    
        getline (cin, file_name);
        ifstream ch_file(file_name.c_str());
    but it doesnt work. The program halts for input, like its supposed to but when i hit enter nothing happens. It just adds a line.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    might help if it was a \n not a /n
    Woop?

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    ... duuuhhh what? im jackass homer. how manny magic beans should i sell the baby for? uuhhhh threee?

    *bangs head with big wooden mallet*

    alright now now that i have punished my self im gonna go ahead and correct that and continue Youll hear from me again soon... *scary music*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21