Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Math for Devs - Encryption Essentials

Posted on Sep 4 Encryption is something that we use extensively in computer science. It's an indispensable technique in systems connected through a network or those containing sensitive data. But what most people don't know, is that cryptography is essentially math. So when you encrypt a message, you are basically using a mathematical algorithm to hide your message. While encryption as a way to secret a message is central to computer science, its roots go much deeper. Ancient civilizations utilized encryption techniques long before the advent of modern computing. The first known evidence of cryptography was found in an inscription carved around 1900 BC in Egypt. Another famous early cryptographic method was the "Caesar Cipher," used by the Roman consul Julius Caesar to transmit secret messages to his generals during military campaigns.Cryptography assumed a more prominent role during the World Wars, especially during World War II. The Nazis developed the "Enigma" machine to encrypt radio transmissions, making communication more secure. The complexity of the Enigma encryption far surpassed that of the Caesar Cipher. It employed cipher keys for encrypting messages, and these keys were rotated daily. Polish cryptanalysts initially made significant progress in understanding how the Enigma worked. Later, Alan Turing and his team in Britain built on this work and developed a machine and algorithms to deduce the daily key settings of the Enigma. Number Theory: modular arithmetic, and prime numbers are widely used in public key cryptographic schemes.Probability and Statistics: Used to evaluate the randomness of algorithms, analyze potential vulnerabilitiesCombinatorics: Helpful in understanding the strength of cryptographic systems by determining possible combinations or permutations, especially in brute-force attacks. Permutations are also a way to ensure that the encryption algorithm is non-linear and "mixed" relative to the input.Linear Algebra: Plays a role in several cryptographic protocols and systems. Classic algorithms such as "Hill Cipher" are heavily based on linear algebra, more specifically matrices operations.Information Theory: Concepts like entropy are used to measure and ensure the unpredictability and randomness necessary in cryptographic systems. In this section we are gonna discuss some basic Cryptographic Algorithms, and how they use math in its implementations.The Caesar Cipher is one of the earliest and simplest forms of encryption. It involves shifting each letter in a message by a predetermined number of positions in the alphabet. In a way, it's a linear transformation of each letter in the text.Imagine the alphabet as a linear array of characters:A B C D ... ZWhen you want to encrypt a letter using the Caesar Cipher, you simply move it down (or up) by a certain number of spaces, wrapping around when you reach the end of the alphabet. This "certain number" is known as the shift.For instance, with a shift of 3:The formula for each letter transformation is essentially: C = (P+s)  mod 26C \space = \space (P + s) \space \space mod \space 26 C = (P+s)  mod 26Where:Given our example shift of 3, the message:would translate to:To decrypt, you'd shift back by the same amount.Though a Caesar Cipher is simple and easy to break today, it was innovative during its time and serves as a foundational concept in cryptography.In classical cryptography, the Hill cipher is a substitution cipher based on linear algebra. Invented by Lester S. Hill in 1929, it is a subsitution cipher just like Caesar Cipher, but it is more powerful because it uses a public key signature to encrypt the text.We can use this table as a base to the mod 26 alphabet mapping:Let's suppose we have the following message that we want to encrypt:With the text in hands, now we define our encryption key.In Hill Cipher, the encryption key is a square matrix, it means that its dimensions should be 2x2 , 3x3 , 4x4 and so on. For our example, let's use a 3x3 matrix, but keep in mind that for more secure keys you should use a bigger key matrix to make brute-force attacks more challenging.The second step involves dividing the input text into blocks. Given that we're working with a 3x3 matrix, it's necessary to segment the text into blocks of three characters each. So, at the end of this segmentation, our blocks will look like this:Observe that when spaces are removed, our sentence doesn't neatly divide by three. To fix this, we've duplicated the final character to ensure a complete block of three characters.To do this, we have to use the table above as a reference. With the table, we can map a letter to its corresponding number. Let's see how it works: ′′PEL′′→[15411]''PEL'' \rightarrow\begin{bmatrix}15 \\ 4 \\ 11 \\\end{bmatrix}′′PEL′′→⎣⎡​15411​⎦⎤​ ′′EIS′′→[4818]''EIS'' \rightarrow\begin{bmatrix}4 \\ 8 \\ 18 \\\end{bmatrix}′′EIS′′→⎣⎡​4818​⎦⎤​ ′′THE′′→[1974]''THE'' \rightarrow\begin{bmatrix}19 \\ 7 \\ 4 \\\end{bmatrix}′′THE′′→⎣⎡​1974​⎦⎤​ ......... ......... ......... ′′MEE′′→[1244]''MEE'' \rightarrow\begin{bmatrix}12 \\ 4 \\ 4 \\\end{bmatrix}′′MEE′′→⎣⎡​1244​⎦⎤​Note: I hadn't showed all the substitutions, so you need to do the rest with the other blocks. I did only the first 3 ones and the last.Now, we multiply each column vector of the text block, by the key matrix, and then apply the mod26mod 26 mod26 to the resulting matrix. mod26mod 26 mod26 is dividing a number by 26 and getting the remainderAfter doing all the multiplying operations, we already have the result matrices. So, the only thing left to do is to map the matrices items to the letters. For our case, the final result will be: In summary, while the Caesar Cipher involves a straightforward shift of letters, the Hill Cipher employs linear algebra to provide a more complex transformation. To decrypt, you essentially reverse the encryption process using the matrix's inverse.https://www.dcode.fr/hill-cipherhttps://cryptii.com/pipes/caesar-cipherhttps://en.wikipedia.org/wiki/Hill_cipherTemplates let you quickly answer FAQs or store snippets for re-use.I really liked this writeup as security and encryption are a favorite subject for me. Though once you get to areas like Elliptic Curve Cryptography, the math side can get a bit wild. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Connie Leung - Aug 16 Bry - Aug 27 ⚡ Nirazan Basnet ⚡ - Aug 27 ansango - Aug 16 Once suspended, josethz00 will not be able to comment or publish posts until their suspension is removed. Once unsuspended, josethz00 will be able to comment and publish posts again. Once unpublished, all posts by josethz00 will become hidden and only accessible to themselves. If josethz00 is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to José Thomaz. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag josethz00: josethz00 consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging josethz00 will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Math for Devs - Encryption Essentials

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×