-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.cpp
More file actions
97 lines (96 loc) · 2.04 KB
/
Cipher.cpp
File metadata and controls
97 lines (96 loc) · 2.04 KB
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
#include "Cipher.h"
Cipher::Cipher(int key)
{
k=key;
}
wstring Cipher::zakodirovat(Cipher w, wstring& s)
{
wstring code;
s=getValidOpenText(s);
w.k=getValidKey(w.k,s);
int h;
if (s.size()%w.k!=0) {
h=s.size()/w.k+1;
} else {
h=s.size()/w.k;
}
wchar_t a[h][w.k];
int k=0;
for (int i=0; i<h; i++) {
for (int j=0; j<w.k; j++) {
if (k<s.size()) {
a[i][j]=s[k];
k++;
} else a[i][j]=' ';
}
}
for (int i=0; i<w.k; i++) {
for (int j=0; j<h; j++) {
code+=a[j][i];
}
}
return code;
}
wstring Cipher::raskodirovar(Cipher w, wstring& s)
{
s=getValidOpenText(s);
w=getValidKey(w.k,s);
int h;
if (s.size()%w.k!=0) {
h=s.size()/w.k+1;
} else {
h=s.size()/w.k;
}
wchar_t a[h][w.k];
int k=0;
for (int i=0; i<w.k; i++) {
for (int j=0; j<h; j++) {
a[j][i]=s[k];
k++;
}
}
wstring decode;
for (int i=0; i<h; i++) {
for (int j=0; j<w.k; j++) {
decode+=a[i][j];
}
}
return decode;
}
inline int Cipher::getValidKey(const int k, const std::wstring & s)
{
if (k<=0)
throw cipher_error("IVALID KEY");
else if (k>(s.size()/2))
throw cipher_error("THE KEY IS TOO LONG");
else
return k;
}
inline std::wstring Cipher::getValidOpenText(const std::wstring & s)
{
std::wstring tmp;
for (auto c:s) {
if (isalpha(c)) {
if (islower(c)) {
tmp.push_back(toupper(c));
} else
tmp.push_back(c);
}
}
if (tmp.empty())
throw cipher_error("NO TEXT");
return tmp;
}
inline std::wstring Cipher::getValidCipherText(const std::wstring & s)
{
std::wstring tmp;
for (auto c:s) {
if (isalpha(c)) {
if (islower(c)){
tmp.push_back(toupper(c));
} else
tmp.push_back(c);
}
}
return tmp;
}