Introduction

In this article, we'll talk about the Caesar Cipher Algorithm and create a programme that uses it. One of the most well-known and straightforward encryption methods is the Caesar Cipher. Each letter in the plaintext is replaced by a letter that is located a certain number of places farther down the alphabet in this form of substitution cypher. With a left shift of 3, for instance, D would become A, E would become B, and so on. At the end, C++ code is also given for complete coverage.

Understanding Cyphers

A cypher (or cypher) in cryptography is a set of predetermined procedures that may be used as a technique to achieve encryption or decryption. Encipherment is a different, less often used word. Information is transformed into a code or cypher when it is encoded or enciphered. In everyday speech, the terms "cypher" and "code" are interchangeable since they both refer to a series of operations that encrypt a message; nevertheless, in cryptography, especially classical cryptography, the ideas are different.

While cyphers often replace the same amount of characters as are entered, codes typically replace varying length strings of characters in the output. There are exceptions, and certain cypher systems may emit a small number of characters more or less than they did when they were entered.

Encryption

INPUT:

  1. key (key) [ideally integer, between 0-25]
  2. message (s) [string or text message]

OUTPUT:

  1. Encrypted message (t)

CODE

#include<iostream>
#include<string>
using namespace std;
int main(){
    int i,j,k;
    string s,t;
    int key;
    cout<<"Enter the key\n";
    cin>>key;
    cout<<"Enter the message\n";
    cin>>s;
    for(i=0;i<s.size();i++){
        t+=(s[i]-'A'+key)%26+'A';
    }
    cout<<"\n\nEncrypted message is "<<t<<'\n';
    return 0;
}

Enter the key

4

Enter the message

HELLOWORLD

Encrypted message is LIPPSASVPH


Decryption

INPUT:

  1. key (key) [ideally integer, between 0-25]
  2. message (s) [string or text message]

OUTPUT:

  1. decrypted message (t)

CODE

#include<iostream>
#include<string>
using namespace std;
int main(){
    int i,j,k;
    string s,t;
    int key;
    cout<<"Enter the key\n";
    cin>>key;
    cout<<"Enter the message to decrypt\n";
    cin>>s;
    for(i=0;i<s.size();i++){
        t+=(s[i]-'A'-key+26)%26+'A';
    }
    cout<<"\n\nDecrypted message is "<<t<<'\n';
    return 0;
}


Enter the key

4

Enter the message to decrypt

LIPPSASVPH

Decrypted message is HELLOWORLD

write your code here: Coding Playground

Algorithm Complexity

Time complexity:   O(N) -> N is the length of the given text Auxiliary space: O(N)

Advantages of Caesar Cipher

  1. Applying it is pretty simple.
  2. The simplest form of cryptography is this one.
  3. Its whole operation uses a single short key.
  4. It is ideal for a system if it doesn't employ sophisticated coding methods.
  5. It simply needs a little amount of CPU power.

Hope this article gives you a clear understanding of the Caesar Cipher Algorithm.