-
Notifications
You must be signed in to change notification settings - Fork 0
/
affine.cpp
83 lines (60 loc) · 1.87 KB
/
affine.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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void affine()
{
ifstream crypted_file("encr_affine.txt", ios::in); //file with the encrypted text
ofstream output_file("output.txt", ios::out); //file with the key pairs' generated decrypted text
string file_line;
string buffer;
string possible_message;
int flag=0;
int inverted_a=0;
if(crypted_file.good()) //if the file opening returns no errors...
{
while(!crypted_file.eof()) //scan the file to the end
{
getline(crypted_file, file_line); //copy each line of the file
buffer+=file_line; //and add it to the buffer
}
}
crypted_file.close();
for(int i = 0; i < buffer.length(); i++) //turn buffer text from lower to uppercase
buffer[i] = toupper(buffer[i]);
for(int a=0;a<26;a++) //searching for k1...
{
for (int i=0;i<26;i++) //check if k1 is proper to be used as inverted a
{
flag = (a*i) % 26; //should be a*a^-1 mod m == 1 to be used as inverted a
if (flag==1)
{
for(int l=0;l<=a;l++)
{
inverted_a=((l*26)+1);
if(inverted_a%a==0)
break;
}
inverted_a=inverted_a/a;
break;
}
}
if(flag==1)
{
for(int b=0;b<26;b++) //searching for k2...
{
possible_message="";
for (int i=0;i<buffer.length();i++) //scan the buffer
{
if(buffer[i]!=' ') //decode the buffer text with each pair of k1,k2
possible_message = possible_message + (char) (((inverted_a * ((buffer[i]+'A' - b)) % 26)) + 'A');
else //leave the spaces as they are
possible_message+= buffer[i];
}
output_file<<"k1= "<<a<<", k2= "<<b<<", Output: "<<possible_message<<endl; //print the possible message to the output file
}
output_file<<endl;
}
}
output_file.close();
}