Thread: XOR'ed *smacks head*, need help with it

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    XOR'ed *smacks head*, need help with it

    Let's say I have this code block right here:
    Code:
    ch = ch ^ Password; // Encrypting...
    That encrypts my ch. Now would this decrypt my ch (back to normal)?
    Code:
    ch = Password ^ ch; // Decrypting...
    Because I was having trouble writing a program that takes a file and a key and then encrypts it, and then when the user specifies, decrypts it. It would encrypt the file, but never decrypt it.

    Thanks a bunch.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well it looks OK so far...

    Do you treat your files as text files or binary files?

    You have to be careful with text files, exclusive-oring a char with itself gets you '\0', which is usually pretty bad in most string contexts
    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.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    I believe I was treating them as text files unless I'm mistaken.

    Code:
    fstream FileE(argv[1], ios::in | ios::out | ios::nocreate);
    Could I treat the text file as a binary file, and then encrypt/decrypt it? If so, how?
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Trying to do input and output on the same file is trickier than normal. You have to keep using flush() and seek() to make sure you're reading and writing the correct data.

    It's normally a lot easier to create a temp file, then do delete/rename
    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.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    as salem was saying
    >> if(ch !=pwd)
    >> ch ^= pwd;
    tho I think you would have to store the last value of the encryption and then work backwords to decrypt it. Is that right?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-13-2003, 04:35 AM