Why is Exclusive Or (XOR) Important in Cryptography?
Tags: cryptography, go, security, cipher
Published: 2020-01-18 13:28:52

---
title: Why is Exclusive Or (XOR) Important in Cryptography?
published: true
date: 2020-01-18 13:26:21 UTC
tags: cryptography,golang,security,cipher
canonical_url: https://qvault.io/2020/01/18/why-is-exclusive-or-xor-important-in-cryptography/?utm_source=rss&utm_medium=rss&utm_campaign=why-is-exclusive-or-xor-important-in-cryptography
---

![](https://qvault.io/wp-content/uploads/2020/01/binary-code-on-pixellated-screen-johan-swanepoel.jpg)

If you are getting into cryptography, or just trying to understand the fundamentals, you may have noticed that the exclusive-or operation is used quite often, especially in ciphers.

## What is XOR ( **⊕** )?

XOR, or “exclusive or” operates on binary data. It returns true if both of its inputs are opposites (one false and one true), otherwise, it returns false.

![XOR example](https://qvault.io/wp-content/uploads/2020/01/Screenshot-from-2019-08-04-12-01-49.png)

An example in go code would be something like:

```
func exclusiveOr(a bool, b bool) bool {
	return a != b
}
```

## XOR Cipher – The Perfect Cipher

The XOR operation can be used as a simple cipher for encrypting and decrypting messages with a single key. This is known as symmetric encryption.

It is interesting to note that if:

1. The key is the same size as the message
2. The key is kept secret and generated truly randomly

Then the cipher is **impossible** to crack. This is known as a [one time pad](https://en.wikipedia.org/wiki/One-time_pad). However, a simple XOR shouldn’t be used in production due to the key length needing to be too long to be practical.

## Cipher Example

As a simple example, let’s encrypt the word “hi”

1. Convert “hi” to binary, here is a free tool: [https://www.rapidtables.com/convert/number/ascii-to-binary.html](https://www.rapidtables.com/convert/number/ascii-to-binary.html))

```
01101000 01101001
```

2. Create a random secret key that has the same length:

```
01010010 01000101
```

3. Create an encrypted message by XOR’ing the message and the key:

```
01101000 01101001 ("hi") XOR 01010010 01000101 (secret key) = 00111010 00101100 (encrypted message)
```

4. Decrypt the message by XOR’ing the key with the encrypted message again:

```
00111010 00101100 (encrypted message) XOR 01010010 01000101 (secret key) = 01101000 01101001 ("hi")
```

## Why does it work?

XOR works as a cipher because it is its own inverse.

> 𝑎 = (𝑎 **⊕** 𝑏) **⊕** 𝑏

And, as we demonstrated in our example:

> _encrypted_ = _message_ **⊕** _key_  
> and  
> _message_ = _encrypted_ **⊕** _key_

## Is XOR used in production ciphers?

The simple XOR cipher isn’t used in production because it is impractical to use keys that are the same length as the message body. However, the XOR is still extremely useful. In fact, it is used in almost all symmetric encryption algorithms. XOR is the primary operation in the [“add round key” step of AES-256](https://qvault.io/2020/01/02/very-basic-intro-to-aes-256-cipher/). It is also used in the [DES cipher.](http://page.math.tu-berlin.de/~kant/teaching/hess/krypto-ws2006/des.htm)

## Thanks For Reading

Lane on Twitter: [@wagslane](https://twitter.com/wagslane)

Lane on Dev.to: [wagslane](https://dev.to/wagslane)

Download Qvault: [https://qvault.io](https://qvault.io/)

The post [Why is Exclusive Or (XOR) Important in Cryptography?](https://qvault.io/2020/01/18/why-is-exclusive-or-xor-important-in-cryptography/) appeared first on [Qvault](https://qvault.io).