0% found this document useful (0 votes)
4 views

Caesar Cipher in C

The document provides a detailed explanation of the Caesar Cipher program in C for both encryption and decryption, including code examples and outputs. It describes how to handle alphanumeric characters and the process of encoding and decoding messages using a key. Additionally, it outlines the advantages and disadvantages of the Caesar Cipher, highlighting its simplicity and resource efficiency, as well as its vulnerabilities due to being an older algorithm.

Uploaded by

ahmedmoradg2003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Caesar Cipher in C

The document provides a detailed explanation of the Caesar Cipher program in C for both encryption and decryption, including code examples and outputs. It describes how to handle alphanumeric characters and the process of encoding and decoding messages using a key. Additionally, it outlines the advantages and disadvantages of the Caesar Cipher, highlighting its simplicity and resource efficiency, as well as its vulnerabilities due to being an older algorithm.

Uploaded by

ahmedmoradg2003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

https://www.scaler.

com/topics/caesar-cipher-program-in-
c/

Program for Caesar Cipher in C


Caesar Cipher Encryption (Explain, Syntax & Output)

The Caesar Cipher Program in C can be applied as given below. We will check each
character one by one to see if it is a lowercase character, an uppercase character, or a digit.
We have an above-derived formula for each of them which we can use to encrypt as per the
key given by the user.

Apply the above-derived formulae for each category. If the given character is an
alphanumeric value, only we try encoding it. Else we print an error message.

#include<stdio.h>

#include<ctype.h>

int main() {

char text[500], ch;

int key;

// Taking user input.


printf("Enter a message to encrypt: ");

scanf("%s", text);

printf("Enter the key: ");

scanf("%d", & key);

// Visiting character by character.

for (int i = 0; text[i] != '\0'; ++i) {

ch = text[i];
// Check for valid characters.
if (isalnum(ch)) {

//Lowercase characters.
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}

// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
// Invalid character.
else {
printf("Invalid Message");
}

// Adding encoded answer.


text[i] = ch;

printf("Encrypted message: %s", text);

return 0;
}

Output:

Enter a message to encrypt: yZq8NS92mdR


Enter the key: 6
Encrypted message: eFw4TY58sjX

We first check if the character is alphanumeric using the isalnum() method, which returns
true if the given character is an alphanumeric value, i.e., either lowercase or uppercase
alphabets or numbers. Then we check if the character is lowercase, uppercase, or a number
using islower(), isupper(), and isdigit(), respectively. Accordingly, conversion is processed.
Lastly, the encrypted character is added to the text at its position.

Caesar Cipher Decryption (Explain, Syntax & Output)

Similarly, we can write Caesar Cipher Program in C for decoding. Instead of adding the key
subtract it and add 26 so that we don't get a negative value as our answer.

#include<stdio.h>

#include<ctype.h>

int main() {

char text[500], ch;

int key;

// Taking user input.

printf("Enter a message to decrypt: ");

scanf("%s", text);

printf("Enter the key: ");

scanf("%d", & key);

// Visiting each character.


for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
// Check for valid characters.
if (isalnum(ch)) {
//Lowercase characters.
if (islower(ch)) {
ch = (ch - 'a' - key + 26) % 26 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';
}
// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' - key + 10) % 10 + '0';
}
}
// Invalid characters.
else {
printf("Invalid Message");
}
// Adding decoded character back.
text[i] = ch;

printf("Decrypted message: %s", text);

return 0;

Output:

Enter a message to decrypt: eFw4TY58sjX


Enter the key: 6
Decrypted message: yZq8NS92mdR

The example above returns the value we inserted to encode in the above encoding C code in
the previous section.

Advantages of Caesar Cipher


Caesar Cipher Program in C has various advantages due to its simplicity as follows:

 It is a simple algorithm and very easy to implement.


 Only a small amount of computing resources are required.
 Single key is used for the entire message, which makes it less complex.
 It is very much suitable for non-complex systems.

Disadvantages of Caesar Cipher


Caesar Cipher Program in C also has a few disadvantages as it is simple and old such as:

You might also like