0% found this document useful (0 votes)
12 views3 pages

CN-Lab Exp-7

The document provides a C program that implements data encryption and decryption using the Caesar Cipher technique. It explains the encryption and decryption formulas, along with the functions to perform these operations on a given plaintext. The program allows users to input a plaintext message and a shift value, then outputs the encrypted and decrypted texts accordingly.

Uploaded by

harshsing0009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

CN-Lab Exp-7

The document provides a C program that implements data encryption and decryption using the Caesar Cipher technique. It explains the encryption and decryption formulas, along with the functions to perform these operations on a given plaintext. The program allows users to input a plaintext message and a shift value, then outputs the encrypted and decrypted texts accordingly.

Uploaded by

harshsing0009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

7. Implement data encryption and data decryption using c program.

Ans: Below is an example of a simple C program that implements data


encryption and decryption using the Caesar Cipher, a basic encryption
technique.
The Caesar Cipher shifts each letter in the plaintext by a fixed number of
positions down the alphabet.
Plaintext usually refers to raw data, simple text,unencrypted, or the original
message in cryptography.
Cipher usually refers to the data which is encrypted, encoded, or the secret
text which hides the actual meaning of the text.
The main goal of any cryptography method is to convert plaintext into cipher
text. In Caesar cipher, we have the Encryption Formula and Decryption
formula.
Encryption Formula
The formula of Caesar cipher encryption is En(x) = (x + n) mod 26.
Here,
En(x) stands for Encryption of x,
x is the letter that is going to be encrypted,
and n is the number that is going to be added.
mod 26 is the modulus.

Decryption Formula
The Formula for Caesar cipher decryption is Dn(x) = (x – n) mod 26.
here,
Dn(x) represents the Decryption of x,
x is the letter that is going to be decrypted,
and n is the number that is going to be reduced.

C Program for Caesar Cipher Encryption and Decryption


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

// Function to encrypt the plaintext using Caesar Cipher


void encrypt(char *plaintext, int shift, char *ciphertext) {
int i;
for (i = 0; plaintext[i] != '\0'; i++) {
char ch = plaintext[i];
if (ch >= 'a' && ch <= 'z') {
ciphertext[i] = (ch - 'a' + shift) % 26 + 'a';
} else if (ch >= 'A' && ch <= 'Z') {
ciphertext[i] = (ch - 'A' + shift) % 26 + 'A';
} else {
ciphertext[i] = ch;
}
}
ciphertext[i] = '\0';
}

// Function to decrypt the ciphertext using Caesar Cipher


void decrypt(char *ciphertext, int shift, char *plaintext) {
int i;
for (i = 0; ciphertext[i] != '\0'; i++) {
char ch = ciphertext[i];
if (ch >= 'a' && ch <= 'z') {
plaintext[i] = (ch - 'a' - shift + 26) % 26 + 'a';
} else if (ch >= 'A' && ch <= 'Z') {
plaintext[i] = (ch - 'A' - shift + 26) % 26 + 'A';
} else {
plaintext[i] = ch;
}
}
plaintext[i] = '\0';
}

int main() {
char plaintext[100], ciphertext[100], decryptedtext[100];
int shift;

printf("Enter the plaintext: ");


gets(plaintext);
printf("Enter the shift value: ");
scanf("%d", &shift);
encrypt(plaintext, shift, ciphertext);
printf("Encrypted text: %s\n", ciphertext);

decrypt(ciphertext, shift, decryptedtext);


printf("Decrypted text: %s\n", decryptedtext);

return 0;
}
Explanation
 encrypt Function:
o Encrypts the input plaintext by shifting each letter by the specified
shift value.
o The % 26 operation ensures the shifting wraps around the
alphabet.
o Non-alphabet characters are not altered.
 decrypt Function:
o Decrypts the input ciphertext by reversing the shift operation.
o The + 26 ensures the result is always positive before taking the %
26 operation.
Usage
1. Input:
o The user enters the plaintext message they wish to encrypt.
o The user specifies the shift value for the Caesar Cipher.
2. Encryption:
o The program encrypts the plaintext using the shift value and prints
the ciphertext.
3. Decryption:
o The program decrypts the ciphertext using the same shift value
and prints the decrypted text.
Example
Input:
Enter the plaintext: HELLO
Enter the shift value: 3
Output:
Encrypted text: KHOOR
Decrypted text: HELLO

You might also like