×

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

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.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 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.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 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.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.