Let us learn how to write a program to encrypt and decrypt using caesar cipher in C programming. Here, we shall see two different ways of implement caesar cipher algorithm in C programming language.
What is Caesar Cipher Algorithm?
The Caesar Cipher algorithm is one of the oldest methods of password encryption and decryption system. It is popular by the following naming conventions:
- Caesar shift
- Caesar’s cipher
- Shift cipher
- Caesar’s code
This caesarc cipher encryption algorithm is a kind of substitution cipher wherein every character in the plain-text or the user input is replaced by another character which is defined with a fixed number of positions away from the existing character.
Caesar Cipher Encryption and Decryption Example
Input: ABCDEFGHIJ
Encrypted String: KLMNOPQRST
As you can find out from the encrypted string, we have moved every character’s position by 10 towards the right. You can implement your own complex calculations as well.
However, this method cannot be implemented in real time systems for encrypting and decrypting strings as these are very easy to decode. In this method, every string character is replaced by a fixed value.
Apart from caesar cipher encryption and decryption algorithm, there are many different algorithms used for encrypting and decrypting passwords or strings. Some of them are:
- Triple DES
- BlowFish
- Simple DES
- RSA
- TwoFish
- Advanced Encryption Standard (AES)
Here, we have taken an array of characters in the encrypt and decrypt functions. We have incremented and decremented the string characters by 10 in decrypt and encrypt functions respectively.
The strlen() method is used to find the length of the string and it is defined in the string.h header file. The stdlib.h header files include the definitions for exit() method.
C Program To Implement Caesar Cipher Algorithm
Note: This implementation of caesar cipher in C programming language is compiled with GNU GCC compiler on Linux Ubuntu 14.04 operating system.
Caesar Cipher in C Language [Encryption]
1 2 3 4 5 6 7 8 | void encrypt(char arr[]) { int i; for(i = 0; i < strlen(arr); i++) { arr[i] = arr[i] - 10; } } |
Caesar Cipher in C Language [Decryption]
1 2 3 4 5 6 7 8 | void decrypt(char arr[]) { int i; for(i = 0; i < strlen(arr); i++) { arr[i] = arr[i] + 10; } } |
Method 1: Caesar Cipher Encryption and Decryption Program in C with Output
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 | #include<stdio.h> #include<stdlib.h> #include<string.h> void decrypt(char arr[]) { int i; for(i = 0; i < strlen(arr); i++) { arr[i] = arr[i] + 10; } } void encrypt(char arr[]) { int i; for(i = 0; i < strlen(arr); i++) { arr[i] = arr[i] - 10; } } int main() { char password[40]; int ch; printf("Enter a Password:\t"); scanf("%s", password); printf("\nPassword:\t%s\n",password); encrypt(password); printf("\nEncrypted Password:\t%s\n", password); decrypt(password); printf("\nDecrypted Password:\t%s\n", password); return 0; } |
Output

Here is another code to perform Encryption and Decryption using Caesar Cipher in C programming
It makes use of a key which is taken from the user and the generated encrypted string is manipulated accordingly. The temp variable takes in the character from the string.
Method 2: C Program For Encryption and Decryption using Caesar Cipher Algorithm
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include<stdio.h> #include<stdlib.h> char data[50], temp; int key, count; void getmessage() { printf("Enter a String:\t"); scanf("%[^\n]s", data); } void key_input() { printf("Enter a Key:\t"); scanf("%d", &key); } void caesar_cipher_encryption() { for(count = 0; data[count] != '\0'; count++) { temp = data[count]; if(temp >= 'a' && temp <= 'z') { temp = temp + key; if(temp > 'z') { temp = temp - 'z' + 'a' - 1; } data[count] = temp; } else if(temp >= 'A' && temp <= 'Z') { temp = temp + key; if(temp > 'Z') { temp = temp - 'Z' + 'A' - 1; } data[count] = temp; } } printf("\nEncrypted Message:\t%s\n", data); } void caesar_cipher_decryption() { for(count = 0; data[count] != '\0'; count++) { temp = data[count]; if(temp >= 'a' && temp <= 'z') { temp = temp - key; if(temp < 'a') { temp = temp + 'z' - 'a' + 1; } data[count] = temp; } else if(temp >= 'A' && temp <= 'Z') { temp = temp - key; if(temp < 'A') { temp = temp + 'Z' - 'A' + 1; } data[count] = temp; } } printf("\nDecrypted Message:\t%s\n", data); } int main() { int choice; getmessage(); key_input(); while(1) { printf("\n1. Encryption\n2. Decryption\n3. Exit\n"); printf("\nEnter You Choice:\t"); scanf("%d", &choice); switch(choice) { case 1: caesar_cipher_encryption(); break; case 2: caesar_cipher_decryption(); break; case 3: exit(0); default: printf("\nPlease select a correct option:\n"); } } printf("\n"); return 0; } |
Output

If you have any doubts or compilation errors in this C program to perform encryption and decryption using caesar cipher algorithm, let us know about it in the comment section below. Find more about it on Wikipedia.
The Caesar Cipher technique is too simple I guess and therefore, it becomes easier to program as well. Thanks a lot.
can you pls tell me that why did check for temp>z ?to encrypt in only alphabets?if so can u explain the logic..?
There can be two different types of Encryption methods such as Asymmetric Encryption and Symmetric encryption.
So primarily, if the data is encrypted and decrypted using the same key, it is called as Cipher encryption.
This Caesar Cipher in C Program is too good! THanks a lot!