Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

On the simplest level, reversible operations such as XOR (pronounced "exclusive OR") form the foundation of most cryptography.

XOR acts like a toggle switch where you can flip specific bits on and off. If you want to "scramble" a number (a pattern of bits), you XOR it with a "secret" number. If you take that scrambled number and XOR it again with the same secret number, you get your original number back.

Encrypt a number (210) with a secret "key" (145).

                  210 XOR 145 gives you 65   ←-- your "scrambled" result
                                      |
                  +   now unscramble it   +
                  |
                  ↓
                  65  XOR 145 gives you 210  ←-- and back to your original number

This is a very rudamentary example. When you encrypt a sequence of numbers (or text or any pattern of bits) with XOR, you have a very basic cipher algorithm.

I wrote a blog about XOR encryption http://programmingconsole.blogspot.in/2013/10/xor-encryption-for-alphabets.html

Mathematically, XOR encryption/cipher is additive cipher, an encryption algorithm that operates according to following principles:

(A * B) + (!A * !B)

 A  B  A XOR B
 0  0     0
 1  0     1
 0  1     1
 1  1     0

xor operator is just like AND(*) and OR(+) operator To decrypt the cipher we just need to XOR the cipher with the key to regain the original text . The XOR operator is extremely common component in complex encryption Algorithms. Such a encryption can easily be broken by using a constant repeating key and using frequency analysis . But we change the key after each encryption breaking such encryption is computationally very hard such a cipher is called a stream cipher in which every next bit is encrypted using a different pseudo-random key , such a kind of encryption was used by Germans in theirs Lorentz cipher .

By using a truly random* stream of key the cipher is theoretically unbreakable hence unusable

I would recommend you to watch

BBC: Code Breakers Bletchley Parks lost Heroes documentary

It will give you real insights into the world of cryptography and encrypted bits . How important cryptography is ? Well it was the cause for the invention of computers.

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.

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.

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top