tags:

views:

457

answers:

3

So with the help of you guys I finished creating my very simple image encrypter. It's enough to keep any non-tech person out, right? :P

Now to the next step. Someone suggested I use XOR. I read about XOR and it's basically a logical table that determines what the answer is between two bits, right?

Only when one is true, the statement is true.

0 0 = false 1 0 = true 0 1 = true 1 1 = false

Is this correct? So, how would I go about XOR encrypting an image?

Here's my previous way using a Caeser cipher.

private void EncryptFile()
    {            
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
        dialog.InitialDirectory = @"C:\";
        dialog.Title = "Please select an image file to encrypt.";
        byte[] ImageBytes;
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            ImageBytes = File.ReadAllBytes(dialog.FileName);

            for (int i = 0; i < ImageBytes.Length; i++)
            {
                ImageBytes[i] = (byte)(ImageBytes[i] + 5);
            }

            File.WriteAllBytes(dialog.FileName, ImageBytes);
        }            
    }

    private void DecryptFile()
    {
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
        dialog.InitialDirectory = @"C:\";
        dialog.Title = "Please select an image file to decrypt.";
        byte[] ImageBytes;
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            ImageBytes = File.ReadAllBytes(dialog.FileName);

            for (int i = 0; i < ImageBytes.Length; i++)
            {
                ImageBytes[i] = (byte)(ImageBytes[i] - 5);
            }

            File.WriteAllBytes(dialog.FileName, ImageBytes);
        }            
    }
+1  A: 

XOR is a logical operation between two bits. The benefit is that if you run the XOR a second time, it undoes the first time. So change the code to

ImageBytes[i] = (byte)(ImageBytes[i] ^ 5);
John Saunders
BTW, as has been said, this is not encryption - it's a demonstration that you don't understand encryption, and an invitation to view your unprotected data.
John Saunders
A: 

I'm sorry I didn't make this clearer in responding to your initial question.

XOR is just a bitwise operator. In C# the ^ character is used for this operation. Essentially you just need to replace

 ImageBytes[i] = (byte)(ImageBytes[i] + 5);

by

 ImageBytes[i] = (byte) (ImageBytes[i] ^ (byte) 0xA9);  // or some other value

The exact same line (as the second line) can be used for decoding, for ((X XOR Y) XOR Y) = X, whatever X and Y may be.

As hinted in my answer in the previous question, rather that using one single value to add (or to XOR), you should use a small array, (cycling through the array, over and over, possibly with some "twists", such as every other cycle in the array, only take the odd elements from the array). In this fashion the resulting encoded file is harder to "crack". (overall these encryption schemes remain quite simple and merely keep the non-technically oriented folks at bay).

mjv
Thank you for the detailed answer. I'll try completing my little encrypted with XOR and then move up the food chain accordingly. I'm just trying to learn and I'm bound to make newbie mistakes. :D
Sergio Tapia
A: 

Papuccino1, as you've probably figured out . . . using XOR isn't encryption in any real sense. This is a bit of a rambling answer, but this just 'hides' an image - it certainly doesn't encrypt it. But as an aside, an amusing side effect of XOR, as was alluded to in a previous message, is that it is 'reversible'.

Therefore

Vx XOR Vy => Vz where V is a byte array of some arbitrary length.

Let say that Vx is your image, you can create an array Vy of exactly Vx long of random numbers and use it to seed your 'XOR' engine to produce Vz. If you then discard Vx, and make Vy 'private' and known only to you now have 'hidden' image. You can then use Vz XOR vy => to get the original image. This is fast, especially if it's all done in memory. Interestingly enough, if you now logically replace 'Vn' with DISKn STRIPEs, then you have RAID 5! Imagine that the Vx that you discarded is 1 of 3 disk drives in a RAID5 set, and that drive went bad. Because it still has two drives left, and an XOR engine, you can re-create missing data on the fly. When a working drive is substituted, the XOR engine regenerates the original data. XOR is cool stuff . . . This really doesn't answer his first question, its late and my math might be off . . . sorry for the ramble!

TheEruditeTroglodyte