caesar cipher in c with code examples

Caesar Cipher, also known as shift cipher, is one of the simplest and most widely known encryption techniques used worldwide for centuries. In this technique, each letter of the message is shifted by a fixed number of positions down the alphabet, and the receiver can decipher the message by reversing the shift by the same number of positions. For instance, if we shift every letter of the message by three positions down the alphabet, the letter 'A' would become 'D,' 'B' would become 'E,' and so on.

The Caesar Cipher is named after Julius Caesar, who allegedly used it to communicate messages with his generals that nobody could read, except for the intended party. It is also believed that this cipher was used by the Jewish historian Josephus in the first century AD to encrypt his correspondence. Today, the Caesar Cipher is still used in computer security to encrypt data.

Implementation of the Caesar Cipher in C:

The Caesar Cipher can be easily implemented in C language. The implementation is straightforward, and the following is the algorithm:

  1. Read the plaintext message to be encrypted.
  2. Read the shift key or the number of positions each letter of the message is to be shifted.
  3. Encrypt the message by shifting each letter in the plaintext by the key value.
  4. Print the ciphertext or the encrypted message.

Here’s the code example that implements the Caesar Cipher in C:


int main()
char plaintext[100], ciphertext[100];
int i, key;

printf("Enter the plaintext to be encrypted: ");
printf("Enter the shift key: ");

//loop through each letter of the plaintext and shift it by the key value
    if(plaintext[i]>='a' && plaintext[i]<='z') //for lower case letters
         ciphertext[i]= ((plaintext[i]-'a')+key)%26+'a';
    else if(plaintext[i]>='A' && plaintext[i]<='Z') //for upper case letters
         ciphertext[i]= ((plaintext[i]-'A')+key)%26+'A';
    else //for special characters
         ciphertext[i] = plaintext[i];

printf("The encrypted message is: %s", ciphertext);

return 0;


Explanation of the Code:

The code starts with including the standard I/O header file (stdio.h), standard library header file (stdlib.h) and the string header file (string.h).

In the main function, two character arrays ‘plaintext’ and ‘ciphertext’ of length 100 are declared to store the plaintext and ciphertext, respectively. The integer i and ‘key’ variables are declared to iterate through the plaintext string and store the number of positions to shift the characters.

The user is prompted to enter the plaintext to be encrypted using the fgets() function which reads input from the standard input stream (stdin).

The user is then asked to enter the shift key or the number of positions each character of the plaintext is to be shifted, using the scanf() function.

The for loop is used to iterate through each character of the ‘plaintext’ array. We use the if statement to process each letter, which checks if the letter is upper case, lower case, or a special character.

If the letter is an upper case or lower case, we shift the letter by the ‘key’ value using the modulo operator. The modulo operator is used to wrap the range of the characters within the alphabets, making sure we do not go out of range. If the plaintext[i] does not fall between ‘a’ or ‘z’ and ‘A’ or ‘Z’, the else statement executes and the character remains unchanged.

Finally, the result or the encrypted message is printed using the printf() function.


Now that you know how to implement the Caesar Cipher in C language, you can create more complex implementations like Vigenère cipher, Playfair cipher, or even implement the Caesar Cipher in other programming languages. Note that the Caesar Cipher is not secure and can be easily broken using brute-force attacks. However, this cipher is still useful in applications that require basic encryption, for example, encrypted URL parameters, encrypted email subject lines, and more.

Caesar Cipher is a type of substitution cipher that involves shifting every letter of the plaintext message by a fixed number of positions down the alphabet. While it is a simple encryption technique, it is no longer considered very secure as it can be quickly deciphered using brute-force methods. In order to make it more secure, a common approach is to have the shift key change for each letter. This evolution of Caesar Cipher is referred to as the Vigenère Cipher.

The Vigenère Cipher is a polyalphabetic substitution cipher that uses a keyword to generate multiple alphabets to encrypt the message. Unlike the Caesar Cipher, the keyword is used to shift letters by different amounts based on the position of the letter in the message. Due to its versatility, the Vigenère Cipher was more difficult to crack, and it remained unbreakable for over 250 years. However, with the development of computers and improved cryptanalysis techniques, it can now be deciphered much more quickly.

Another example of a substitution cipher is the Playfair Cipher, which is a polygraphic substitution technique developed in the 1850s by Sir Charles Wheatstone. This cipher encrypts pairs of letters, rather than individual letters, making it harder to break. In this cipher, a keyword is used to generate a 5×5 matrix, which contains all of the letters of the alphabet except for one chosen letter. The plaintext message is then divided into pairs of letters, and each pair is encrypted using the Playfair matrix.

The Hill Cipher is a type of substitution cipher that relies on linear algebra. It was developed in the 1920s by Lester Hill and is sometimes referred to as the matrix cipher. In this cipher, every letter is represented by a number that corresponds to its position in the alphabet. The message is then divided into blocks of a fixed length, and each block is multiplied by a matrix to generate the ciphertext block. The matrix used to encrypt the message must be invertible, ensuring that the message can be decrypted.

In conclusion, there are several different types of encryption techniques, including substitution ciphers like Caesar Cipher, Vigenère Cipher, Playfair Cipher, and the Hill Cipher. While these techniques are no longer considered secure, they provide a foundation for modern encryption methods that have proven to be much stronger and more difficult to crack. Understanding how these ciphers work is a useful exercise, and it can help individuals better understand the principles of encryption and security.

Popular questions

  1. What is Caesar Cipher?
    Answer: Caesar Cipher is a simple encryption technique that involves shifting every letter of the plaintext message by a fixed number of positions down the alphabet.

  2. What is the purpose of implementing Caesar Cipher in C language?
    Answer: The purpose of implementing Caesar Cipher in C language is to encrypt plaintext messages for basic encryption needs.

  3. How does the code example for Caesar Cipher in C language work?
    Answer: The code example for Caesar Cipher in C language works by taking in a plaintext message and a key value from the user, then shifting each letter of the plaintext by the key value using a for loop and if statement depending on the input character's type. Finally, the result is printed as ciphertext.

  4. Why is the Caesar Cipher no longer considered secure?
    Answer: The Caesar Cipher is no longer considered secure because it can be easily deciphered using brute-force methods, especially as the key length is short and fixed.

  5. Can you use the code example to decrypt a message that is encoded using the same key value?
    Answer: Yes, the code example can be used to decrypt a message that is encoded using the same key value by simply reversing the shift calculation to shift each letter of the ciphertext back to its original position. In other words, we can decrypt a message by using the negative key value instead of the positive one.



As an experienced software engineer, I have a strong background in the financial services industry. Throughout my career, I have honed my skills in a variety of areas, including public speaking, HTML, JavaScript, leadership, and React.js. My passion for software engineering stems from a desire to create innovative solutions that make a positive impact on the world. I hold a Bachelor of Technology in IT from Sri Ramakrishna Engineering College, which has provided me with a solid foundation in software engineering principles and practices. I am constantly seeking to expand my knowledge and stay up-to-date with the latest technologies in the field. In addition to my technical skills, I am a skilled public speaker and have a talent for presenting complex ideas in a clear and engaging manner. I believe that effective communication is essential to successful software engineering, and I strive to maintain open lines of communication with my team and clients.
Posts created 3227

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top