编译时 std::string 混淆

问题描述 投票:0回答:0

我试图编写函数,用于编译时隐藏(混淆)二进制代码中的文本字符串。目的:使用二进制编辑器禁用轻松搜索和修改文本消息。

算法很简单:将字符串中的字符转换为

deltas
,i。 e.字符串
abcd
必须在二进制文件中保存为
a\001\001\001
.

我写了下面的代码,它有效:

#include <string>
#include <stdio.h>
#include <string.h>

constexpr std::string str_encrypt(const char *str) {
    std::string rc; 
    unsigned char x = 0, c;
    while(c = *str++) {
         rc.push_back(c - x); 
        x = c;
    }   
    return rc; 
}

std::string str_decrypt(const std::string &str) {
    std::string rc; 
    unsigned char x = 0;
    for(unsigned char c : str)
        rc.push_back(x += c); 
    return rc; 
}

const std::string hello(str_encrypt("Hello, world!"));

int main(int argc, char **argv) {
    for(unsigned char c : hello)
        printf("%d ", c); 
    putchar('\n');
    printf("S=[%s]\n", str_decrypt(hello).c_str());
    return 0;
}

但是二进制文件中的字符串仍然没有“加密”:

$ c++ -std=c++2b -O2 constexpr.cpp  && strings a.out | grep Hello
Hello, world!

当我将

constexpr
更改为
consteval
时,我看到编译错误:

$ c++ -std=c++2b -O2 constexpr.cpp 
constexpr.cpp:23:36: error: ‘std::string{std::__cxx11::basic_string<char>::_Alloc_hider{((char*)(&<anonymous>.std::__cxx11::basic_string<char>::<anonymous>.std::__cxx11::basic_string<char>::<unnamed union>::_M_local_buf))}, 13, std::__cxx11::basic_string<char>::<unnamed union>{char [16]{'H', '\035', '\007', '\000', '\003', '\37777777675', '\37777777764', 'W', '\37777777770', '\003', '\37777777772', '\37777777770', '\37777777675', 0}}}’ is not a constant expression
   23 | const std::string hello(str_encrypt("Hello, world!"));

我的问题: 可以编写这样的编译时加密吗?如果是这样,请提出建议,怎么做。

我的编译器:g++ (GCC) 12.2.1 20230201

c++ obfuscation
© www.soinside.com 2019 - 2024. All rights reserved.