Vigenere Cypher Program In C

The content can be encoded and decoded through the Vigenere encryption method. Caesar ciphers are connected to the Vigenere encryption. It is a method for securing alphabetical text. The arrangement of letters in a keyword acts as its foundation. This substitution cipher utilizes polyalphabetic characters. Understanding and implementing this technique is straightforward. Giovan Battista Bellaso initially presented this algorithm in 1553.

Algorithm:

  1. Enter the term and the plaintext .
  2. To make the keyword the length of the plaintext , repeat it.
  3. Implement modular addition of the repeated keyword and the plaintext when encrypting data .

Ci = Pi + Ki(mod m)

Where Ci is the cipher text ,

Pi is the plaintext ,

Ki represents the recurring keyword, while 'm' symbolizes the alphabet's length.

Perform modular subtraction of the key terms from the encrypted text during the decryption process.

Pi = Ci - Ki

(modular m)

  1. Present the encoded and decoded message accordingly.
  2. Example:

Now, let's explore a program to grasp the implementation of the Vigenere Cipher technique in the C programming language.

Example

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#include <stdlib.h>



void encrypt() {

    char plaintext[128];

    char key[16];

    printf("\nEnter the plaintext (up to 128 characters): ");

    scanf(" %[^\n]", plaintext); // Read input with spaces

    printf("Enter the key (up to 16 characters): ");

    scanf(" %[^\n]", key);



    printf("Cipher Text: ");

    for (int i = 0, j = 0; i < strlen(plaintext); i++, j++) {

        if (j >= strlen(key)) {

            j = 0;

        }

        int shift = toupper(key[j]) - 'A';

        char encryptedChar = ((toupper(plaintext[i]) - 'A' + shift) % 26) + 'A';

        printf("%c", encryptedChar);

    }

    printf("\n");

}



void decrypt() {

    char ciphertext[128];

    char key[16];

    printf("\nEnter the ciphertext: ");

    scanf(" %[^\n]", ciphertext);

    printf("Enter the key: ");

    scanf(" %[^\n]", key);



    printf("Deciphered Text: ");

    for (int i = 0, j = 0; i < strlen(ciphertext); i++, j++) {

        if (j >= strlen(key)) {

            j = 0;

        }

        int shift = toupper(key[j]) - 'A';

        char decryptedChar = ((toupper(ciphertext[i]) - 'A' - shift + 26) % 26) + 'A';

        printf("%c", decryptedChar);

    }

    printf("\n");

}



int main() {

    int option;

    while (1) {

        printf("\n1. Encrypt");

        printf("\n2. Decrypt");

        printf("\n3. Exit\n");

        printf("\nEnter your option: ");

        scanf("%d", &option);



        switch (option) {

            case 1:

                encrypt();

                break;

            case 2:

                decrypt();

                break;

            case 3:

                exit(0);

            default:

                printf("\nInvalid selection! Try again.\n");

                break;

        }

    }

    return 0;

}

Working mechanism:

  • The user is given a menu with the options to encrypt, decrypt , or exit the program.
  • If a user chooses to "Encrypt":

The user is asked to enter a plaintext message and a key by the program.

  • After that, the Vigenere encryption algorithm is used to construct the encrypted character by iteratively going through each character in the plaintext and the key.
  • The cypher text is printed using the encoded characters .
  • Whenever a user chooses "Decrypt" :

The software asks the user to enter a key and a ciphertext .

  • In order to produce the decrypted character , the Vigenère decryptio n formula is applied iteratively to each character in the ciphertext and the key.
  • The original plaintext of the decrypted characters is printed.
  • The user can repeatedly execute encryption and decryption processes because the program loops until the user chooses the "Exit" option .

Output:

Some expected output like:

Example

1. Encrypt

2. Decrypt

3. Exit



Enter your option: 1



Enter the plaintext (up to 128 characters): Hello World

Enter the key (up to 16 characters): KEY



Cipher Text: RIJVS UYVJN



1. Encrypt

2. Decrypt

3. Exit



Enter your option: 2



Enter the ciphertext: RIJVS UYVJN

Enter the key: KEY



Deciphered Text: HELLO WORLD



1. Encrypt

2. Decrypt

3. Exit



Enter your option: 3

Input Required

This code uses input(). Please provide values below: