-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.cpp
95 lines (86 loc) · 1.83 KB
/
decrypt.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
//Vigenere Cipher(decryption)
//Have to create file(.txt) that you want to decrypt first
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 100;
int strLen(char str[])
{
int i = 0;
while (str[i] != '\0')
i++;
return i;
}
char determine(char x)
{
if(x < 10)
x += '0';
else if(x > 9 && x < 36)
x += '7';
else
x += '=';
return x;
}
int main()
{
ofstream outFile;
ifstream inFile;
char key[SIZE], filename[SIZE];
char ch;
int L, i = 0;
cout << "Enter name of input file: ";
cin.getline(filename, SIZE);
inFile.open(filename); //Read the input file
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << ".\n";
cout << "Programing terminating.\n";
exit(EXIT_FAILURE);
}
outFile.open("decryptResult.txt"); //Read the output file
cout << "Please enter the key: ";
cin.getline(key, SIZE);
L = strLen(key);
inFile >> ch;
while (inFile.good()) //Access to read data
{
if (isdigit(ch))
{
ch = determine(((ch - '0') - (key[i % L] - '=') + 62) % 62);
outFile << char(ch);
i++;
}
else if (isupper(ch))
{
ch = determine(((ch - '7') - (key[i % L] - '=') + 62) % 62);
outFile << char(ch);
i++;
}
else if (islower(ch))
{
ch = determine(((ch - '=') - (key[i % L] - '=') + 62) % 62);
outFile << char(ch);
i++;
}
else
{
outFile << char(ch);
}
inFile >> ch;
} //Output will be saved in decryptResult.txt
if (inFile.eof())
cout << "End of file reached.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated for unknown reason.\n";
if (i == 0)
cout << "No data processed.\n";
else
cout << "The output data has been saved to decryptResult.txt.\n";
inFile.close();
outFile.close();
//system("pause");
return 0;
}