X27CN 高级代码混淆加密库 - Advanced code obfuscation with variable renaming, string encryption, control flow flattening
Project description
X27CN
X27CN 代码混淆加密库 - Code obfuscation and encryption library
安装
pip install x27cn
快速开始
import x27cn
# 加密
encrypted = x27cn.encrypt('Hello World')
print(encrypted) # <faee><38db>...
# 解密
decrypted = x27cn.decrypt(encrypted)
print(decrypted) # Hello World
# 使用自定义密钥
encrypted = x27cn.encrypt('敏感数据', key='mySecretKey')
decrypted = x27cn.decrypt(encrypted, key='mySecretKey')
文件混淆(前端代码保护)
Python API
import x27cn
# 混淆整个 HTML 文件(生成自解密页面)
x27cn.obfuscate_file('index.html') # 生成 index.obf.html
# 混淆 JavaScript 文件
x27cn.obfuscate_file('app.js') # 生成 app.obf.js
# 混淆 CSS 文件
x27cn.obfuscate_file('style.css') # 生成 style.obf.css
# 自定义输出路径和密钥
x27cn.obfuscate_file('app.html', 'dist/app.html', key='myKey')
命令行
# 混淆 HTML
x27cn obfuscate index.html
# 混淆 JS
x27cn obfuscate app.js dist/app.js
# 使用自定义密钥
x27cn obfuscate app.html --key=mySecretKey
# 加密文本
x27cn encrypt -t "Hello World"
# 解密文本
x27cn decrypt -t "<faee><38db>..."
混淆效果
原始 HTML:
<!DOCTYPE html>
<html>
<body>
<h1>Hello World</h1>
<script>alert('Secret!');</script>
</body>
</html>
混淆后:
<!DOCTYPE html>
<html>
<body>
<script>
(function(){var _$='<9525><0d5b>...';var _k=[0x78,0x32,...];...})();
</script>
</body>
</html>
浏览器加载混淆后的文件会自动解密并正常显示原始内容。
内联混淆
import x27cn
html = '''
<html>
<style>body { color: red; }</style>
<script>alert('hello');</script>
</html>
'''
# 只混淆内联 JS
result = x27cn.obfuscate_inline_js(html)
# 只混淆内联 CSS
result = x27cn.obfuscate_inline_css(html)
API 参考
基础加密/解密
# 标准格式(<xxxx> 标签)
encrypted = x27cn.encrypt('text')
decrypted = x27cn.decrypt(encrypted)
# 纯十六进制格式
hex_encrypted = x27cn.encrypt_hex('text')
decrypted = x27cn.decrypt_hex(hex_encrypted)
# Base64 格式
b64_encrypted = x27cn.encrypt_base64('text')
decrypted = x27cn.decrypt_base64(b64_encrypted)
文件混淆
# 混淆整个文件
x27cn.obfuscate_file(input_path, output_path=None, key='x27cn2026')
# 混淆 HTML 字符串
x27cn.obfuscate_html(html_content, key='x27cn2026')
# 混淆 JS 字符串
x27cn.obfuscate_js(js_content, key='x27cn2026')
# 混淆 CSS 字符串
x27cn.obfuscate_css(css_content, key='x27cn2026')
密钥管理
# 使用默认密钥
x27cn.encrypt('data') # 使用 'x27cn2026'
# 自定义密钥
x27cn.encrypt('data', key='myKey')
# 生成随机密钥
random_key = x27cn.generate_key(16) # 16 字符随机密钥
高级 JavaScript 混淆(防AI识别)
v1.2.0 新增高级混淆功能,使代码难以被 AI 分析识别:
混淆技术
| 技术 | 说明 |
|---|---|
| 变量名混淆 | handleWebSocket → _0x3f2a |
| 字符串加密 | "websocket" → String.fromCharCode(119,101,98...) |
| 控制流扁平化 | if-else → 三元表达式 |
| 死代码注入 | 插入无意义代码增加分析难度 |
| 注释移除 | 删除所有注释信息 |
| 反调试 | 定时触发 debugger 陷阱 |
Python API
import x27cn
# 混淆 JavaScript 代码
code = '''
const UUID = "secret-uuid";
function handleWebSocket(request) {
if (request.upgrade === "websocket") {
return connectProxy(request);
}
}
'''
# 高级混淆(level: low/medium/high/maximum)
protected = x27cn.obfuscate_js_advanced(code, level='maximum')
print(protected)
# 输出: const _0x9534e9 = "\x73\x65\x63..."; function _0x7816ed(_0x6f2a5d) {...}
# 混淆 HTML 中的所有 JavaScript
html = '<html><script>alert("secret")</script></html>'
protected_html = x27cn.obfuscate_html_js(html, level='high')
命令行
# 高级混淆 JavaScript 文件
x27cn protect app.js # 生成 app.protected.js
x27cn protect app.js dist/app.min.js # 指定输出
# 选择混淆级别
x27cn protect worker.js --level=maximum # 最高级别
x27cn protect worker.js --level=medium # 中等级别
# 混淆 HTML 中的 JS
x27cn protect index.html --level=high
混淆效果对比
原始代码:
// VLESS WebSocket 服务
const UUID = "02649992-ba8d-4f87";
if (p && "websocket" === p) {
return handleWebSocket(request, UUID);
}
混淆后:
const _0x9534e9="\x30\x32"+"\x36\x34\x39"+"\x39\x39\x32";
if(_0x7af6f2&&String.fromCharCode(119,101,98,115,111,99,107,101,116)===_0x7af6f2){
return _0x19eba2(_0xbdc3e6,_0x9534e9);}
算法说明
X27CN v2 使用以下加密步骤:
- 密钥扩展 - 将密钥扩展为 256 字节
- S-Box 替换 - 非线性字节替换
- 位旋转 - 循环左移 5 位
- 状态混合 - 使用累积状态值混淆
安全说明
X27CN 设计用于代码混淆,不是密码学安全的加密算法。
适用场景:
- 前端代码混淆保护
- API 响应混淆
- 配置文件保护
- 防止代码被轻易复制
不适用场景:
- 密码存储(请使用 bcrypt/argon2)
- 敏感数据加密(请使用 AES-256)
- 通信加密(请使用 TLS)
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file x27cn-1.2.0.tar.gz.
File metadata
- Download URL: x27cn-1.2.0.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c534cbfc6fdda70b15cef027a9613ac8d5bbfcf71201f45f77b0461fbb57894
|
|
| MD5 |
432bf9a9c732597cc70df004c2afd242
|
|
| BLAKE2b-256 |
173072f68623f32554bf93b101986303f997cce2aa89affef4bcf136a0a60621
|
File details
Details for the file x27cn-1.2.0-py3-none-any.whl.
File metadata
- Download URL: x27cn-1.2.0-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16b30534ae076a4684a6e8592eba3ba64ed1f6a51b43a7b99bd723df2bb1244c
|
|
| MD5 |
2c424db3b5652b6acf2430cdb5431a5b
|
|
| BLAKE2b-256 |
b3d716ae26147ac9cb5589e1ccb1328e270ceb1ea465db8999d2371076a5da2f
|