Developer knowledge network ยท moderated exchange

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Knowledge indexLive
4Categories
919Threads
2.8KPosts
Release

C++20 consteval Compile-Time String Encryption Macro with dynamic XOR keys

vtable_slayer
Senior Reverser
MEMBER
Rep: 215
Join Date: Mar 2018
Posts: 86
Thanks: 61
3 weeks ago ยท Jul 27, 2026 2:27 PM
#1

Clean C++20 string obfuscation macro that encrypts strings at compile time with zero runtime decryption overhead:

CPP
template <size_t N, uint32_t Seed>
class ObfuscatedString {
    char m_data[N];
public:
    consteval ObfuscatedString(const char(&str)[N]) {
        for (size_t i = 0; i < N; i++) {
            m_data[i] = str[i] ^ (char)((Seed + i * 7) & 0xFF);
        }
    }
    std::string Decrypt() const {
        std::string res(N - 1, '\0');
        for (size_t i = 0; i < N - 1; i++) {
            res[i] = m_data[i] ^ (char)((Seed + i * 7) & 0xFF);
        }
        return res;
    }
};

#define CRYPT_STR(s) ([]() { consteval ObfuscatedString<sizeof(s), __LINE__ * 0x5F37> x(s); return x; }().Decrypt())
ptr_arithmetic
C++ Wizard
MEMBER
Rep: 162
Join Date: May 2018
Posts: 73
Thanks: 42
3 weeks ago ยท Jul 27, 2026 5:19 PM
#2

Using __LINE__ as entropy seed generates unique XOR keys for every single string in the codebase automatically.

ghidra_fanatic
RE Specialist
MEMBER
Rep: 101
Join Date: Mar 2019
Posts: 16
Thanks: 14
3 weeks ago ยท Jul 28, 2026 3:49 AM
#3

Checked binary in IDA Pro: .rdata strings list is 100% scrambled bytes.