Cryptography introduction and cheatsheet
Cryptography, an interesting word most people do not understand. It is nothing more than the art of sending messages which are protected from spies, you convert them into a secret code with the use of an algorithm called "cipher". It has altered the course of history because it has been decisive during war times.
There are many ciphers out there, some of which have been used for centuries, some which are still being used today, specially when you use the internet, after all it is necessary to make sure you can access your bank account, send Whatsapp messages and shop online with security, we can't allow some hacker to tap into your internet transmission and steal all your data, therefore everything must be encrypted before it leaves your computer, and it must be decrypted as soon as it arrives at its destination, which could be someone else's computer or some data center.
Although it seems complex, the principles are actually simple, all you need to do is to figure out how to turn each letter of your message into a different letter, making it impossible for someone to understand the original meaning, the only problem is that you must use an algorithm which is complex enough to make sure no one will easily crack it with brute force. This is the soul of cryptography, using your creativity to invent ever more complex algorithms to resist the exponential growth of computing power available in our CPUs, specially now that they have created quantum computers.
Rest assured that although they have created quantum processors, they have also created a new science called "quantum cryptography", which makes it impossible to decrypt the code regardless of your computing power. However, this article is not about anything related to the quantum world, this article is about some of the most popular ciphers out there, there is a list of them below.
Try encrypting the message "I LOVE YOU" using each one of the ciphers below, then try to decrypt it, you will discover it is so much fun, cryptography is something that seduces anyone, I myself fell in love with it. Have fun!
Caesar cipher
Write the message.
Make K = the key value.
For each character in the message you will find its position in the alphabet starting at 0, and assign it to the variable X.
Then you will make Y = (X + K) % 26.
Then you will find the character that corresponds to the Y number in the alphabet, starting at 0.
In order to decipher the message you must do the exact same process above, but on step 4 you will use the formula Y = (X + 26 - K) % 26.
Frequency analysis
Make a bar graph with the percentages of the letters used in the cipher, meaning you should check what percentage of the cipher consists of the letter A, then the letter B, and so on.
Compare this graph with the fingerprint of natural languages, such as English, German, Spanish, etc... until you find the best match.
Then you check the distance between the bars in the graph, that should be the key to break the code.
This technique only breaks the Caesar cipher.
Polyalphabetic cipher (Vigenere)
Choose a word to be the key, such as BEER.
Find the alphabet number of each letter, meaning B = 2, E = 5 and R = 18, meaning BEER = 2 5 5 18.
Now use those numbers repeatedly to shift the letters of the message, meaning the first letter is shifted by 2, the second by 5, the third by 5 and the fourth by 18, then you repeat the key numbers for the next letters.
In order to do the shifts you can use the Caesar cipher algorithm described above.
In order to decrypt the message you will use the sequence of numbers and the decryption technique of the Caesar cipher algorithm above.
Polyalphabetic cipher frequency analysis
You first need to know the size of the key word.
Once you know the size then you will group letters based on their positions, e.g. if the size of key word is 5 then you should group every fifth letter.
On each subgroup of letters you will apply the Frequency analysis algorithm above, meaning each subgroup will be treated as an individual Caesar cipher.
One-time pad
Write the message.
Calculate the number of characters in the message and store it in the variable charNum.
Use an RNG to generate an array of random numbers, in the range [1, 26], the size of the array will be equal to charNum.
For each letter in the message you will apply the Caesar cipher using the letter and its corresponding random number in the array, so the 1st letter is shifted by the 1st number, the 2nd letter is shifted by the 2nd number, and so on.
This is it, you now have the encrypted message with perfect secrecy.
In order to decipher the message you will need to have a copy of the array of random numbers and then you will apply the Caesar cipher reversal method above to all characters with their corresponding random number.
Frequency stability property
Get the sequence of "random" numbers and divide them into groups of a certain length, it could be groups of 3, or groups of 4.
Then you make the frequency analysis of those groups to check if certain groups appear more often than others.
If certain groups appear way more often than others then you can bet it was not truly random. For example, if "010" appears way more often than "111", then it was probably not truly random.
Image file encryption
Convert the image file to its binary representation, meaning the plain text image, this will be the message to be encrypted.
Create a list of random binary that is as long as the plain text image, this will be the key.
Now perform the XOR operation between the message and the key, the result will be the encrypted cipher text image.
To decrypt it you must simply perform the XOR operation between the binary of the encrypted plain text image and the key.
Notes
The one-time pad has perfect secrecy because the message space size is equal to the key space size which is equal to the cipher text space size.
All text ciphers above are shift ciphers.
You may use a pseudo-random number generator with the one-time pad, that way you only need to share the seed instead of the whole list of random numbers with the recipient.
Writing the message using words from different languages may be a way to deceive the Frequency analysis.
Comments
Post a Comment