Skip to content

Instantly share code, notes, and snippets.

@etodanik
Created April 28, 2017 18:38
Show Gist options
  • Save etodanik/b2f042ad1a5be2f9c62dd5d3218e4a75 to your computer and use it in GitHub Desktop.
Save etodanik/b2f042ad1a5be2f9c62dd5d3218e4a75 to your computer and use it in GitHub Desktop.
main() function for the Shabak Challenge Part 2
int main()
{
// Load Key.bin into a vector of EncryptionStepDescriptor structures
std::vector<EncryptionStepDescriptor> descriptors = loadKey("C:\\Users\\Danny\\Documents\\Visual Studio 2015\\Projects\\LikeABoss\\Debug\\Key.bin");
// Load the encrypted message
std::vector<uint8_t> v = loadCipher("C:\\Users\\Danny\\Documents\\Visual Studio 2015\\Projects\\LikeABoss\\Debug\\EncryptedMessage.bin");
string str(v.begin(), v.end());
// The cursor keeps track of our current position in the encrypted message
int cursor = 0;
// This is just a step number for logging purposes
int stepn = 1;
// We also need to know the direction we're currently moving our cursor in (Forward or Backward)
int direction = Forward;
cout << "Before decryption: " << endl << str << endl;
for (auto const& step : descriptors) {
cout << "Step #" << stepn << ": operation " << getOperationName(step.operationCode) << " with param " << (int) step.operationParameter << " for " << step.lengthToOperateOn << " chars" << endl;
for (uint32_t i = 0; i < step.lengthToOperateOn; i++) {
// We apply the current step to the byte our cursor points to in the vector
uint8_t dc = getDecryptedChar(v[cursor], step.operationCode, step.operationParameter);
v[cursor] = dc;
// If we reach the beginning or end of the file we reverse direction
if (direction == Forward && cursor < v.size() - 1) {
cursor++;
}
else if (direction == Forward && cursor == v.size() - 1) {
direction = Backward;
}
else if (direction == Backward && cursor > 0) {
cursor--;
}
else if (direction == Backward && cursor == 0) {
direction = Forward;
}
}
stepn++;
}
string dStr(v.begin(), v.end());
// We can also save the output into a text file if we want:
// ofstream output_file("C:\\Users\\Danny\\Documents\\Visual Studio 2015\\Projects\\LikeABoss\\Debug\\DecryptedMessage.txt", std::ios::out | std::ifstream::binary);
// output_file.write((char*)&v[0], v.size() * sizeof(v[0]));
// output_file.close();
cout << "After decryption: " << endl << dStr << endl;
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment