# Vigenere Cipher C Program

By | November 5, 2017

Let us learn how to implement Vigenere cipher in C programming with its algorithm, explanation, output and much more.

#### What is Vigenere Cipher Algorithm?

The Vigenère cipher algorithm is a method of encrypting alphabetic text by using a series of interwoven Caesar ciphers based on the letters of a keyword.

This algorithm is easy to understand and implement and is an implementation of polyalphabetic substitution. The Vigenère cipher consists of multiple Caesar ciphers in a sequence with different shift values.

A popular cross-table called Vigènere square is used to identify elements for encryption and decryption based on Vigenere Cipher algorithm.

The Vigenere Cipher C program requires two inputs from the end user:

• Message
• Key

The algorithm generates a new key by repeating the user-entered key. The generated key automatically takes up the length of the original message entered by the user.

Now, check the initials of the message and the generated key. In this case, we have C as the initial value of the message and X as the initial value of the generated key.

Now, identify the element(character) that coincides with row C and the column X. It will be the encrypted message for that particular character of the original message.

#### Vigenere Cipher Encryption

Message: CODINGALPHA
Key: XYZ
Generated Key: XYZXYZXYZXY
Encrypted Message: ZMCFLFXJOEY

#### Vigenere Cipher Decryption

Encrypted Messaged: ZMCFLFXJOEY
Generated Key: XYZXYZXYZXY
Decrypted Message: CODINGALPHA

Note: This encryption and decryption algorithm of Vigenere Cipher algorithm in C programming is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.

#### Method 1: C Program For Vigenere Cipher Encryption and Decryption

```#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
int count, j;
char message[50];
char key[20];
printf("\nEnter Message To Encode:\t");
fflush(stdin);
scanf("%[^\n]s", message);
printf("\nEnter Key:\t");
fflush(stdin);
scanf("%[^\n]s", key);
int message_length = strlen(message), key_length = strlen(key);
char temp_key[message_length], encrypted_message[message_length], decrypted_message[message_length];
for(count = 0, j = 0; count < message_length; ++count, ++j)
{
if(j == key_length)
{
j = 0;
}
temp_key[count] = key[j];
}
temp_key[count] = '\0';
count = 0;
while(count < message_length)
{
encrypted_message[count] = ((message[count] + temp_key[count]) % 26) + 'A';
count = count + 1;
}
encrypted_message[count] = '\0';
count = 0;
while(count < message_length)
{
decrypted_message[count] = (((encrypted_message[count] - temp_key[count]) + 26) % 26) + 'A';
count = count + 1;
}
decrypted_message[count] = '\0';
printf("\n-------------------------------\n");
printf("\nIntial String:\t%s", message);
printf("\nKey:\t%s", key);
printf("\nGenerated Key:\t%s", temp_key);
printf("\nEncrypted Message:\t%s", encrypted_message);
printf("\nDecrypted Message:\t%s", decrypted_message);
return 0;
}```

#### Method 2: Implement Vigenere Cipher in C Programming

```#include<stdio.h>

int main()
{
int count, key_length, text_length, temp = 0;
int z[4][text_length];
char cipher_text[key_length], plaintext[text_length], key[key_length];
printf("\nEnter key length:\t");
scanf("%d", &key_length);
printf("\nEnter key length:\t");
scanf("%d", &text_length);
printf("\nEnter the key:\n");
for(count = 0; count < key_length; count++)
{
scanf("%c", &key[count]);
}
printf("\nEnter the plaintext:\n");
for(count = 0; count < text_length; count++)
{
scanf("%c", &plaintext[count]);
}
for(count = 0; count < text_length; count++)
{
if(65 <= plaintext[count] && plaintext[count] <= 91)
{
z[0][count] = plaintext[count] % 65;
}
else
{
z[0][count] = plaintext[count] % 97;
}
}
for(count = 0; count < text_length; count++)
{
printf("%d ", z[0][count]);
}
do
{
for(count = 0; count < key_length; count++)
{
if(65 <= key[count] && key[count] <= 91)
{
z[1][temp + count] = key[count] % 65;
}
else
{
z[1][temp + count] = key[count] % 97;
}
}
temp = temp + key_length;
}while(temp < text_length);
printf("\n");
for(count = 0; count < text_length; count++)
{
printf("%d ", z[1][count]);
}
printf("\n");
for(count = 0; count < text_length; count++)
{
printf("%d ", z[2][count]);
}
printf("\nEncrypted Cipher Text:\t");
for(count = 0; count < text_length; count++)
{
z[2][count] = (z[0][count] + z[1][count]) % 26;
cipher_text[count] = (char)(z[2][count] + 97);
printf("%c", cipher_text[count]);
}
return 0;
}```

#### Output

If you have any doubts about the implementation of Vigenere Cipher in C programming, let us know about it in the comment section. Find more about it on Wikipedia.

ENCRYPTION AND DECRYPTION ALGORITHMS
C Program For DES Algorithm
C Program For Caesar Cipher Algorithm
C Program For Triple DES Algorithm
C Program To Encrypt and Decrypt Text Files
Vernam Cipher Python Program

## 5 thoughts on “Vigenere Cipher C Program”

1. Nishi Thakkar

I am so confused! Is it Vigenere or Vignère cipher? Are these the same?

1. Meenal Deo

A person named Blaise de Vigenère contributed in the development of this algorithm and therefore, in French language, this is pronounced as Vigenère cipher. There is no difference between Vigenere and Vigenère apart from the pronunciation.

2. Priya Patil

What is the significance of [^\n] in the scanf statement above?

1. Shakti Mohan

This is an interesting question! 😀
When you use scanf(“%s”, &value), it allows you to input only a single word without any spaces. However, for a multi-word input in a string, you have to use scanf(“%[^\n]s”, &value).
Usually, when you’re given a long text file to encrypt or decrypt irrespective of the algorithm, there is a high chance that the file will contain spaces between words.