Here you will get the program for caesar cipher in C and C++ for encryption and decryption. I will also list some of its advantages and disadvantages.
What is Caesar Cipher?
It is one of the simplest encryption techniques in which each character in plain text is replaced by a character with some fixed number of positions down to it.
For example, if key is 3 then we have to replace the character by another character that is 3 positions down to it. Like A will be replaced by D, C will be replaced by F, and so on.
For decryption just follow the reverse of the encryption process.
Below I have shared a program to implement caesar cipher in C and C++.
Also Read: Vigenere Cipher in C and C++
Program for Caesar Cipher in C
Encryption
#include<stdio.h> int main() { char message[100], ch; int i, key; printf("Enter a message to encrypt: "); gets(message); printf("Enter key: "); scanf("%d", &key); for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch + key; if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch + key; if(ch > 'Z'){ ch = ch - 'Z' + 'A' - 1; } message[i] = ch; } } printf("Encrypted message: %s", message); return 0; }
Output
Enter a message to encrypt: axzd
Enter key: 4
Encrypted message: ebdh
Decryption
#include<stdio.h> int main() { char message[100], ch; int i, key; printf("Enter a message to decrypt: "); gets(message); printf("Enter key: "); scanf("%d", &key); for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch - key; if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch - key; if(ch < 'A'){ ch = ch + 'Z' - 'A' + 1; } message[i] = ch; } } printf("Decrypted message: %s", message); return 0; }
Output
Enter a message to decrypt: ebdh
Enter key: 4
Decrypted message: axzd
Program for Caesar Cipher in C++
Encryption
#include<iostream> using namespace std; int main() { char message[100], ch; int i, key; cout << "Enter a message to encrypt: "; cin.getline(message, 100); cout << "Enter key: "; cin >> key; for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch + key; if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch + key; if(ch > 'Z'){ ch = ch - 'Z' + 'A' - 1; } message[i] = ch; } } cout << "Encrypted message: " << message; return 0; }
Output
Enter a message to encrypt: asd zf
Enter key: 3
Encrypted message: dvg ci
Decryption
#include<iostream> using namespace std; int main() { char message[100], ch; int i, key; cout << "Enter a message to decrypt: "; cin.getline(message, 100); cout << "Enter key: "; cin >> key; for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch - key; if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch - key; if(ch > 'a'){ ch = ch + 'Z' - 'A' + 1; } message[i] = ch; } } cout << "Decrypted message: " << message; return 0; }
Output
Enter a message to decrypt: az GjK
Enter key: 2
Decrypted message: yx EhI
Caesar Cipher Advantages
- The simplest and easiest technique requires very less computational resources.
- It can be used to encrypt short messages.
Caesar Cipher Disadvantages
- Provides less security and can be easily cracked. Vulnerable to various attacks such as brute force and frequency analysis attacks.
- It is not good for encrypting long messages.
Below I have given a video that can help you to understand the concept of caesar cipher.
Comment below if you have doubts or found anything incorrect in the above program for caesar cipher in C and C++.
Thank u very much..
Thanks man ,you’re awesome,looking forward for more encryption stuff.
What are ‘z’ and ‘a’ for exactly? Care to explain a bit on the conditional statements there?
We are checking that given character in message should be in between ‘a’ and ‘z’ or ‘A’ and ‘Z’.
#include
#include
#include
void main()
{
int k,j,i,h,u=0;
char plain[30],p[30],c[30],alpha[30]=”abcdefghijklmnopqrstuvwxyz”;
clrscr();
printf(“**********Encryption**********”);
printf(“\nEnter the plain text = “);
gets(plain);
printf(“Enter the key = “);
scanf(“%d”,&k);
for(i=0;i<strlen(plain);i++)
{
if(plain[i]==' ') {
i++;u++; }
for(j=0;j<strlen(alpha);j++)
{
if(alpha[j]==plain[i])
{
h=(j+k)%26;
c[i-u]=alpha[h];
break;
}
}
}
c[i-u]='\0';
printf("\nCipher Text = ");
puts(c);
printf("**********Decryption**********");
for(i=0;i<strlen(c);i++)
{
for(j=0;j<strlen(alpha);j++)
{
if(alpha[j]==c[i])
{
h=(j-k)%26;
p[i]=alpha[h];
break;
}
}
}
p[i]='\0';
printf("\nPlain Text = ");
puts(p);
getch();
}
/*
**********Encryption**********
Enter the plain text = hello hii
Enter the key = 3
Cipher Text = khoorkll
**********Decryption**********
Plain Text = hellohii
*/
works well………..
Can you help me to write encryption
Sir what is the meaning of this condition i don’t undetstand
if(ch > ‘z’){
ch = ch – ‘z’ + ‘a’ – 1;
}
It is to subtract the ASCII value of the character in the message[i] (i is index) and to shift it, in order to keep message in alphabets only and so that it doesn’t go in the special characters. In other words take this example :
if your key is 2 and your word is zebra the *first letter* shifts by the ASCII value of z(ASCII value-122) to 124 and then when the outcome comes it will print something like this |gdtc and the vertical bar is an absurd in this case, so by placing the character like this ‘z’ (which is actually 122 in computer terms) is, admin writes this algorithm (very clever) [ch-‘z’+’a’ -1 ] in terms of computer what it does is [124-122+97-1=98] and 98 is the ASCII value of b and hence the shift still remains in the character format….. I hope this makes it clear
PS: the whole point of doing this was to keep things simple the other way it could have been written:
ch-26… i think correct me if I am wrong people…. 😛
thank you so much
cause your c=a chutya bruh thats why you wont get thet shit
Is there anyone in whatsapp group for programming plz add me 9788342473
Thank you friends, sisters and brothers
I dont have any whatsapp group. You can join our facebook group here https://goo.gl/6Jw8fJ
I get an error with the gets(message); below the enter the encryption message, why could that be?
What error you are getting?
can i do the same code but picking thr content from a file?
Hi,
considering i dont know the key to decrypt and if i run the c ++ decryption code under a ‘for’ loop for 25 times the code doesn’t work.How do i do this ?
if(ch < 'a'){
ch = ch + 'z' – 'a' + 1;
I cannot understand ths.. plz explain ths with an example
what is the encrytion techniqy
Bro actualy i didnt get the final output after running the program…can please guide me
bro can u please explain me this peice of code
if(ch > ‘z’)
{
ch = ch – ‘z’ + ‘a’ – 1;
what is the purpose of having they key for the encryption and decryption?
The purpose for having a key is to determine the number of letters to move over to encrypt
why if I entered a 2 or 3 digit key the encryption has a letter with a symbol on it? like Å¡?
How can I change the key from 4 to 6 ?
Hey, does anyone know how to put the script she used into python, I am having trouble with it.
If the key value is more than 5 the out put is wrong for z and of the key is more than 6 the out put for y is also wrong.can any one explain?
Hej!
It is bug in the program. Simple char can holp up to 127 values. z is equal to 122 and if you want to shift z up to 6 spaces so its become 128. Just change char declaration to unsinged char ch;
It would work.
This doesn’t work for capital letters for some reason, do you know why?
And also how to implement numbers to work more than 10 shifts.
Would it work if I choose key 6 or above and i have character z in my string? I don’t think so.
char has range 0-127, it would be better if you declare it like unsigned char ch;
i im new programmer i dont know very much about c but i thought a way for that but it doesn’t work clearly if you have time could y tell me why not working?
#include
int main(){
char mystring(int k){
char a[52];
int m;
m=0;
a[m]=”a”;
a[m++]=”b”;
a[m++]=”c”;
a[m++]=”d”;
a[m++]=”e”;
a[m++]=”f”;
a[m++]=”g”;
a[m++]=”h”;
a[m++]=”i”;
a[m++]=”j”;
a[m++]=”k”;
a[m++]=”l”;
a[m++]=”m”;
a[m++]=”n”;
a[m++]=”o”;
a[m++]=”p”;
a[m++]=”q”;
a[m++]=”r”;
a[m++]=”s”;
a[m++]=”t”;
a[m++]=”u”;
a[m++]=”v”;
a[m++]=”w”;
a[m++]=”x”;
a[m++]=”y”;
a[m++]=”z”;
int f;
f=26;
int l;
l=0;
a[f++]=toupper(a[l]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
a[f++]=toupper(a[l++]);
int d;
d=0;
while(d<=52){
if(a[d]=k){ k=1;
}
d++;
}
return k;
}
char source[50],target[50];
printf("give letter/if you want to stop give 2:");
char on[50];
int p;
p=0;
while((on[p]=getchar())!="2"){
p++;}
int h;
h=0;
int metr;
while(h<=p){metr=0; if(mystring(on[h])!=1){target[h]="á"; source[h]=on[h]; h++;
} else if(on[h]="x"){ source[h]="x"; target[h]="a"; h++; metr++;}else
if(on[h]="X"){ source[h]="X"; target[h]="A"; h++; metr++;}else
if(on[h]="y"){ source[h]="y"; target[h]="b"; h++; metr++;}else
if(on[h]="Y"){ source[h]="Y"; target[h]="B"; h++; metr++;}else
if(on[h]="z"){ source[h]="z"; target[h]="c"; h++; metr++;}else
if(on[h]="Z"){ source[h]="Z"; target[h]="C"; h++; metr++;}else
if(metr=0){ source[h]=on[h]; target[h]=on[h]+3; h++;}
}
int sp;
for(sp=0;sp<p;sp++){ if(sp=0){printf("word:");} printf("%c",source[sp]);
}
for(sp=0;sp<p;sp++){ if(sp=0){printf("Caesar's hidden word:");} printf("%c",target[sp]);
}
return 0;
}
i want to implemente this code into java can you help me??
You can find the code for java here: https://www.thejavaprogrammer.com/caesar-cipher-java-encryption-decryption/
sir,please explain the meaning of ch=ch-‘z’+’a’-1
I have a problem that requires use of Caesar cipher to encrypt and decrypt message in C++, but with a keyword for example “bat”. Also, i need to avoid duplication of letters and use upper case. Can you do this problem with the use of a keyword!. Help appreciated.
Have error…
If you encrypt capital letter.. Decryption is wrong!
bro your code is not efficient.
check this
Ciphering
26
Explain me some one how this will work
if(Ch>’z’)
{
Ch=ch-‘z’+’a’-1;
}
can you explain this program
what do you mean by this code?
if(ch > ‘z’){
ch = ch – ‘z’ + ‘a’ – 1;
Great work! Very simple and effective code. Thanks for sharing, saved me some time. 🙂
What if the key is a negative number? For example, if the message is ‘bat’ and key is -2, then this doesn’t convert the code into just the alphabet. It will include special characters.
How can it be coded to keep within the alphabet in this direction?
there’s a mistake in the decryption part of c++ program ….
it should be like
for(i = 0; message[i] != ‘\0’; ++i){
ch = message[i];
if(ch >= ‘a’ && ch <= 'z'){
ch = ch – key;
if(ch = ‘A’ && ch <= 'Z'){
ch = ch – key;
if(ch < 'A'){
ch = ch + 'Z' – 'A' + 1;
}
message[i] = ch;
}
}
hi neeraj bro,
pls guide me, how to get ciphertext by shifting key in below code
#include
#include
#include
#include
// getting command-line arguments
int main(int argc, string argv[])
{
int i;
char plaintext[100], c;
if (argc == 2)
{
//converting command-line argument from string to int
i = atoi(argv[1]);
}
else
{
printf(“Usage: ./caesar key\n”);
}
{
//prompting user for plaintext
string s = get_string(“Plaintext:”);
for (i = 0; i < strlen(s) ; i++)
{
c = (c+i)%26; (<———pls guide me, how to get ciphertext by shifting key in this line)
s[i] = c;
// printing out ciphertext
printf("ciphertext: %i", c);
}
printf("\n");
}
}
i am getting output as ciphertet:0
//Simple C++ program to encrypt and decrypt a string
Bro why it give me wrong result of encryption and decryption, can you find the mistake.
#include
using namespace std;
int main()
{
int i, x;
char str[100];
cout <> str;
cout << "\nPlease choose following options:\n";
cout << "1 = Encrypt the string.\n";
cout <> x;
//using switch case statements
switch (x)
{
//first case for encrypting a string
case 1:
for (i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = (str[i] + 2)%26; //the key for encryption is 3 that is added to ASCII value
cout << "\nEncrypted string: " << str << endl;
break;
//second case for decrypting a string
case 2:
for (i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = (str[i] – 2)%26; //the key for encryption is 3 that is subtracted to ASCII value
cout << "\nDecrypted string: " << str << endl;
break;
default:
cout << "\nInvalid Input !!!\n";
}
system("pause");
return 0;
}
The C code doesn’t work with negative shifts or with large shifts.. for example:
the following text (minus the quotes)
“this is a test”
shifted 263
Should read “wklv lv d whvw”
But it does not.. why?
donot use space for long string use underscore
How create a C++ code of cipher?
example i have: aa=dr; ab=fh; ac=er;… and all any combinations with two letters (676)
and i write input e.g.: aaacab
and i get output: drerfh
Hi Neeraj Mishra!
Thanks, thanks, thanks. I love you 😀 😀
[]’s
It won’t let me input any characters.
OMG, guys. You are plain IDIOTS. Your code does not work proper way. CHECK THIS OUT
#include
#include
using namespace std;
int main()
{
int step, len;
char word[100];
cin >> word >> step;
len = strlen(word);
char *pword = word;
for (int i = 0; i<len; i++){
word[i] = (*(pword+i)+(26-step)-65)%26 + 65;
}
cout << word;
}
Hello how can I use this program in one encryption and decryption interface. In that one
it is so because computer reads the ascii code of character and suppose if we enter z in message which we want to encrypt then ch = z= 122(ascii code of ‘z’) + 3
= 125 (which is ascii code of right curly brace )
therefore in order to remove this we continue the series with continuation from ‘a’ after ‘z’ therefore
we take 123 as a, 124 as b, 125 as c and so on by applying the formulae
ch = 125 – 122 +97(ascii code of ‘a’) -1
= 99 (ascii code of ‘c’)