Encryption using Caesar Cipher
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#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
——————————–