forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar_cipher.c
88 lines (73 loc) · 2.08 KB
/
caesar_cipher.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Caesar Cipher Algorithm
Caesar Cipher Algorithm is an old encryption methodology used for encryption of data. Also known as Caesar Shift or
Caesar’s Code, it is one of the most straightforward and prevalent encryption technique. This Caesar Cipher
encryption algorithm is a kind of substitution cipher wherein every character in the plain-text or the user
input is replaced by another character which is defined with a fixed number of positions away from the existing character.
It is a method in which every letter or character in the plain text is altered and shifted. Every text character replaced
by a specific character depending upon the algorithm.
The Caesar Cipher Algorithm is one of the oldest and easiest algorithms for encryption and decryption algorithm
in C programming language.
*/
#include<stdio.h>
#include<stdlib.h>
char data[50], temp;
int key, count;
/* Cipher_Caesar Encryption */
void encryption()
{
for (count = 0; data[count] != '\0'; count++) {
temp = data[count];
if (temp >= 'a' && temp <= 'z') {
temp = temp + key;
if (temp > 'z') {
temp = temp - 'z' + 'a' - 1;
}
data[count] = temp;
} else if (temp >= 'A' && temp <= 'Z') {
temp = temp + key;
if (temp > 'Z') {
temp = temp - 'Z' + 'A' - 1;
}
data[count] = temp;
}
}
printf("Encrypted Message:\t%s\n", data);
}
/* Cipher_Caesar Decryption */
void decryption()
{
for (count = 0; data[count] != '\0'; count++) {
temp = data[count];
if (temp >= 'a' && temp <= 'z') {
temp = temp - key;
if (temp < 'a') {
temp = temp + 'z' - 'a' + 1;
}
data[count] = temp;
} else if (temp >= 'A' && temp <= 'Z') {
temp = temp - key;
if (temp < 'A') {
temp = temp + 'Z' - 'A' + 1;
}
data[count] = temp;
}
}
printf("Decrypted Message:\t%s\n", data);
}
int main()
{
printf("Enter a String:\t");
scanf("%[^\n]s", data);
printf("Enter a Key:\t");
scanf("%d", &key);
encryption();
decryption();
printf("\n");
return 0;
}
/* Sample Output:
Enter a String: HACKINCODES
Enter a Key: 4
Encrypted Message: LEGOMRGSHIW
Decrypted Message: HACKINCODES
*/