-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCesar_crypto.pas
230 lines (182 loc) · 4.12 KB
/
Cesar_crypto.pas
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
program Cesar_crypto;
type matrix = Array[1..5,1..5] of char;
var char_arr : matrix;
alphabet, new_alphabet, key, text, encrpt_txt, decrpt_txt : string;
m,k,a,i,j, key_length, last_i, last_j, text_len: integer;
in_key_char : Boolean;
i_arr, j_arr : Array[1..500] of Integer;
// Deleting same chars and i --> j
Procedure Delete_same_char(var key:string);
var i, j : integer;
begin
for i:=1 to Length(key) do
begin
if key[i] = 'j' then
begin
Delete(key,i,1);
Insert('i',key,i-1);
end;
end;
for i:=1 to Length(key) do
begin
for j:=i+1 to Length(key) do
begin
if ((key[i] = key[j]) OR (key[j] = ' ')) then delete(key,j,1);
end;
end;
end;
Begin {main}
alphabet := 'abcdefghiklmnopqrstuvwxyz';
new_alphabet := '';
encrpt_txt := '';
decrpt_txt := '';
//initializing '0' all the matrix
for i:=1 to 5 do begin
for j:=1 to 5 do begin
char_arr[i,j] := '0';
end;
end;
write('Enter the text you want to encrypt: ');
readln(text);
write('Enter the key string: ');
readln(key);
Delete_same_char(key);
writeln('New key: ', key);
key_length := length(key);
m := 1;
//Put the key in the matrix
for i:=1 to 5 do
begin
for j:=1 to 5 do
begin
char_arr[i,j] := key[m];
last_i := i;
last_j := j;
if((m+1) <= key_length) then m:=m+1
else break;
end;
if((m+1) > key_length) then break;
end;
m:=1;
k:=1;
in_key_char := false;
//Creating new alphabet where there is no key characters
//k = new alphabet indexing
for i:=1 to 26 do
begin
for j:=1 to key_length do
begin
if alphabet[i] = key[j] then in_key_char := true;
end;
if in_key_char = false then new_alphabet := new_alphabet + alphabet[i];
in_key_char := false;
end;
m:=1;
if last_j = 5 then begin
last_j := 1;
last_i := last_i + 1;
end
else last_j := last_j + 1;
//writeln('last_i = ', last_i);
//writeln('last_j = ', last_j);
//Put the alphabet in the matrix
//m = key length indexing
//k = alphabet length indexing
j := last_j;
for i:=last_i to 5 do
begin
while(j <= 5) do
begin
char_arr[i,j] := new_alphabet[m];
m:= m + 1;
j:= j + 1;
end;
j:=1;
end;
//Deleting spaces
for i:=1 to length(text) do
begin
if text[i]=' ' then Delete(text,i,1);
end;
text_len := length(text);
k:=1; //
a:=1;//i and j array indexing
i:=1;//char_arr indexing
j:=1;//char_arr indexing
m:=1; //text indexing
//getting indexes of the text
for m:=1 to text_len do
begin
for i:=1 to 5 do begin
for j:=1 to 5 do begin
if text[m] = char_arr[i,j] then
begin
i_arr[a]:= i;
j_arr[a]:= j;
a:=a+1;
end;
end;
end;
end;
writeln(a);
a := a - 1;
for i:=1 to a do begin
write(i_arr[i]);
end;
writeln;
for j:=1 to a do begin
write(j_arr[j]);
end;
writeln;
for i:=1 to 5 do
begin
for j:=1 to 5 do
begin
write(char_arr[i,j]);
end;
writeln;
end;
writeln;
writeln(text);
writeln;
for i:=1 to length(new_alphabet) do begin
write(new_alphabet[i]);
end;
//i_arr and j_arr seperating for (x,x+1) pares and getting chars from that index from new_alph
if a mod 2 = 1 then begin
j:=2;
i:=1;
while (i <= (a*2)-1) do begin
if i < a then
encrpt_txt := encrpt_txt + char_arr[i_arr[i],i_arr[i+1]];
if i = a then
encrpt_txt := encrpt_txt + char_arr[i_arr[a],j_arr[1]];
if i > a then begin
encrpt_txt := encrpt_txt + char_arr[j_arr[j],j_arr[j+1]];
j := j + 2;
end;
i:= i + 2;
end;
end
else begin
j:=1;
i:=1;
while (i <= (a*2)-1) do begin
if i < a then
encrpt_txt := encrpt_txt + char_arr[i_arr[i],i_arr[i+1]];
if i >= a then begin
encrpt_txt := encrpt_txt + char_arr[j_arr[j],j_arr[j+1]];
j := j + 2;
end;
i:= i + 2;
end;
end;
WriteLn;
writeln('Encrypted text: ', encrpt_txt);
//**************************************************************************
//***************************** DECRYPTION *********************************
for i := 1 to a do begin
decrpt_txt := decrpt_txt + char_arr[i_arr[i],j_arr[i]];
end;
writeln('decrypte text: ', decrpt_txt);
End.