Skip to content

Instantly share code, notes, and snippets.

@WKL-Sec
Created February 26, 2024 19:30
Show Gist options
  • Save WKL-Sec/00a9bd39634a149d5319e45139122e96 to your computer and use it in GitHub Desktop.
Save WKL-Sec/00a9bd39634a149d5319e45139122e96 to your computer and use it in GitHub Desktop.
Simple C++ implementation of double XOR encryption for string obfuscation, showcasing encryption and decryption with two keys.
// White Knight Labs - Offensive Development Course
// String Enbcryption- Double XOR
#include <iostream>
#include <string>
// Function to apply XOR operation between the message and a key
std::string xorEncryptDecrypt(const std::string& text, const std::string& key) {
std::string result = text; // Start with the original text
for (size_t i = 0; i < text.length(); ++i) {
result[i] = text[i] ^ key[i % key.length()]; // Apply XOR with the key, cycling through the key if necessary
}
return result;
}
int main() {
std::string originalText = "Hello, World!";
std::string key1 = "meta123456";
std::string key2 = "code123456";
// Encrypt with the first key
std::string encryptedWithKey1 = xorEncryptDecrypt(originalText, key1);
// Then encrypt the result of the first encryption with the second key
std::string encryptedWithKey2 = xorEncryptDecrypt(encryptedWithKey1, key2);
// To decrypt, reverse the process
std::string decryptedWithKey1 = xorEncryptDecrypt(encryptedWithKey2, key2);
std::string decryptedText = xorEncryptDecrypt(decryptedWithKey1, key1);
std::cout << "Original Text: " << originalText << std::endl;
std::cout << "Encrypted Text with Key 1 then Key 2: ";
for (unsigned char c : encryptedWithKey2) std::cout << std::hex << (int)c << " ";
std::cout << std::endl;
std::cout << "Decrypted Text: " << decryptedText << std::endl;
return 0;
}
@iilegacyyii
Copy link

This is the same as encrypting with one key, except in your case less secure (aware this is just an example though)

meta123456 ^ code123456 leaves the last 6 bytes of your "double XOR key" as null bytes, meaning you're leaving 60% of the data as plaintext if you encrypt with this newly calculated key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment