Vigenere Cipher in C and C++

In this instructional exercise, you will find out about vigenere cipher in C and C++ for encryption and decryption.

Vigenere Cipher is somewhat polyalphabetic substitution strategy. It is utilized for

encryption of alphabetic content. For encryption and decryption, Vigenere Cipher Table is utilized in

which letters in order from start to finish are written in 26 lines.

Vigenere Cipher Encryption

Message Text: JUSTTECHREVIEW

Key: HELLO

Here we need to acquire another key by rehashing the given key till its length become equivalent to unique message length.

New Generated Key: HELLOHELLOHELLOHEL

For encryption take the first letter of message and new key for example T and H. Take the letters in order in

Vigenere Cipher Table where T line and H section harmonizes for example A.

Rehash a similar procedure for every single outstanding letter set in the message content.

At long last the encoded message content is:

Encoded Message: ALPNFHDJAFVKCLATIC

The calculation can be communicated in arithmetical structure as given underneath.

The cipher content can be created by underneath condition.

Ei = (Pi + Ki) mod 26

Here P is plain content and K is vital.

Vigenere Cipher Decryption

Encoded Message: ALPNFHDJAFVKCLATIC

Key: HELLO

New Generated Key: HELLOHELLOHELLOHEL

Take first letters in order of scrambled message and produced a key, for example, An and H.

Break down Vigenere Cipher Table, search for letters in order An in segment H, the comparing line

will be the primary letters in order of unique message for example T.

Rehash a similar procedure for every one of the letters in order in the encoded message.

Unique Message: JUSTTECHREVIEW

The above procedure can be spoken to in arithmetical structure by the following condition.

Pi = (Ei – Ki + 26) mod 26

We will use the above arithmetical conditions in the program.

Program for Vigenere Cipher in C

#include<stdio.h>
#include<string.h>
 
int main(){
    char msg[] = "JUSTTECHREVIEW";
    char key[] = "HELLO";
    int msgLen = strlen(msg), keyLen = strlen(key), i, j;
 
    char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];
 
    //generating new key
    for(i = 0, j = 0; i < msgLen; ++i, ++j){
        if(j == keyLen)
            j = 0;
 
        newKey[i] = key[j];
    }
 
    newKey[i] = '\0';
 
    //encryption
    for(i = 0; i < msgLen; ++i)
        encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
 
    encryptedMsg[i] = '\0';
 
    //decryption
    for(i = 0; i < msgLen; ++i)
        decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
 
    decryptedMsg[i] = '\0';
 
    printf("Original Message: %s", msg);
    printf("\nKey: %s", key);
    printf("\nNew Generated Key: %s", newKey);
    printf("\nEncrypted Message: %s", encryptedMsg);
    printf("\nDecrypted Message: %s", decryptedMsg);
 
	return 0;
}

Output

Original Message: JUSTTECHREVIEW
Key: HELLO
New Generated Key: HELLOHELLOHELLOHEL
Encrypted Message: ALPNFHDJAFVKCLATIC
Decrypted Message: JUSTTECHREVIEW

Program for Vigenere Cipher in C++

#include<iostream>
#include<string.h>
 
using namespace std;
 
int main(){
    char msg[] = "JUSTTECHREVIEW";
    char key[] = "HELLO";
    int msgLen = strlen(msg), keyLen = strlen(key), i, j;
 
    char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];
 
    //generating new key
    for(i = 0, j = 0; i < msgLen; ++i, ++j){
        if(j == keyLen)
            j = 0;
 
        newKey[i] = key[j];
    }
 
    newKey[i] = '\0';
 
    //encryption
    for(i = 0; i < msgLen; ++i)
        encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
 
    encryptedMsg[i] = '\0';
 
    //decryption
    for(i = 0; i < msgLen; ++i)
        decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
 
    decryptedMsg[i] = '\0';
 
    cout<<"Original Message: "<<msg;
    cout<<"\nKey: "<<key;
    cout<<"\nNew Generated Key: "<<newKey;
    cout<<"\nEncrypted Message: "<<encryptedMsg;
    cout<<"\nDecrypted Message: "<<decryptedMsg;
 
	return 0;
}

Remark beneath on the off chance that you have questions or discovered anything inaccurate in above instructional exercise for vigenere cipher in C and C++.

Leave a Comment

error: Alert: Content is protected!!