我们可以用以下代码把你的代码加密,来让别人看不懂。
// 假设你的文件是 obfuscator.cpp ,obfuscator.cpp — 把一份 C++ 源码里的 token 全部换成下划线宏
//
// 用法:
// obfuscator < in.cpp > out.cpp 从标准输入读,写到标准输出
// obfuscator in.cpp > out.cpp
// obfuscator in.cpp out.cpp
//
// 输出仍是合法 C++:每个非 # 行里的 token(关键字、标识符、运算符、
// 数字、字符串)统一替换成 _ / __ / ___ ...,宏表放在文件开头。
// # 开头的预处理指令原样保留(放最前面的那批会被提到宏表上方)。
#include <bits/stdc++.h>
using namespace std;
namespace obf
{
static bool ident_char(char c)
{
return isalnum((unsigned char)c) || c == '_';
}
// 去掉 // 与 /* */ 注释,但跳过字符串/字符常量;换行原样保留
static string strip_comments(const string &s)
{
string out;
out.reserve(s.size());
for (size_t i = 0; i < s.size(); )
{
char c = s[i];
if (c == '"' || c == '\'')
{
out.push_back(c);
i++;
while (i < s.size())
{
char d = s[i];
out.push_back(d);
if (d == '\\' && i + 1 < s.size())
{
out.push_back(s[i + 1]);
i += 2;
continue;
}
i++;
if (d == c) break;
}
}
else if (c == '/' && i + 1 < s.size() && s[i + 1] == '/')
{
while (i < s.size() && s[i] != '\n') i++;
}
else if (c == '/' && i + 1 < s.size() && s[i + 1] == '*')
{
i += 2;
while (i + 1 < s.size() && !(s[i] == '*' && s[i + 1] == '/'))
{
if (s[i] == '\n') out.push_back('\n');
i++;
}
if (i + 1 < s.size()) i += 2;
}
else
{
out.push_back(c);
i++;
}
}
return out;
}
static vector<string> split_lines(const string &s)
{
vector<string> res;
string cur;
for (char c : s)
{
if (c == '\n')
{
if (!cur.empty() && cur.back() == '\r') cur.pop_back();
res.push_back(cur);
cur.clear();
}
else
{
cur.push_back(c);
}
}
if (!cur.empty())
{
if (cur.back() == '\r') cur.pop_back();
res.push_back(cur);
}
return res;
}
static bool is_directive(const string &line)
{
size_t i = 0;
while (i < line.size() && isspace((unsigned char)line[i])) i++;
return i < line.size() && line[i] == '#';
}
// 最长匹配多字符运算符,其余单字符直接落单
static vector<string> tokenize(const string &line)
{
static const unordered_set<string> multi = {
"<<=", ">>=", "<=>", "->*", "...", "::", "->", "++", "--", "<<", ">>",
"<=", ">=", "==", "!=", "&&", "||", "+=", "-=", "*=", "/=", "%=",
"&=", "|=", "^=", "##", ".*"
};
vector<string> res;
size_t i = 0;
size_t n = line.size();
while (i < n)
{
unsigned char c = (unsigned char)line[i];
if (isspace(c))
{
i++;
continue;
}
if (c == '"' || c == '\'')
{
size_t st = i;
i++;
while (i < n)
{
if (line[i] == '\\' && i + 1 < n)
{
i += 2;
continue;
}
if (line[i] == (char)c)
{
i++;
break;
}
i++;
}
res.push_back(line.substr(st, i - st));
}
else if (isdigit(c))
{
size_t st = i;
while (i < n)
{
unsigned char d = (unsigned char)line[i];
if (isalnum(d) || d == '.' || d == '_')
{
i++;
continue;
}
if ((d == '+' || d == '-') && i > st)
{
char prev = line[i - 1];
if (prev == 'e' || prev == 'E' || prev == 'p' || prev == 'P')
{
i++;
continue;
}
}
break;
}
res.push_back(line.substr(st, i - st));
}
else if (isalpha(c) || c == '_')
{
size_t st = i;
while (i < n && ident_char(line[i])) i++;
res.push_back(line.substr(st, i - st));
}
else
{
string take;
for (int len = 4; len >= 2; len--)
{
if (i + (size_t)len <= n && multi.count(line.substr(i, len)))
{
take = line.substr(i, len);
break;
}
}
if (take.empty()) take = string(1, line[i]);
res.push_back(take);
i += take.size();
}
}
return res;
}
// 这几个 token 不能换成宏:函数式宏调用要靠字面的 ( ) , 才能被识别
static const unordered_set<string> &keep_literal()
{
static const unordered_set<string> keep = {"(", ")", ","};
return keep;
}
static bool only_underscores(const string &s)
{
if (s.empty()) return false;
for (char c : s)
{
if (c != '_') return false;
}
return true;
}
struct Namer
{
unordered_map<string, string> mp;
vector<pair<string, string>> table;
set<int> reserved; // 源码里本来就有的全下划线标识符,别覆盖它
int next_len = 1;
void reserve(const string &t)
{
if (only_underscores(t)) reserved.insert((int)t.size());
}
string name_of(const string &t)
{
if (only_underscores(t) || keep_literal().count(t)) return t;
auto it = mp.find(t);
if (it != mp.end()) return it->second;
int len = next_len;
while (reserved.count(len)) len++;
next_len = len + 1;
string name((size_t)len, '_');
mp[t] = name;
table.push_back(make_pair(name, t));
return name;
}
};
static void emit_tokens(ostream &os, const vector<string> &toks, Namer &namer, int seed)
{
const size_t width = 160;
string cur((size_t)(seed % 4), ' ');
bool first = true;
for (const string &t : toks)
{
string nm = namer.name_of(t);
if (!first && cur.size() + 1 + nm.size() > width)
{
os << cur << "\n";
cur.assign((size_t)((seed * 5 + 2) % 4), ' ');
first = true;
}
if (!first) cur.push_back(' ');
cur += nm;
first = false;
}
os << cur << "\n";
}
enum LineKind
{
BLANK,
DIRECTIVE,
BODY
};
struct Line
{
LineKind kind = BLANK;
string text;
vector<string> toks;
};
static string read_all(istream &in)
{
return string(istreambuf_iterator<char>(in), istreambuf_iterator<char>());
}
static string run(const string &src)
{
vector<string> raw = split_lines(strip_comments(src));
vector<Line> lines;
for (size_t i = 0; i < raw.size(); i++)
{
Line ln;
if (is_directive(raw[i]))
{
ln.kind = DIRECTIVE;
ln.text = raw[i];
// 处理 #define 行尾反斜杠续行,拼成一行再原样输出
while (!ln.text.empty() && ln.text.back() == '\\' && i + 1 < raw.size())
{
ln.text.pop_back();
i++;
ln.text += " " + raw[i];
}
}
else
{
ln.text = raw[i];
ln.toks = tokenize(raw[i]);
ln.kind = ln.toks.empty() ? BLANK : BODY;
}
lines.push_back(ln);
}
size_t first_body = lines.size();
for (size_t i = 0; i < lines.size(); i++)
{
if (lines[i].kind == BODY)
{
first_body = i;
break;
}
}
Namer namer;
for (size_t i = first_body; i < lines.size(); i++)
{
if (lines[i].kind == BODY)
{
for (const string &t : lines[i].toks) namer.reserve(t);
}
else if (lines[i].kind == DIRECTIVE)
{
// 指令里 #define 的名字也可能和生成的全下划线宏重名
for (const string &t : tokenize(lines[i].text)) namer.reserve(t);
}
}
ostringstream body;
int seed = 0;
for (size_t i = first_body; i < lines.size(); i++)
{
if (lines[i].kind == DIRECTIVE) body << lines[i].text << "\n";
else if (lines[i].kind == BLANK) body << "\n";
else emit_tokens(body, lines[i].toks, namer, seed++);
}
ostringstream out;
for (size_t i = 0; i < first_body; i++) out << lines[i].text << "\n";
if (!namer.table.empty())
{
out << "\n";
for (const auto &p : namer.table) out << "#define " << p.first << " " << p.second << "\n";
out << "\n";
}
out << body.str();
return out.str();
}
}
int main(int argc, char **argv)
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string src;
if (argc >= 2)
{
ifstream fin(argv[1], ios::binary);
if (!fin)
{
cerr << "obfuscator: cannot open " << argv[1] << "\n";
return 1;
}
src = obf::read_all(fin);
}
else
{
src = obf::read_all(cin);
}
string res = obf::run(src);
if (argc >= 3)
{
ofstream fout(argv[2], ios::binary);
if (!fout)
{
cerr << "obfuscator: cannot write " << argv[2] << "\n";
return 1;
}
fout << res;
}
else
{
cout << res;
}
return 0;
}