The Caesar Cipher is one of the earliest encryption techniques. The data is encrypted by replacing each letter of the message with another letter after adding an integer key. This changes the ASCII value and converts the letter of the message into another letter.
The transformation of the data can be seen in the table below. We encrypt the data by adding a key n
that is 23, which right rotates the letter to 23 places and assigns the value.
The result of the encrypted message looks like the following:
Plain | Caesar Cipher |
A | X |
B | Y |
C | Z |
D | A |
E | B |
F | C |
G | D |
H | E |
I | F |
J | G |
K | H |
L | I |
M | J |
N | K |
O | L |
P | M |
Q | N |
R | O |
S | P |
T | Q |
U | R |
V | S |
W | T |
X | U |
Y | V |
Z | W |
Plaintext: hello world |
---|
Ciphertext: ebiilQtloia |
---|
The following program gives an encrypted text after taking a string and a shift value:
#include<iostream>using namespace std;string encrypt(string message,int key){string enc_string="\0";int i = 0;for(;i<message.length();i++){enc_string += char(int(message[i]+key-97)%26 +97);}return enc_string;}int main(){string message = "\0";string encrypyted_string="\0";int key = 23;message = "hello world";encrypyted_string = encrypt(message,key);cout<<"Encrypted String : "<< encrypyted_string;return 0;}
enc_string
.message
and encrypted_string
.key
to encrypt the string.message
.encrypt()
and pass parameters to encrypt the string.Free Resources