Site icon T4Tutorials.com

C++ program to encryption and decrypt using Caesar Cipher

Encryption using Caesar Cipher

#include<iostream>
using namespace std;
int main()
{
char text_for_Encryption[100], ch;
int loop, key;
cout << "Enter a text to encrypt: ";
cin.getline(text_for_Encryption, 100);
cout << "Enter key: ";
cin >> key;
for(loop = 0; text_for_Encryption[loop] != '\0'; ++loop){
ch = text_for_Encryption[loop];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
text_for_Encryption[loop] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
text_for_Encryption[loop] = ch;
}
}
cout << "Encrypted text is: " << text_for_Encryption;
return 0;
}

Output

Enter a text to encrypt: t4tutorials.com
Enter key: 4
Encrypted text is: x4xyxsvmepw.gsq
——————————–

Decryption using Caesar Cipher

#include<iostream>
using namespace std;
int main()
{
char text_for_encryption[100], ch;
int loop, key;
cout << "Enter a text_for to decrypt: ";
cin.getline(text_for_encryption, 100);
cout << "Enter key: ";
cin >> key;
for(loop = 0; text_for_encryption[loop] != '\0'; ++loop){
ch = text_for_encryption[loop];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
text_for_encryption[loop] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'a'){
ch = ch + 'Z' - 'A' + 1;
}
text_for_encryption[loop] = ch;
}
}
cout << "Decrypted  message is: " << text_for_encryption;
return 0;
}

Output

Enter a message to decrypt: x4xyxsvmepw.gsq
Enter key: 4
Decrypted message is: t4tutorials.com


Caesar Cipher for encryption in C

#include<stdio.h>
int main()
{
char text_for_encryption[100], ch;
int loop, key;
printf("Enter a text to encrypt: ");
gets(text_for_encryption);
printf("Enter key: ");
scanf("%d", &key);
for(loop = 0; text_for_encryption[loop] != '\0'; ++loop){
ch = text_for_encryption[loop];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
text_for_encryption[loop] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
text_for_encryption[loop] = ch;
}
}
printf("Encrypted text for encryption: %s", text_for_encryption);
return 0;
}

Output

Enter a text to encrypt: best tutorials site
Enter key: 5
Encrypted text for encryption: gjxy yzytwnfqx xnyj
——————————–

Caesar Cipher for Decryption in C

#include<stdio.h>
int main()
{
char text_for_encryption[100], ch;
int loop, key;
printf("Enter a text_for_encryption to decrypt: ");
gets(text_for_encryption);
printf("Enter key: ");
scanf("%d", &key);
for(loop = 0; text_for_encryption[loop] != '\0'; ++loop){
ch = text_for_encryption[loop];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
text_for_encryption[loop] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
text_for_encryption[loop] = ch;
}
}
printf("Decrypted text_for_encryption: %s", text_for_encryption);
return 0;
}

Output

Enter a text_for_encryption to decrypt: gjxy yzytwnfqx xnyj
Enter key: 5
Decrypted text_for_encryption: best tutorials site
——————————–

Exit mobile version