XOR cipher is a simple additive encryption technique in itself but is used commonly in other encryption techniques. The truth table for XOR cipher is as below. If the bits are same then the result is 0 and if the bits are different then the result is 1.

 

Bit 1 Operation Bit 2 Result
0 0 0
1 0 1
0 0 1
1 1 0

 

Let’s take an example. We would encrypt Sun using the key 01010010 repeatedly .

Encryption
Text          |     S    |     u     |     n       |
ACII Code     |    083   |    117    |    110      |
Binary        | 01010011 |  01110101 |  01101110   |
Key           | 01010010 |  01010010 |  01010010   |
Cipher        | 00000001 |  00100111 |  00111100   |

Now if we XOR the cipher with the same key we will get back the out original text.

Decryption
Cipher        | 00000001 |  00100111 |  00111100   |  
Key           | 01010010 |  01010010 |  01010010   |
Output        | 01010011 |  01110101 |  01101110   |
ACII Code     |    083   |    117    |     110     |
Text          |     S    |     u     |      n      |

This encryption we just did was not very secure because used the same key over and over again. To make our encryption more secure we should use a unique key and not the one which is repetitive in nature. A good technique that could be used is One-time Pad. This makes the encryption much more secure to the brute force attack.

XOR encryption and decryption

The encryption and decryption using XOR has the same code. A python implementation for the same is below:

 

input_str = raw_input("Enter the cipher text or plain text: ")
key = raw_input("Enter the key for encryption or decryption: ")
no_of_itr = len(input_str)
output_str = ""


for i in range(no_of_itr):
    current = input_str[i]
    current_key = key[i%len(key)]
    output_str += chr(ord(current) ^ ord(current_key))

print "Here's the output: ", output_str

And here’s a sample run

Image showing sample run of ROT13 encoder decoder

Image showing sample run of XOR encryption and decryption

 

The entire source code for this post can be found at https://github.com/abhishuk85/cryptography-plays

Any questions, comments or feedback are most welcome.

Image showing sample run of ROT13 encoder decoder

ROT13 is a letter substitution cipher and a special case of Caesar Cipher where each character in the plain text is shifted exactly 13 places. If you are not aware of Caesar Cipher then look at Caesar Cipher. For example the cipher for SUN becomes FHA.

The cool thing about this technique is that if we do a ROT13 on the cipher text then we get back the plain text since each letter in the text is shifted by 13 places. For example when we do a ROT13 on FHA we get back SUN

 

A block representation of ROT13 encryption and decryption

A block representation of ROT13 encryption and decryption

 

ROT13 Encoder and Decoder

The encoder and Decoder for ROT13 is the same because there is no special logic during decoding since the shift for both encoding and decoding is the same. Below is the python code for the implementation of it. The code is pretty much the same as Caesar Cipher with the shift value set to 13 always.

 

alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

input_str = raw_input("Enter message that you would like to encrypt/decrypt using ROT13: ")
shift = 13
no_of_itr = len(input_str)
output_str = ""

for i in range(no_of_itr):
    current = input_str[i]
    location = alphabets.find(current)
    if location < 0:
        output_str += input_str[i]
    else:
        new_location = (location + shift)%26
        output_str += alphabets[new_location]

print "Here's the output: ", output_str

Here’s a sample run of ROT13

Image showing sample run of ROT13 encoder decoder

Image showing sample run of ROT13 encoder decoder

 

The entire source code for this post can be found at https://github.com/abhishuk85/cryptography-plays

Any questions, comments or feedback are most welcome.