Problem with basic encryption program.

This is a discussion on Problem with basic encryption program. within the C++ Programming forums, part of the General Programming Boards category; I have been working on an sncryption program recently, and at first it was okay and everything was working, but ...

  1. #1
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18

    Problem with basic encryption program.

    I have been working on an sncryption program recently, and at first it was okay and everything was working, but then it all started to go wrong.
    It was working before, because i was using rather short strings. Then i actually used some decent sized files and it all stopped working. Rather than being able to see all the decrypted text, i only ever got the first line or so, then it would stop showing me the text. Below is the code for both my encryption, and my decryption program. Below that is an example of what happens.


    Encryption Program

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    char encrypt(char letter, int pass, int i)
    {
         return letter + pass * pass * i - i;
    }
    int main()
    {
        ifstream oldfile;
        ofstream newfile;
        string input;
        int pass;
        char letter;
        int i = 1;
        cout<<"what is the name of the file that you want encrypted?\n";
        getline(cin,input,'\n');
        oldfile.open(input.c_str());
        cout<<"what is the number password you wish to use to encrypt the file?\n";
        cin>>pass;
        cout<<"what is the name of the file you want the encrypted text to go to?\n";
        cin.get();
        getline(cin,input,'\n');
        newfile.open(input.c_str());
        while (oldfile)
        {
              oldfile.get(letter);
              if (oldfile)
              {
                           newfile.put(encrypt(letter,pass,i));
                           i++;
              }
        }
        cout<<"finished";
        oldfile.close();
        newfile.close();
        cin.get();
        cin.get();
    }

    Decryption Program


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    char encrypt(char letter, int pass, int i)
    {
         return letter - pass * pass * i + i;
    }
    int main()
    {
        ifstream oldfile;
        string input;
        int pass;
        char letter;
        int i = 1;
        cout<<"what is the name of the file that you want decrypted?\n";
        getline(cin,input,'\n');
        oldfile.open(input.c_str());
        cout<<"what is the number password you used to encrypt the file?\n";
        cin>>pass;
        cout<<"here is your decrypted text:\n";
        while (oldfile)
        {
              oldfile.get(letter);
              if (oldfile)
              {
                           cout<<encrypt(letter,pass,i);
                           i++;
              }
        }
        oldfile.close();
        cin.get();
        cin.get();
    }

    Anyway, this is what happens when i use the program on the following string, using the number 12 as a password.

    ok, this is a test, im working on it, i appear to be facing a lot of problems, these are really starting to annoy me.
    This is why I'm resorting to the internet, maybe someone can help.
    Anyway, I'm still typing because I need a long string to test, it seems to be okay with shorter strings.
    I think the problem might be when i press enter and start a new line. Anyway, we'll see what people on the forums think.

    This is the outputted text once i run the program.

    ok, this is a test, im working on it, i appear to be f

    Does anyone know why this is happening, and if so, what can i do about it?
    The amount of text i see has something to do with the password because with a different password, i see a different amount of text.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Try opening the encrypted file in binary mode when you're reading and writing to it.

    Also try
    Code:
    char encrypt(char letter, int pass, int i)
    {
         return letter;
    }
    to make sure your basic file handling is OK. If the output isn't a copy of the input, you need to address that problem first.
    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
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18
    hmm, i tried having it simply returning the letter (like Salem said) and i got the whole file in plaintext, anyone got any ideas on whats going on?

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    i've not looked at your code properly but the problem could be to do with the fact that you are not reading it in using binary.

    Take a look at this thread here?

    C++ File I/O question

    Maybe?

  5. #5
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18
    thank you so much, now for all the ifstreams, i add in ios::binary | ios::in and i do this for the ofstreams ios::binary | ios:ut.
    Thanks for the help!

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    No problem, it's one of those things you don't know of until someone actually points it out to you. I had exactly the same problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. I have big problem with encryption by ASCII
    By LINUX in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2007, 12:46 PM
  3. Very basic program - In need of help
    By gohan2091 in forum C Programming
    Replies: 10
    Last Post: 05-31-2006, 03:36 PM
  4. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  5. XOR encryption problem
    By cgod in forum C++ Programming
    Replies: 13
    Last Post: 07-29-2005, 08:17 PM

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