From 4d657b5506a7241ec52891e3808bfed5d1dbd191 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Sat, 17 Apr 2021 21:49:11 +0200 Subject: [PATCH] work in progress --- 059/main.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/059/main.cpp b/059/main.cpp index 56a0b22..60d259b 100644 --- a/059/main.cpp +++ b/059/main.cpp @@ -12,7 +12,7 @@ Your task has been made easy, as the encryption key consists of three lower case #include #include - +#include using namespace std; @@ -20,5 +20,66 @@ int main(){ cout << "Hello this is Patrick" << endl; + ifstream inFile; + inFile.open("../txts/cipher.txt"); + + if(!inFile){ + cerr << "Unable to open file: ../txts/cipher.txt" << endl; + exit(1); + } + + vector encrypted; + while(true){ + unsigned int current; + inFile >> current; + + if(!inFile) break; + + encrypted.push_back(current); + inFile.get(); + } + encrypted.pop_back(); + + inFile.close(); + + for(unsigned char i = 'a'; i <= 'z'; ++i){ + for(unsigned char j = 'a'; j <= 'z'; ++j){ + for(unsigned char k = 'a'; k <= 'z'; ++k){ + const unsigned char key[] = {i,j,k}; + + vector decoded; + for(size_t pos = 0; pos < encrypted.size(); ++pos){ + decoded.push_back(encrypted[pos] ^ key[pos % 3]); + } + + bool valid = true; + for(auto d : decoded){ + valid = (d >= '0' && d <= '9'); + valid |= (d >= 'a' && d <= 'z'); + valid |= (d >= 'A' && d <= 'Z'); + valid |= (d == ' ' || d == ',' || d == '.' || d == '?' || d == '!'); + valid |= (d == ';' || d == ':' || d == '-' || d == '\'' || d == '(' || d == ')'); + + if(!valid){ + break; + } + } + + if(!valid){ + continue; + } + + unsigned int asciiSum = 0; + for(auto d : decoded){ + asciiSum += d; + cout << d; + } + cout << endl; + cout << asciiSum << endl; + return 0; + } + } + } + return 0; } \ No newline at end of file