Today we'll look at the only PERFECTLY secure way to encrypt your messages: The One-Time-Pad. As the name implies it can be only used at most once. Otherwise it's not PERFECTLY secure anymore.
After this explaining post you'll be able to understand a simple but perfect method to encrypt your messages and why it does not work with reusing the key.
In history this was used in the Cold War.
Every text can be coded into a string of ones and zeroes. This can be done by using a standard coding. ASCII is an example for that.
The string of 1s and 0s is equivalent to a number which is assigned to a letter in this example.
This is how computers actually work, with ones and zeroes.
Please read online how binaries work and come back, or comment that you would like to have a post about binary numbers.
This means we can simply use number as messages.
The next thing we need to know: Thebitwise XOR function.
The XOR-function is the "Exclusive OR".
How does it work?
It takes two bitstring and for each pair of bits it puts as a result:
As you see: if the bits at the same place in both inputs are the same then the result bit in the same place is 0, otherwise it's 1.
So we'll look at the message: "Hello, World!". We'll use the variable m for the message.
"Hello World!" is equal to
m =
01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
Find a key. It is important that the key has the same length as the message m. The key k must also be a binary!!!!
We''ll take now for example the key: "Go Steemians!"
k =
01000111 01101111 00100000 01010011 01110100 01100101 01100101 01101101 01101001 01100001 01101110 01110011 00100001
Do a bitwise XOR with the key and the message.
k XOR m is
01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
XOR
01000111 01101111 00100000 01010011 01110100 01100101 01100101 01101101 01101001 01100001 01101110 01110011 00100001
__________________________________________________________________________ EQUALS
00001111 00001010 01001100 00111100 00011011 01001001 01000101 00111010 00000110 00010011 00000010 00010111 00000000
This is our result of the calculation. Which we will call ciphertext c. (For completion: c = m XOR k)
Here we get this as text something we cannot not display.
Which shows us we encrypted it, but the result doesn't look beautiful. In the result we use numbers not defining a certain letter or sign. But that doesn't matter since you don't need to read the encrypted message. Therefore we will now look at how to decrypt!
You received a message. For decrypting it you need obviously the key. The key must be known! Let's say: You and your friend met alone at your place and you gave him a list of keys of the same length for your messages. So as previously assumed the receiver gets the key.
When receiving the message, the only thing the receiver needs to do is:
XOR the key and the ciphertext and he gets the message which was sent.
m = k XOR c
Okay, concentrate. We want to send two messages m and m'
Let's assume you send the ciphertexts:
Of course your friend will be able to decrypt it, but an intruder who is checking your messages and doesn't know your key k can still learn SOMETHING about your messages.
Because if he does as follows:
c XOR c' = m XOR k XOR c' XOR k = m XOR m'
He can learn something about your messages. Imagine that problem if you used the key more often!
You can find tons of good material online, but if you want I can provide some code!
The One-Time-Pad is provenly (the prove would be to much for this post) the only perfectly secure encryption. We have seen how to encrypt and decrypt messages.
But we have also seen WHY the reuse of keys can be a problem. That's why the One-Time-Pad is not often used everyday. It's unpractical but beautiful.