What is XOR Encryption?
Asked Answered
G

8

15

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 ?

Greenery answered 8/1, 2010 at 17:35 Comment(1)
en.wikipedia.org/wiki/XOR_cipherKarlsruhe
K
6

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.
Karlsruhe answered 8/1, 2010 at 17:40 Comment(0)
D
28

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
Designation answered 8/1, 2010 at 17:38 Comment(0)
P
11

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.

Profess answered 8/1, 2010 at 17:38 Comment(1)
If B is used only once, then this is a one-time pad encryption, the only kind of encryption that is theoretically unbreakable.Zacatecas
K
6

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.
Karlsruhe answered 8/1, 2010 at 17:40 Comment(0)
H
2

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 67   ←-- your "scrambled" result
                                      |
                  +   now unscramble it   +
                  |
                  ↓
                  67  XOR 145 gives you 210  ←-- and back to your original number

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

Holstein answered 8/1, 2010 at 17:41 Comment(1)
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.Bibliomania
S
1

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.

Strong answered 8/1, 2010 at 17:40 Comment(0)
B
1

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.

Becki answered 8/1, 2010 at 17:41 Comment(2)
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.Primitive
However, then you have to find a way to securely transmit the key to the recipient - and if you could do that, you could have just sent the message using that secure channel instead. (Hence why OTP is not used much in the real world).Kalmick
A
1

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.

Attribute answered 19/10, 2013 at 19:14 Comment(0)
X
0

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/

Xenon answered 8/1, 2010 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.