-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesarcipher.c
63 lines (53 loc) · 1.52 KB
/
caesarcipher.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
60
61
62
63
#include <stdio.h>
// Function to convert characters to uppercase
char toUpperCase(char ch) {
if (ch >= 'a' && ch <= 'z') {
return ch - 'a' + 'A';
}
return ch;
}
// Function to encrypt the message using Caesar cipher
void encrypt(char message[], int key) {
int i;
for (i = 0; message[i] != '\0'; ++i) {
if (message[i] >= 'A' && message[i] <= 'Z') {
message[i] = (message[i] + key - 'A') % 26 + 'A';
}
}
}
// Function to decrypt the message using Caesar cipher
void decrypt(char message[], int key) {
int i;
for (i = 0; message[i] != '\0'; ++i) {
if (message[i] >= 'A' && message[i] <= 'Z') {
message[i] = (message[i] - key + 'A' + 26) % 26 + 'A';
}
}
}
int main() {
int key;
char plaintext[1000], ciphertext[1000];
// Input the key
scanf("%d", &key);
// Input plaintext from Alex and encrypt it
scanf(" %[^\n]s", plaintext);
for (int i = 0; plaintext[i] != '\0'; ++i) {
plaintext[i] = toUpperCase(plaintext[i]);
}
// Input encrypted text from Rosa and decrypt it
scanf(" %[^\n]s", ciphertext);
for (int i = 0; ciphertext[i] != '\0'; ++i) {
ciphertext[i] = toUpperCase(ciphertext[i]);
}
// Check for invalid key
if (key < 0 || key > 25) {
printf("INVALID KEY !! CANNOT PERFORM ENCRYPTION !!\n");
}
else{
encrypt(plaintext, key);
printf("%s\n", plaintext);
decrypt(ciphertext, key);
printf("%s\n", ciphertext);
}
return 0;
}