-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge4.cpp
More file actions
53 lines (39 loc) · 1018 Bytes
/
challenge4.cpp
File metadata and controls
53 lines (39 loc) · 1018 Bytes
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
/*
Detect single-character XOR
One of the 60-character strings in this file has been encrypted by single-character XOR.
Find it.
(Your code from #3 should help.)
*/
#include "challenges.h"
#include "encoding_utils.h"
#include "heuristic.h"
std::string challenge4() {
//open and read
//ittterate over every line
//itterate over every char
//score everything and return the best score
std::ifstream file("4.txt");
if (!file.is_open()) {
return "File 4.txt failed to open";
}
std::string line;
double bestScore = 0.0;
int bestLine = 0, l = 0;
std::string bestPlaintext = "";
while (std::getline(file, line)) {
std::string ciphertext = hexToText(line);
for(unsigned char c = 0; c < 255; c++) {
std::string key(ciphertext.length(), c);
std::string plaintext = fixedXor(ciphertext, key);
double score = scoreText(plaintext);
if (score > bestScore) {
bestScore = score;
bestLine = l;
bestPlaintext = plaintext;
}
}
l++;
}
file.close();
return bestPlaintext;
}