tags:

views:

548

answers:

7

I have heard about people starting encryption and thought it may be someething I would like so I checked XOR and can't make any sense of it. So can someone tell me what XOR is ?

+8  A: 

XOR, or 'exclusive or' is a 2 operand logical operation defined as:

(a and b) or (not a and not b)

 a  b  result
 0  0  0
 1  0  1
 0  1  1
 1  1  0

The critical feature of XOR with respect to encryption is it is reversible, ie where C = A XOR B, then you can get back A using A = C XOR B.

So for a stream of plaintext A, and a key of the same length B, you can generate cryptotext C, and send that to the recipient.

The recipient, who has a copy of B in his safe, can do C XOR B and regenerate A.

Alex Brown
If B is used only once, then this is a one-time pad encryption, the only kind of encryption that is theoretically unbreakable.
Laurynas Biveinis
+11  A: 

you take a key, such as 0101, then you use that to XOR your string (in binary format) to achieve an encrypted string.

0101 XOR <-- key
1011 <---- original message
----
1110 <-- send message

You send 1110 to your receiver. That receiver, then takes the received string and XORs it with the key to obtain the original message:

1110 XOR <--- received message
0101 <-- key
----
1011 <--- original message
yx
A: 

XOR is short for 'exclusive or'. A XOR B is true if A is true, or if B is true, but not if both A and B are true.

It is used for cryptography because A XOR B XOR A is equal to B - so if you can use A as a key for both encryption and decryption.

fiirhok
+1  A: 

XOR is a logical operation, pronounced exclusive or. It can be used to cipher messages simply and fast. You can see a truth table for this operation here: http://mathworld.wolfram.com/XOR.html

quasi-pseudo code implementation (via http://www.evanfosmark.com/2008/06/xor-encryption-with-python/):

#!/usr/bin/env python

from itertools import izip, cycle

def xor_crypt_string(data, key):
    return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))

my_data = "Hello. This is a secret message! How fun."
my_key= "firefly"

# Do the actual encryption
encrypted = xor_crypt_string(my_data, key=my_key)

print encrypted
print '---->'

# This will obtain the original data from the encrypted
original = xor_crypt_string(encrypted, key=my_key)

print original

Output:

.     BY2F
FRR
DF$IB
---->
Hello. This is a secret message! How fun.
The MYYN
A: 

It should be noted, that this method of encryption can hardly be considered secure. If you encrypt any common file (PNGs, JPGs, etc.) where the header is well known, the key can easily be derived from the encrypted content and the known header.

Timothy Baldridge
Actually, it depends on the implementation. If your key length is less then the message length, the key is going to be repeated and it can be derived. If the key length is the same as the message length, and you never use the same key twice, you essentially have a one-time-pad which cannot be broken.
Marko
+2  A: 
Robert Cartaino
Not really the basis of *all* cryptography since block ciphers have several modes that never do a single XOR such as ECB, OFB, and CFB. Also any reversible operation can take the place of XOR, such as mod 256 addition/subtraction as one common example.
GregS
I clarified the post a bit.
Robert Cartaino
A: 

XOR encryption can also be used in cipher block chaining. XOR CBC is used as an addition to many encryption implementations. There is a google code project that makes use of this by itself, although XOR alone is not very secure: http://code.google.com/p/xorencryption/

pokstad