×

Caesar Cipher Program in C

What is Caesar Cipher?

The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to his officials. The Caesar Cipher works by replacing each letter in a plaintext message with a different letter or symbol, based on a fixed number of positions (shift value) down the alphabet.

For example, if the shift value is 3, the letter "A" would be replaced with "D", "B" would be replaced with "E", and so on. The result is a ciphertext message that can only be deciphered by someone who knows the shift value.

The technique is easy to break but can be useful for very basic data encryption and for educational purposes. It was used to encrypt military and personal communications in ancient Rome. Nowadays, it is used for educational and recreational purposes, such as puzzle books and online games.

The Caesar Cipher is considered to be a very weak encryption method because it only has a small number of possible keys (typically 26, one for each letter of the alphabet), and can be easily broken by trying all possible keys. It is also vulnerable to frequency analysis which is a method of identifying the letters in a ciphertext by analyzing the frequency of the letters.

The Caesar Cipher is still widely used in educational and research contexts, but it is not recommended to use in any serious cryptographic application due to its lack of security. More secure algorithms such as RSA and AES are commonly used instead.

Here is an example of a C program that implements the Caesar Cipher.

Example:

#include <stdio.h>
#include <string.h>


void encrypt(char* text, int shift) {
    int length = strlen(text);
    for(int i = 0; i < length; i++) {
        char c = text[i];
        if(c >= 'a' && c <= 'z') {
            c = c + shift;
            if(c > 'z') {
                c = c - 'z' + 'a' - 1;
            }
            text[i] = c;
        } else if(c >= 'A' && c <= 'Z') {
            c = c + shift;
            if(c > 'Z') {
                c = c - 'Z' + 'A' - 1;
            }
            text[i] = c;
        }
    }
}


int main() {
    char text[100];
    int shift;
    printf("Enter a message to encrypt: ");
    scanf("%s", text);
    printf("Enter shift value: ");
    scanf("%d", &shift);
    encrypt(text, shift);
    printf("Encrypted message: %s\n", text);
    return 0;
}

Output:

Enter a message to encrypt: Javatpoint
Enter shift value: 2
Encrypted message: Lcxcvrqkpv

This program takes a string of text and an integer shift value as input and encrypts the text by shifting each letter by the shift value. The shift value can be positive or negative. Here, in the above example, we have used a shift value 2 which has shifted the encrypted code by 2 positions further as compared to original message.

Note: This program only considers lowercase and uppercase alphabets and doesn't handle any special characters or whitespaces.


Related Topics

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.