-
Notifications
You must be signed in to change notification settings - Fork 0
/
Additive Cipher.c
65 lines (55 loc) · 1.25 KB
/
Additive 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
#include<stdio.h>
int main()
{
char stringmsg[50];
char c1; int i, k;
char stringmsg1[50];
char c2; int i1, k1;
printf("Enter the message to be encrypted: ");
scanf("%s",stringmsg);
printf("\nEnter the key: ");
scanf("%d", &k);
// cipher text encryption
for(i=0; stringmsg[i]!='\0'; i++){
c1 = stringmsg[i];
if(c1>='A' && c1 <='Z'){
c1=c1+k;
if(c1>'Z'){
c1 = c1 - 'Z' + 'A' - 1;
}
stringmsg[i] = c1;
}
else if(c1>='a'&&c1<='z'){
c1=c1+k;
if(c1>'z'){
c1=c1-'z'+'a'-1;
}
stringmsg[i] = c1;
}
}
printf("\nThe Encrypted message is: %s", stringmsg);
printf("\nEnter the message to be decrypted: ");
scanf("%s",stringmsg1);
printf("\nEnter the key: ");
scanf("%d", &k1);
// cipher text decryption
for(i1=0; stringmsg[i1]!='\0'; i1++){
c2 = stringmsg1[i1];
if(c2>='A' && c2 <='Z'){
c2=c2-k1;
if(c2<'A'){
c2=c2+'Z'-'A'+1;
}
stringmsg1[i1] = c2;
}
else if(c2>='a'&&c2<='z'){
c2=c2-k1;
if(c2<'a'){
c2=c2+'z'-'a'+1;
}
stringmsg1[i1] = c2;
}
}
printf("\nThe Decrypted message is: %s", stringmsg1);
return 0;
}