XOR Encryption

This is a discussion on XOR Encryption within the C++ Programming forums, part of the General Programming Boards category; Ok well if anyone cares I finally got it to work, thanks to someone at the beginning who told me ...

  1. #31
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok well if anyone cares I finally got it to work, thanks to someone at the beginning who told me to use read... anyway I attached the encrypt and decrypt programs, if anyone wants to see them.



    Actually, I now noticed that after decrypting the file, it works just fine, and the file looks okay at first glance, but there's actually a bunch of ` things in it. For example, I encrypted a random file on my hard drive (which happens to be a Java readme file- go figure) and this is what happens (it's not code, but it's easier to read):

    Code:
         The Java 2 SDK software includes tools useful for developin`g and
         testin`g pro`grams written in the Java pro`grammin`g lan`gua`ge and
         runnin`g on the Java platform. These tools are desi`gned to be used
         from the command line. Except for appletviewer, these tools do not
         provide a `graphical user interface.
    My code is attached to this post, did I do something wrong? I know somebody mentioning something about my variable "k" but I don't know what I was supposed to do. Can anyone help me?
    Attached Files Attached Files
    Last edited by mikeman118; 08-09-2007 at 12:45 PM. Reason: Doesn't Work

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code contains hardcoded paths, and tries to delete files that aren't related to what the user is doing.

    Clearing the screen should really be something the user chooses to do unless your application is "full-screen" (which yours isn't and shouldn't be).

    I don't see the purpose of having two source-files for the encryption and decryption, you should be able to do both in one.

    --
    Mats

  3. #33
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok I do weird things, lets get that out of the way. I was tired of deleting them manually. This is for myself, I'm not planning on distributing it. With that out of the way, does anyone know what's wrong?

  4. #34
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by mikeman118 View Post
    BTW, if you think I should just give this up and do something else, just tell me. All I really need to know is how to read an entire text file.
    STL can be powerful:

    Code:
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    using namespace std;
    
    string file_data;
    
    cin >> noskipws;
    // Read entire file using a single line of code
    copy(istream_iterator<char>(cin), istream_iterator<char>(), back_inserter(file_data));
    I wrote "using namespace std" to reduce verbosity, not because I necessarily think it's good :-)

  5. #35
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    BTW those two files that I have it delete at the bottom are files that my compiler makes when I compile things, so that's why I have it delete them, so I don't have to.

  6. #36
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Memory leaks. You also have some issues where the pointer could be NULL but you don't check for it in main.

    In you stream open function you don't clean up on a bad open. That function should clean up that pointer on a bad open.

    In main since you are working with that pointer you should check for NULL when you call the open function and if its NULL break out. If everything goes through ok, you should deallocate that pointer with delete at the end of main.

    You XOR function seems a bit odd in how it handles the key index. I would probably prefer something like this.
    Code:
    k = k + 1 < klen ? k + 1 : 0;
    Last edited by prog-bman; 08-09-2007 at 01:00 PM.

  7. #37
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by prog-bman View Post
    You XOR function seems a bit odd in how it handles the key index. I would probably prefer something like this.
    Code:
    k = k + 1 < klen ? k + 1 : 0;
    Or...

    Code:
    if(++k >= klen) k = 0;

  8. #38
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote:
    Originally Posted by mikeman118 View Post
    BTW, if you think I should just give this up and do something else, just tell me. All I really need to know is how to read an entire text file.
    STL can be powerful:

    Code:

    Code:
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    using namespace std;
    
    string file_data;
    
    cin >> noskipws;
    // Read entire file using a single line of code
    copy(istream_iterator<char>(cin), istream_iterator<char>(), back_inserter(file_data));
    I wrote "using namespace std" to reduce verbosity, not because I necessarily think it's good :-)
    Ok what am I supposed to do with this? I mean, I put it in my code but I'm asking when I'm running my program, what am I supposed to enter? When I tried it, it wouldn't end uhhh... cin-ing, as in it wouldn't stop asking for more data. How do I terminate it?

  9. #39
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    mike that code isn't supposed to be taken literary. We are just posting examples how to do stuff. It's your job to take those examples and see if you want to incorporate them into your code.

  10. #40
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok well now I feel even stupider than I did before... oh well. I'm toying with the memory thing -- as in, what you told me to do with the program.

  11. #41
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by mikeman118 View Post
    How do I terminate it?
    Ctrl-Z under Windows, and Ctrl-D under Linux is EOF character.

  12. #42
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mikeman118 View Post
    Ok I do weird things, lets get that out of the way. I was tired of deleting them manually.
    Why do they need deleting at all? You get a new one every time you compile, so why not just let it be there - it's not like modern computers are going to run out of disk-space like what was a problem when a hard-disk was 10MB...

    If you really want to delete files automatically, try using a batch-file or some such. Or write a program that specifically deletes files you don't want. It is completely out of place where you put it - and it will give you an error message when you use the program a second time, as the file is no longer there (ok, so maybe it's so quick that you don't SEE the error message, but it's still happening).

    --
    Mats

  13. #43
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, your openfile doesn't return ANY value if the open fails - this means that the return value is some random goobledegook that is then used as a pointer - it will probably crash if you do that. You should return NULL so as to signal that it failed.

    Also, for future use, you may want to allow more than one encrypted text to exist, so maybe you should just merge the two source-codes to one application, and just decrypt/encrypt all in one thing - that's the beuty of a XOR encryption - it's exactly the same thing to encrypt and decrypt - the only difference is what you've got as an input - clear text or encrypted text.

    If you really want to delete files, perhaps you should figure out how to delete files within the application rather than calling system() - it is a fairly expensive operation to start another application, compared to the (relatively) simple operation of deleting a file - not that you NEED to delete the output file before writing to it if you use the right operation - also, if you run your decrypt - assuming you don't open the file for every single read and use append to avoid it being removed, that is. You SHOULD ONLY open a file once, unless it's ABSOLUTELY unavoidable to open it more than once.

    --
    Mats

  14. #44
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Sorry this is going on so long, but will these ideas fix it because unless I did something wrong (which I probably did) at the current moment it doesn't seem to have done anything, still have lots of ` s everywhere...

  15. #45
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your real problem, I think, is in how you use the key - but I don't know for sure. Follow some of the advice on how to restart at zero when k hits klen, because your current method only switches between 0 and 1 in a rather random fashion.

    None of the suggestions I've made will actually fix any clear bug, but they will prevent future failures.

    --
    Mats

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

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