37 / 37 / 18
Регистрация: 21.06.2013
Сообщений: 271
|
|
1
|
Почему не полностью работает программа шифрования? шифрует ок, а дешифрирует плохо, теряет последние 2 символа
12.11.2014, 17:27. Показов 771. Ответов 2
вот так. Помогите с проблемой.
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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
| #include <vcl>
#include <conio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
//Видаленя
void Del(char *s);
// верхн регістр
void touper(char *text);
//вставка сим
void vstavka_pusti(char *text);
//шифрування
void encr(char *s);
void crypto(char *text);
char tabul[10][10];
string ResText = "";
int main()
{
int i,j=0;
char alfavit[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ",*p,*text1;
string str="Samsonova"; //Ключ
string str1;
//зашифрований текст
string Resh = ""; //
//верхн регістр
transform(str.begin(), str.end(), str.begin(), toupper) ;
//подвоєня видалення
str.erase(std::unique(str.begin(), str.end()), str.end());
//з стінг в чар
p=str.begin();
strcat(p,alfavit);
cout<<p<<endl;
Del(p);
//таблиця
for(int i=0;i<strlen(p);i++)
{
int j1=i%5;
int i1=i/5;
tabul[i1][j1]=p[i];
}
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
cout<<tabul[i][j]<<" "; }
cout<<endl; }
char text[]="africadamaonka";
char ost2bykv[3];
ost2bykv[0]=text[strlen(text)-2];
ost2bykv[1]=text[strlen(text)-1];
cout<<"ost2"<<ost2bykv<<endl;
cout<<"\nTEX ";
for(i=0;i<strlen(text);i++)
if(text[i]==' ')
text[i]='_';
touper(text);
vstavka_pusti(text);
touper(ost2bykv);
cout<<text<<endl;
crypto(text);
p=ResText.begin();
encr(p);
p=ResText.begin();
getch();
return 0;
}
/*ДЕшифрування тексту
*******************************************************************************/
void encr(char *text1)
{
int k=0;
int ind_x1 = 0;
int ind_y1 = 0;
int ind_x2 = 0;
int ind_y2 = 0;
string ResText = "";
while (k<strlen(text1)-2)
{
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
{
if (text1[k] == tabul[i][j])
{
ind_x1 = i;
ind_y1 = j;
}
if (text1[k+1] == tabul[i][j])
{
ind_x2 = i;
ind_y2 = j;
}
}
// Если буквы находятся в одной строке
if (ind_x1 == ind_x2)
{
if (ind_y1 == 0)
{
ResText += tabul[ind_x1][4];
ResText += tabul[ind_x2][ind_y2-1];
}
else
if (ind_y2 == 0)
{
ResText += tabul[ind_x1][ind_y1-1];
ResText += tabul[ind_x2][4];
}
else
{
ResText += tabul[ind_x1][ind_y1-1];
ResText += tabul[ind_x2][ind_y2-1];
}
}
// Если буквы находятся в одном столбце
if (ind_y1 == ind_y2)
{
if (ind_x1 == 0)
{
ResText += tabul[4][ind_y1];
ResText += tabul[ind_x2-1][ind_y2];
}
else
if (ind_x2 == 0)
{
ResText += tabul[ind_x1-1][ind_y1];
ResText += tabul[4][ind_y2];
}
else
{
ResText += tabul[ind_x1-1][ind_y1];
ResText += tabul[ind_x2-1][ind_y2];
}
}
// Если буквы находятся в разных строках и разных столбцах
if ((ind_x1 != ind_x2) && (ind_y1 != ind_y2))
{
ResText += tabul[ind_x1][ind_y2];
ResText += tabul[ind_x2][ind_y1];
}
k = k + 2;
}
cout<<"Enc "<<ResText;
}
/*Шифрування тексту
*******************************************************************************
*/
void crypto(char *text)
{
int ind_x1 = 0;
int ind_y1 = 0;
int ind_x2 = 0;
int ind_y2 = 0;
int k = 0;
while (k<strlen(text))
{
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
{
if (text[k] == tabul[i][j])
{
ind_x1 = i;
ind_y1 = j;
}
if (text[k+1] == tabul[i][j])
{
ind_x2 = i;
ind_y2 = j;
}
}
// якшо в однакових рядках
if (ind_x1 == ind_x2)
{
if (ind_y1 == 4)
{
ResText += tabul[ind_x1][0];
ResText += tabul[ind_x2][ind_y2+1];
}
else
if (ind_y2 == 4)
{
ResText += tabul[ind_x1][ind_y1+1];
ResText += tabul[ind_x2][0];
}
else
{
ResText += tabul[ind_x1][ind_y1+1];
ResText += tabul[ind_x2][ind_y2+1];
}
}
//якщо на в одному стовпці
if (ind_y1 == ind_y2)
{
if (ind_x1 == 4)
{
ResText += tabul[0][ind_y1];
ResText += tabul[ind_x2+1][ind_y2];
}
else
if (ind_x2 == 4)
{
ResText += tabul[ind_x1+1][ind_y1];
ResText += tabul[0][ind_y2];
}
else
{
ResText += tabul[ind_x1+1][ind_y1];
ResText += tabul[ind_x2+1][ind_y2];
}
}
//якщо в різних стовпцях і рядках
if ((ind_x1 != ind_x2) && (ind_y1 != ind_y2))
{
ResText += tabul[ind_x1][ind_y2];
ResText += tabul[ind_x2][ind_y1];
}
k = k + 2;
}
cout<<"Res "<<ResText<<endl;
}
void Del(char *s)
{
int flag[256] = {0};
char *ps = s;
while(*ps)
if (!flag[*ps])
{
flag[*ps] = 1;
ps++;
}
else strcpy(ps, ps + 1);
}
void touper(char *text)
{
int i;
for(i = 0; text[i] != '\0'; i++)
if( text[i] <= 'z' && text[i] >= 'a')
text[i] += 'A' - 'a';
}
void vstavka_pusti(char *text)
{
char result[100];
int j=0;
for(int i=0; i<strlen(text);i++)
{
result[j++]=text[i];
if(text[i]==text[i+1])
result[j++]='.';
}
result[j++]=0;
//провірка на парність
int dovz;
dovz=strlen(result);
if (dovz%2!=0)
result[dovz]='.';
result[j++]=0;
strcpy(text,result);
} |
|
__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь
0
|