In cryptography, a **cipher** (or **cypher**) is an algorithm for performing encryption or decryption—a series of well-defined steps that can be followed as a procedure. An alternative, less common term is *encipherment*. To encipher or encode is to convert information into cipher or code. In common parlance, “cipher” is synonymous with “code“, as they are both a set of steps that encrypt a message; however, the concepts are distinct in cryptography, especially classical cryptography.

Codes generally substitute different length strings of character in the output, while ciphers generally substitute the same number of characters as are input. There are exceptions and some cipher systems may use slightly more, or fewer, characters when output versus the number that was input.

In this post, we will discuss the Vernam Cipher. Vernam Cipher is a cryptographic algorithm to encrypt and decrypt an alphabetic text. In this cipher, we first make the length of the key equal to the message length by repeating the key. Then, we add the ith letter of the message with the ith letter of the key by assigning each letter with the number(eg, A = 0, B = 1, C = 2, etc). If the sum is exceeding 26, then we take the modulus of the sum with 26 so that it is within the range of the alphabets. We can easily decrypt the encrypted message by reversing the process.

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of Vernam Cipher algorithm in C++, although, it’s very similar to C.

## Encryption

INPUT:

line 1: message (m)

line 2: key (key)

OUTPUT:

line 1: Encrypted message (ans)

The following is the Vernam Cipher encryption algorithm program in C++.

#include<bits/stdc++.h> using namespace std; int main(){ int t,n,i,j,k,sum=0; string m; cout<<"Enter the message"<<'\n'; cin>>m; string key; cout<<"Enter the key"<<'\n'; cin>>key; int mod = key.size(); j=0; for(i=key.size();i<m.size();i++){ key+=key[j%mod]; j++; } string ans=""; for(i=0;i<m.size();i++){ ans += (key[i]-'A'+m[i]-'A')%26+'A'; } cout<<"Encrypted message: "<<ans<<'\n'; return 0; }

OUTPUT:

Enter the message HELLOWORLD Enter the key TEST Encrypted message: AIDEHAGKEH

## Decryption

INPUT:

line 1: message (m)

line 2: key (key)

OUTPUT:

line 1: decrypted message (ans)

The following is the Vernam Cipher decryption algorithm program in C++.

#include<bits/stdc++.h> using namespace std; int main(){ int t,n,i,j,k,sum=0; string m; cout<<"Enter the message"<<'\n'; cin>>m; string key; cout<<"Enter the key"<<'\n'; cin>>key; int mod = key.size(); j=0; for(i=key.size();i<m.size();i++){ key+=key[j%mod]; j++; } string ans=""; for(i=0;i<m.size();i++){ ans += (m[i]-key[i]+26)%26+'A'; } cout<<"Decrypted message: "<<ans<<'\n'; return 0; }

OUTPUT:

Enter the message AIDEHAGKEH Enter the key TEST Decrypted message: HELLOWORLD

Let us know in the comments if you are having any questions regarding this cryptography cipher Algorithm.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You