Imagine a situation where you need to pass some important information to your friend, but it is rather secretive, so you are really afraid that someone may read it. What might be a solution in such a case? Using the cipher, of course! However, using some trivial ones may be a bad idea, because they are easy to break in most cases. So, why don't you learn about the XOR cipher and use it for your needs?
Algorithm
The algorithm, as its name says, relies on the concept of the exclusive OR (shortly, XOR). In our case, it is a bitwise operator, that takes two operands and returns 1 only if strictly one of them equals 1. In other words, it returns 1 in case, when the values of the operands are different. You can look at the output of this operator for different input values in the table below:
You might wonder, how these numbers may be used while dealing with the encryption and decryption of the messages. Well, the thing is that each symbol can be represented by a number, which you can find in some tables (for example, ASCII, UTF-8, Unicode, etc.). And, as you might have already guessed, these numbers can be easily converted into the binary system. For example, this is what the ASCII table looks like:
You can see, that the symbol "H", for instance, is represented by the number 72, which is 0100 1000 in binary.
However, don't be afraid that you'll have to always look for a symbol and its binary version in a table manually. The programming languages that you use to write the programs already have functions to convert symbols into numbers and vice versa, so you can use them for your needs.
Now we can move on to the algorithm itself. For encrypting and decrypting the messages you need to know the key. It can be a word or even a phrase. Then you do the following:
- Repeat the key so that its length reaches one of the messages.
- For each symbol of the message and the corresponding one of the key get their binary numeric representations and XOR them.
- Convert the result of the XOR operation back into a symbol.
That's it! The interesting thing about this particular cipher is that you need to perform exactly the same steps both for encryption and decryption – there are no differences between these two processes.
Example
Let's get down to practicing the freshly learned theory! For example, let's encrypt the message "Encryption" with the key "Cryptography".
From the table above you can find the numeric representation of the letters 'E' and 'C', which are 69 and 67 respectively. You can also find binary values in the same table and perform the XOR operation:
The character behind this code is non-printable – ACK. In case when the encrypted message contains such symbols, but you want to print the result out, you may print the codes and not the characters themselves.
Then you repeat this process for the rest of the letters of the word. Try to do it yourself and check with the table below:
Now, why don't we try decrypting the message? As stated previously, the process is the same, as in the case of encrypting. So, imagine that you've received the message "" and want to decrypt it with the use of the key "".
To do so, you take the first symbols of the word and the key ('*' and 'K' respectively) and find their numeric representations in the table. They equal 42 and 75. Now you convert them to the binary system and perform the XOR operation:
Once again you refer to the table of the symbols and find the one with the number 97 near it – a character 'a'.
After you repeat these steps for the other letters of the word, you get the following result:
Of course, it might be tiring to complete all these steps over and over again manually. Therefore, you can simplify the process by using your coding skills! For example, the pseudocode may look like this:
class XorCipher is
method algorithm(message, key) is
keyRepeated = key
result = ""
while (length(key) < length(message)) do
keyRepeated += key
for i in length(message) do
resultCode = code(message[i]) XOR code(key[i])
result += char(resultCode)
return result Conclusion
So, let's once again go through the main points about the XOR cipher:
- In order to encrypt or decrypt the messages you need to know the key.
- The XOR cipher is based on the XOR operation. The operands are the letters of the word and the key.
- To find the numeric representation of the symbol you may use tables for different encoding standards – ASCII, UTF-8, Unicode, etc.
- Encryption and decryption are executed in the same way: you perform the XOR operation between each letter of the message and the corresponding one of the key.