-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenc and decry using caser ciper without key.c
59 lines (56 loc) · 2.05 KB
/
enc and decry using caser ciper without key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Progam for encryption and decryption using Caesar cipher technique without key //
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char plain[50]; // Plain text
char cipher[50]; // Cipher text
char c;
int key = 3,i,j; // Caesar cipher key is set to 3
clrscr(); // Clear screen (platform-specific, not portable)
// Input plain text
printf("Enter Plain Text =");
gets(plain); // Gets input from the user
// Encryption Logic
for(i=0; i<strlen(plain); i++); // Loop through each character in the plain text
{
if(plain[i]>='A' && plain[i]<='Z') // Check if the character is an uppercase letter
{
cipher[i]=((plain[i]-65+key) % 26)+65; // Apply Caesar cipher encryption for uppercase letters
printf("%c",cipher[i]); // Print the encrypted character
}
else if(plain[i]>='a' && plain[i]<='z') // Check if the character is a lowercase letter
{
cipher[i]=((plain[i]-97+key) % 26)+97; // Apply Caesar cipher encryption for lowercase letters
printf("%c",cipher[i]); // Print the encrypted character
}
else // If the character is not a letter
{
cipher[i] = plain[i]; // Copy the character as is
printf("%c",plain[i]); // Print the character
}
}
printf("\n\n");
// Decryption Logic
printf("Decryption is =");
for(i=0;i<strlen(plain);i++) // Loop through each character in the plain text
{
if(cipher[i]>='A' && cipher[i]<='Z') // Check if the character is an uppercase letter
{
plain[i]=((cipher[i]-65-key) % 26)+65; // Apply Caesar cipher decryption for uppercase letters
printf("%c",plain[i]); // Print the decrypted character
}
else if(plain[i]>='a' && plain[i]<='z') // Check if the character is a lowercase letter
{
plain[i]=((cipher[i]-97-key) % 26)+97; // Apply Caesar cipher decryption for lowercase letters
printf("%c",plain[i]); // Print the decrypted character
}
else
{
cipher[i] = plain[i]; // Copy the character as is
printf("%c",cipher[i]); // Print the character
}
}
getch(); // Wait for a key press before exiting
}