Caesar Cipher Encryption and Decryption Program in C
Here, you will know about Caesar Cipher Algorithm and get the example code to perform encryption and decryption using Caesar Cipher program in C.
What is Caesar Cipher
Caesar Cipher is a simple and old method used to encryption and decryption in computer network. The Shift Cypher is another name for it. The encryption process involves selecting a key, which is an integer value representing the number of positions each letter will be shifted.
For example:-
Caesar Cipher with a key of 3, the encryption would be as follows:
To decrypt the message, the receiver needs to know the key and then shift the letters back in the reverse direction.
The Caesar Cipher is a relatively weak encryption technique since there are only 26 possible keys. As a result, it can be easily broken using brute force or frequency analysis, where the attacker tries all possible keys or analyzes the frequency of letters in the ciphertext.
Example code of Caesar Cipher Program 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | //caesar cipher program in c with output #include<stdio.h> #include<conio.h> #include<string.h> int main() { int i, c; char str1[60]; printf("Enter the Text Message here : "); gets(str1); for (i = 0; i < strlen(str1); i++) { switch (str1[i]) { case 'x': str1[i] = 'a'; continue; case 'y': str1[i] = 'b'; continue; case 'z': str1[i] = 'c'; continue; case 'X': str1[i] = 'A'; continue; case 'Y': str1[i] = 'B'; continue; case 'Z': str1[i] = 'C'; continue; } if (str1[i] >= 'a' && str1[i] < 'x') str1[i] = str1[i] + 3; else if (str1[i] >= 'A' && str1[i] < 'X') str1[i] = str1[i] + 3; } printf("Message After Encryption : \n"); puts(str1); for (i = 0; i < strlen(str1); i++) { switch (str1[i]) { case 'a': str1[i] = 'x'; continue; case 'b': str1[i] = 'y'; continue; case 'c': str1[i] = 'z'; continue; case 'A': str1[i] = 'X'; continue; case 'B': str1[i] = 'Y'; continue; case 'C': str1[i] = 'Z'; continue; } if (str1[i] >= 'd' && str1[i] <= 'z') str1[i] = str1[i] - 3; else if (str1[i] >= 'D' && str1[i] < 'Z') str1[i] = str1[i] - 3; } printf("Message After Decryption : \n"); puts(str1); getch(); } |
Output:
Enter the Text Message here : coderevise.com
Message After Encryption : frghuhylvh.frp
Message After Decryption : coderevise.com