-
Notifications
You must be signed in to change notification settings - Fork 1
/
tc.cpp
162 lines (155 loc) · 5.28 KB
/
tc.cpp
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
#include<bits/stdc++.h>
using namespace std;
int N;
map<int,int> keyMap;
int countPairs(string s1,string s2)
{
int f1[26] = { 0 };
int f2[26] = { 0 };
int cnt = 0;
for (int i = 0; i < s1.length(); i++)
f1[s1[i] - 'A']++;
for (int i = 0; i < s2.length(); i++)
f2[s2[i] - 'A']++;
for (int i = 0; i < 26; i++)
cnt += (min(f1[i], f2[i]));
return cnt;
}
int main()
{
freopen("transposition-57.txt","r",stdin);
freopen("output.txt","w",stdout);
string s;
string words[10];
for(int i=0; i<10; i++)
words[i]="";
getline(cin,s);
cout << "Cipher : " << s << endl;
string word;
getline(cin,word);
getline(cin,word);
for(int i=0; i<word.length(); i++)
{
if(word[i]==',')
N++,i++;
else if(word[i]==' ')
continue;
else
words[N].push_back(word[i]);
}
N++;
cout << "Words : ";
for(int i=0; i<N; i++)
{
cout << words[i] << " ";
transform(words[i].begin(),words[i].end(),words[i].begin(),::toupper);
}
cout << endl << endl;
for(int k=2; k<=10; k++)
{
cout << "Key length : " << k << endl;
int l=ceil(s.length()/k);
for(int i=0; i<l; i++)
{
string temp="";
for(int j=0; j<k; j++)
{
temp.push_back(s[i+j*l]);
}
for(int m=0; m<words[0].length()-k+1; m++)
{
int key[k],used[k];
if(countPairs(words[0].substr(m,k),temp)==k)
{
string tt=words[0].substr(m,k);
cout << words[0] <<" " << words[0].substr(m,k)<<" " << temp;
for(int i=0; i<k; i++)
used[i]=0;
for(int g=0; g<k; g++)
{
for(int h=0; h<k; h++)
{
if(tt[g]==temp[h] && !used[h])
{
key[g]=h+1,used[h]=1;
break;
}
}
}
for(int i=0; i<k; i++)
cout << " " << key[i] << " ";
cout << endl;
//
for(int i=0; i<k; i++)
{
keyMap[key[i]]=i+1;
}
//
string plain="";
for(int m=0; m<l; m++)
{
for(int j=0; j<k; j++)
{
plain.push_back(s[m+(key[j]-1)*l]);
}
}
cout << plain << endl;
// check for other words
int c=0;
for(int w=1; w<N; w++)
{
for(int i=0; i<plain.length()-words[w].length()+1; i++)
{
if(!plain.substr(i,words[w].length()).compare(words[w]))
cout << plain.substr(i,words[w].length()) << " matched" << endl,c++;
}
}
if(c==N-1)
{
cout << "Plain text : " << endl;
transform(plain.begin(), plain.end(), plain.begin(), ::tolower);
cout << plain << endl;
cout << "Key length : " << k << endl;
cout << "Order : " ;
for(int i=0; i<k; i++)
cout << key[i] << " ";
cout << endl;
cout << "Encoded using found key : ";
int l=ceil(plain.length()/k);
string encoded="";
//
cout << endl;
for(int i=0; i<k; i++)
{
cout << keyMap[i+1] << " ";
}
cout << endl;
//
for(int j=0; j<k; j++)
{
for(int i=0; i<plain.length(); i=i+k)
{
encoded.push_back(plain[i+(keyMap[j+1]-1)]-'a'+'A');
}
}
cout << encoded << endl;
int cnt=0;
for(int i=0; i<encoded.length(); i++)
{
if(s[i]==encoded[i])
cnt++;
}
cout << "Accuracy : " << cnt/encoded.length()*100 << "%" << endl;
return 0;
}
else
{
cout << c << " words matched" << endl;
}
cout << endl;
}
}
}
cout << endl << endl;
}
}