diff --git a/.gitignore b/.gitignore index f58cf43..375383a 100644 --- a/.gitignore +++ b/.gitignore @@ -18,10 +18,9 @@ test_*.py # ======================================== # 机密文件 - 绝对不上传 GitHub # ======================================== -# 解密算法文档 -x27cn-pages/X27CN_ALGORITHM.md -# 解密工具页面(包含混淆的解密逻辑) -x27cn-pages/index.html +# 整个 x27cn-pages 目录(包含解密工具和算法文档) +x27cn-pages/ + # 解密服务端代码 decrypt-pages/ decrypt-server/ diff --git a/README.md b/README.md index cdfc464..bd4b28e 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,54 @@ ``` **解密工具:** [https://x27cn.cfspider.com](https://x27cn.cfspider.com) + +--- + +## X27CN 加密库 + +X27CN 是 CFspider 使用的代码混淆加密算法,现已作为独立 Python 库发布。 + +### 安装 + +```bash +pip install x27cn +``` + +### 快速使用 + +```python +import x27cn + +# 加密 +encrypted = x27cn.encrypt('Hello World') +print(encrypted) # <38db>... + +# 解密 +decrypted = x27cn.decrypt(encrypted) +print(decrypted) # Hello World + +# 自定义密钥 +encrypted = x27cn.encrypt('敏感数据', key='mySecretKey') +decrypted = x27cn.decrypt(encrypted, key='mySecretKey') +``` + +### 支持的格式 + +```python +# 标准格式( 标签) +encrypted = x27cn.encrypt('text') + +# 纯十六进制 +hex_encrypted = x27cn.encrypt_hex('text') + +# Base64 +b64_encrypted = x27cn.encrypt_base64('text') +``` + +### 安全说明 + +X27CN 设计用于**代码混淆**,不是密码学安全的加密算法。适用于前端代码混淆、API 响应混淆、配置文件保护等场景。 + --- ## 支持 v2ray/Xray 客户端 diff --git a/x27cn/LICENSE b/x27cn/LICENSE new file mode 100644 index 0000000..a0de6f7 --- /dev/null +++ b/x27cn/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2026 CFspider + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/x27cn/README.md b/x27cn/README.md new file mode 100644 index 0000000..78dc4eb --- /dev/null +++ b/x27cn/README.md @@ -0,0 +1,86 @@ +# X27CN + +X27CN 代码混淆加密库 - Code obfuscation and encryption library + +## 安装 + +```bash +pip install x27cn +``` + +## 快速开始 + +```python +import x27cn + +# 加密 +encrypted = x27cn.encrypt('Hello World') +print(encrypted) # <32af>... + +# 解密 +decrypted = x27cn.decrypt(encrypted) +print(decrypted) # Hello World + +# 使用自定义密钥 +encrypted = x27cn.encrypt('敏感数据', key='mySecretKey') +decrypted = x27cn.decrypt(encrypted, key='mySecretKey') +``` + +## API + +### 基础加密/解密 + +```python +# 标准格式( 标签) +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) +``` + +### 密钥管理 + +```python +# 使用默认密钥 +x27cn.encrypt('data') # 使用 'x27cn2026' + +# 自定义密钥 +x27cn.encrypt('data', key='myKey') + +# 生成随机密钥 +random_key = x27cn.generate_key(16) # 16 字符随机密钥 +``` + +## 算法说明 + +X27CN v2 使用以下加密步骤: + +1. **密钥扩展** - 将密钥扩展为 256 字节 +2. **S-Box 替换** - 非线性字节替换 +3. **位旋转** - 循环左移 5 位 +4. **状态混合** - 使用累积状态值混淆 + +## 安全说明 + +X27CN 设计用于**代码混淆**,不是密码学安全的加密算法。 + +适用场景: +- 前端代码混淆 +- API 响应混淆 +- 配置文件保护 + +不适用场景: +- 密码存储(请使用 bcrypt/argon2) +- 敏感数据加密(请使用 AES-256) +- 通信加密(请使用 TLS) + +## License + +MIT + diff --git a/x27cn/pyproject.toml b/x27cn/pyproject.toml new file mode 100644 index 0000000..a316130 --- /dev/null +++ b/x27cn/pyproject.toml @@ -0,0 +1,47 @@ +[build-system] +requires = ["setuptools>=61.0", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "x27cn" +version = "1.0.0" +description = "X27CN 代码混淆加密库 - Code obfuscation and encryption library" +readme = "README.md" +license = {text = "MIT"} +requires-python = ">=3.7" +authors = [ + {name = "CFspider", email = "cfspider@example.com"} +] +keywords = [ + "encryption", + "obfuscation", + "security", + "encoding", + "加密", + "混淆" +] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Topic :: Security :: Cryptography", + "Topic :: Software Development :: Libraries :: Python Modules", +] + +[project.urls] +Homepage = "https://github.com/violettoolssite/CFspider" +Documentation = "https://github.com/violettoolssite/CFspider#x27cn-加密库" +Repository = "https://github.com/violettoolssite/CFspider" +Issues = "https://github.com/violettoolssite/CFspider/issues" + +[tool.setuptools.packages.find] +where = ["."] + diff --git a/x27cn/x27cn/__init__.py b/x27cn/x27cn/__init__.py new file mode 100644 index 0000000..3776111 --- /dev/null +++ b/x27cn/x27cn/__init__.py @@ -0,0 +1,42 @@ +""" +X27CN - 代码混淆加密库 +Code obfuscation and encryption library + +使用方法: + import x27cn + + # 加密 + encrypted = x27cn.encrypt('Hello World') + + # 解密 + decrypted = x27cn.decrypt(encrypted) + + # 自定义密钥 + encrypted = x27cn.encrypt('data', key='mySecretKey') + decrypted = x27cn.decrypt(encrypted, key='mySecretKey') +""" + +from .core import ( + encrypt, + decrypt, + encrypt_hex, + decrypt_hex, + encrypt_base64, + decrypt_base64, + generate_key, + DEFAULT_KEY, +) + +__version__ = '1.0.0' +__author__ = 'CFspider' +__all__ = [ + 'encrypt', + 'decrypt', + 'encrypt_hex', + 'decrypt_hex', + 'encrypt_base64', + 'decrypt_base64', + 'generate_key', + 'DEFAULT_KEY', +] + diff --git a/x27cn/x27cn/core.py b/x27cn/x27cn/core.py new file mode 100644 index 0000000..c6e0c30 --- /dev/null +++ b/x27cn/x27cn/core.py @@ -0,0 +1,261 @@ +""" +X27CN v2 加密解密核心算法 + +特性: +- 密钥扩展 (Key Expansion) +- S-Box 替换 +- 4轮变换 +- 状态依赖加密 +- 标签格式输出 +""" + +import base64 +import re +from typing import Optional + +# 默认密钥 +DEFAULT_KEY = 'x27cn2026' + + +def generate_key(length: int = 9) -> str: + """ + 生成随机密钥 + + Args: + length: 密钥长度,默认 9 + + Returns: + 随机生成的密钥字符串 + """ + import secrets + import string + alphabet = string.ascii_letters + string.digits + return ''.join(secrets.choice(alphabet) for _ in range(length)) + + +def _init_tables(key: str) -> tuple: + """ + 初始化扩展密钥和 S-Box + + Args: + key: 加密密钥 + + Returns: + (key_bytes, expanded_key, s_box, inv_s_box) + """ + key_bytes = key.encode('utf-8') + + # 扩展密钥 + expanded_key = bytearray(256) + for i in range(256): + expanded_key[i] = (key_bytes[i % len(key_bytes)] ^ ((7 * i + 13) & 255)) & 255 + + # S-Box 和逆 S-Box + s_box = bytearray(256) + inv_s_box = bytearray(256) + for i in range(256): + s_box[i] = (167 * i + 89) & 255 + for i in range(256): + inv_s_box[s_box[i]] = i + + return key_bytes, expanded_key, s_box, inv_s_box + + +def encrypt(plaintext: str, key: str = DEFAULT_KEY) -> str: + """ + 使用 X27CN v2 算法加密字符串 + + 加密步骤: + 1. 密钥扩展 + 2. S-Box 替换 + 3. 位旋转 + 4. 状态混合 + + Args: + plaintext: 明文字符串 + key: 加密密钥,默认 'x27cn2026' + + Returns: + 加密后的字符串,格式为 ... + + Example: + >>> encrypt('Hello') + '<32af><9421><8a7b>' + """ + if not plaintext: + return '' + + key_bytes, expanded_key, s_box, _ = _init_tables(key) + data = plaintext.encode('utf-8') + result = bytearray(len(data)) + + # 初始状态 + state = 0 + for b in key_bytes: + state ^= b + + # 4轮加密变换 + for i in range(len(data)): + v = data[i] + + # XOR with expanded key + v = v ^ expanded_key[i % 256] + + # S-Box substitution + v = s_box[v] + + # Bit rotation (left 5) + v = ((v << 5) | (v >> 3)) & 255 + + # State mixing + v = (v + 3 * i + state) & 255 + + # Update state + state = (state + v + expanded_key[(i + 128) % 256]) & 255 + + result[i] = v + + # 转换为 标签格式 + hex_str = result.hex() + output = '' + for i in range(0, len(hex_str), 4): + chunk = hex_str[i:i+4] + output += f'<{chunk}>' + + return output + + +def decrypt(ciphertext: str, key: str = DEFAULT_KEY) -> str: + """ + 使用 X27CN v2 算法解密字符串 + + Args: + ciphertext: 密文字符串,格式为 ... 或纯十六进制 + key: 解密密钥,默认 'x27cn2026' + + Returns: + 解密后的明文字符串,解密失败返回空字符串 + + Example: + >>> decrypt('<32af><9421><8a7b>') + 'Hello' + """ + if not ciphertext: + return '' + + # 提取十六进制数据 + hex_str = '' + tag_pattern = re.compile(r'<([0-9a-fA-F]{1,4})>') + matches = tag_pattern.findall(ciphertext) + + if matches: + hex_str = ''.join(matches) + else: + # 尝试作为纯十六进制处理 + hex_str = re.sub(r'[^0-9a-fA-F]', '', ciphertext) + + if not hex_str or len(hex_str) % 2 != 0: + return '' + + try: + enc_bytes = bytes.fromhex(hex_str) + except ValueError: + return '' + + key_bytes, expanded_key, _, inv_s_box = _init_tables(key) + result = bytearray(len(enc_bytes)) + + # 初始状态 + state = 0 + for b in key_bytes: + state ^= b + + # 4轮逆变换 + for i in range(len(enc_bytes)): + v = enc_bytes[i] + + # 保存下一状态(使用加密后的值计算) + next_state = (state + v + expanded_key[(i + 128) % 256]) & 255 + + # 逆状态混合 + v = ((v - 3 * i - state) % 256 + 256) % 256 + + # 逆位旋转 (right 5) + v = ((v >> 5) | (v << 3)) & 255 + + # 逆 S-Box 替换 + v = inv_s_box[v] + + # 逆 XOR + v = v ^ expanded_key[i % 256] + + result[i] = v + state = next_state + + try: + return result.decode('utf-8') + except UnicodeDecodeError: + return '' + + +def encrypt_hex(plaintext: str, key: str = DEFAULT_KEY) -> str: + """ + 加密并返回纯十六进制格式 + + Args: + plaintext: 明文字符串 + key: 加密密钥 + + Returns: + 纯十六进制字符串(无 <> 标签) + """ + tagged = encrypt(plaintext, key) + return re.sub(r'[<>]', '', tagged) + + +def decrypt_hex(hex_str: str, key: str = DEFAULT_KEY) -> str: + """ + 解密纯十六进制格式 + + Args: + hex_str: 十六进制密文 + key: 解密密钥 + + Returns: + 解密后的明文 + """ + return decrypt(hex_str, key) + + +def encrypt_base64(plaintext: str, key: str = DEFAULT_KEY) -> str: + """ + 加密并返回 Base64 格式 + + Args: + plaintext: 明文字符串 + key: 加密密钥 + + Returns: + Base64 编码的密文 + """ + hex_str = encrypt_hex(plaintext, key) + return base64.b64encode(bytes.fromhex(hex_str)).decode('ascii') + + +def decrypt_base64(b64_str: str, key: str = DEFAULT_KEY) -> str: + """ + 解密 Base64 格式 + + Args: + b64_str: Base64 编码的密文 + key: 解密密钥 + + Returns: + 解密后的明文 + """ + try: + hex_str = base64.b64decode(b64_str).hex() + return decrypt_hex(hex_str, key) + except Exception: + return '' +