Encrypt And Decrypt Vigenere Cipher In Python Tutorial | Techniculus

Encrypt And Decrypt Vigenere Cipher In Python Tutorial

The Vigenere Cipher is a classical cryptographic algorithm that was first described by Blaise de Vigenere in the 16th century. It is a polyalphabetic substitution cipher, which means that it uses multiple alphabets to encrypt plaintext. The Vigenere Cipher is considered more secure than monoalphabetic substitution ciphers, such as the Caesar Cipher, because it is resistant to frequency analysis attacks.

The Vigenere Cipher uses a keyword to determine which alphabet to use for each letter in the plaintext. The keyword is repeated as many times as necessary to match the length of the plaintext. For example, if the keyword is "SECRET" and the plaintext is "HELLO", the keyword is repeated to form "SECRE" and then the alphabets are shifted accordingly.

To encrypt the plaintext, the Vigenere Cipher uses a table known as the Vigenere Square or the Tabula Recta. The table is a square matrix containing all possible combinations of letters in the alphabet. The first row and first column of the matrix are the letters of the alphabet in order. Each subsequent row is the alphabet shifted by one position to the right.

Vigenere Square:

 

 

To encrypt a plaintext letter using the Vigenere Cipher, the corresponding letter in the keyword is used to determine the row of the Vigenere Square to use. The plaintext letter is then found in the first column of the table, and the letter in the corresponding row and column is the ciphertext letter.

For example, to encrypt the plaintext letter "H" using the keyword "SECRET", the letter "S" is used to determine the row of the Vigenere Square to use. The plaintext letter "H" is found in the first column of the table, and the letter in the "S" row and "H" column is "K", which is the ciphertext letter.

The Vigenere Cipher can be decrypted using a similar process. The ciphertext letter is first found in the first column of the Vigenere Square, and the corresponding row is determined by the letter of the keyword used to encrypt that letter. The plaintext letter is then the letter in the first row of the table that corresponds to the ciphertext letter.

One of the strengths of the Vigenere Cipher is that it is resistant to frequency analysis attacks. In a monoalphabetic substitution cipher, the frequency of each letter in the ciphertext can be used to determine the corresponding letter in the plaintext. However, because the Vigenere Cipher uses multiple alphabets, the frequency of each letter in the ciphertext does not provide any useful information about the plaintext.

However, the Vigenere Cipher is not without its weaknesses. One of the most significant weaknesses is that it is vulnerable to a known plaintext attack. If the attacker knows some of the plaintext, they can use it to determine the keyword and then use the keyword to decrypt the rest of the ciphertext.

Creating a Vigenere Cipher Program in Python:

We will start by creating a function to encrypt messages using the Vigenere Cipher. Here is the code:

In this function, we first convert the message and keyword to uppercase using the upper() method. We then initialize variables to hold the encrypted message and the index of the keyword.

We then loop through each letter in the message using a for loop. If the letter is not a letter, we add it to the encrypted message as is. Otherwise, we determine the shift value using the ord() function to convert the letter to its ASCII code and subtracting 65 (the ASCII code for "A").

We then shift the letter using the shift value and add the shifted letter to the encrypted message. We move to the next letter in the keyword by incrementing the keyword index and using the modulus operator (%) to wrap around to the beginning of the keyword if we have reached the end.

Finally, we return the encrypted message.

Now that we have created the Vigenere Cipher program, we can test it by encrypting a message. Here is an example:

This should output the encrypted message "RIJVS ZBYGB".

To decrypt the Vigenere Cipher, we can use a similar approach. We create a function to decrypt messages using the Vigenere Cipher. Here is the code for decrypting the Vigenere Cipher:

This function is similar to the encryption function, but it uses a negative shift value to shift the letter in the opposite direction. This returns the original letter.

We can test the decryption function by decrypting the encrypted message we created earlier:

This should output the original message "HELLO WORLD".

In this tutorial, we learned how to encrypt and decrypt messages using the Vigenere Cipher in Python. The Vigenere Cipher is a classical encryption method that uses a polyalphabetic substitution algorithm to encrypt and decrypt messages. We created functions to encrypt and decrypt messages using the Vigenere Cipher and tested them with an example message. The Vigenere Cipher is a simple encryption method that can be easily implemented in Python.

No comments:

Powered by Blogger.