forked from shalithasuranga/RailFenceCipher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RailFence.c
113 lines (105 loc) · 2.32 KB
/
RailFence.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ---------------------------
RAIL FENCE CIPHER
----------------------------- */
void encrypt(char *S,int key,int n){
/*-- create rail maxtrix --*/
char R[key][n];
/*-- fill with _ chars --*/
for(int i=0; i<key; i++) for(int j=0; j<n; j++) R[i][j]='_';
/*-- add plaintext in zig-zag order --*/
int multi=1;
for(int j=0,i=0; j<n ;j++,i+=1*multi){
if(i==0){
multi=1;
}
else if(i==key-1){
multi=-1;
}
R[i][j]=S[j];
}
/*-- display ciphertext */
printf("%s", "Ciphertext : ");
for(int i=0; i<key; i++){
for(int j=0; j<n; j++){
if(R[i][j]!='_'){
printf("%c", R[i][j]);
}
}
}
}
void decrypt(char *S,int key,int n){
/*-- create rail maxtrix --*/
char R[key][n];
/*-- fill with _ chars --*/
for(int i=0; i<key; i++) for(int j=0; j<n; j++) R[i][j]='_';
/*-- mark zig-zag path with # char --*/
int multi=1;
for(int j=0,i=0; j<n ;j++,i+=1*multi){
if(i==0)
multi=1;
else if(i==key-1)
multi=-1;
R[i][j]='#';
}
/*-- fill the # values with ciphertext values --*/
int z=0;
for(int i=0; i<key; i++){
for(int j=0; j<n; j++){
if(R[i][j]=='#')
R[i][j]=S[z++];
}
}
/*-- display plaintext --*/
printf("%s", "Plaintext : ");
for(int j=0; j<n; j++){
for(int i=0; i<key; i++){
if(R[i][j]!='_'){
printf("%c", R[i][j]);
}
}
}
}
int main(int args0, char **args1){
char S[100];
int key,n,choice;
while(choice!=3){
printf("\n------------------\n");
printf("---- RAIL-FENCE CIPHER ----\n");
printf("------------------\n");
printf("1 : Encryption\n");
printf("2 : Decryption\n");
printf("3 : Quit\n");
printf("Enter choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter Plaintext : ");
fflush(stdin);
fgets(S,100,stdin);
printf("Enter key (positive integer) : ");
scanf("%d",&key);
n=strlen(S)-1;
encrypt(S,key,n);
break;
case 2:
printf("Enter Ciphertext : ");
fflush(stdin);
fgets(S,100,stdin);
printf("Enter key (positive integer) : ");
scanf("%d",&key);
n=strlen(S)-1;
decrypt(S,key,n);
break;
case 3:
printf("Exit success\n");
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}