Files
dvs dd49043171 v4.2.6: Self-describing format, byte pipeline, and a performance pass
Every key file and ciphertext now carries a header that states how it was
produced: magic, two fixed markers, and a complete JSON configuration record.
A reader takes its parameters from the artefact rather than from the caller.

This is an incompatible format change. Headerless input is rejected with E017;
pre-4.2.6 material is read through a compatibility API that wraps it in a
minimal header and then uses the ordinary path, so the strict path stays strict.

Added
- Self-describing header on all artefacts, with a normative specification in
  docs/FORMAT.md
- E017 MissingMagicHeader and E018 MalformedHeader
- Byte pipeline for turbo and raw: the XOR layers exchange raw bytes rather
  than hexadecimal text, cutting expansion from 5.33x to 2.00x and raising
  throughput about 4x. The old behaviour is available as mid="hex".
- Selectable output encoding for turbo and raw: hex (2.00x) or base64 (1.33x)
- Legacy compatibility API: wrap_legacy, decrypt_legacy, decrypt_legacy_auto,
  load_legacy_keys
- Frozen reference vectors covering every mode, encoding and intermediate
- docs/benchmark.md with measured throughput and the profiling behind it

Changed
- Default mode is fast: byte-for-byte compatible with the original algorithm,
  roughly 30x quicker
- Substitution, case flipping and the printable fold use bytes.translate over
  precomputed tables
- XOR layers use one big-integer operation instead of a per-byte loop
- Keystream derivation caches length-independent base material; expansion uses
  cached translation tables and list accumulation rather than str +=
- _get_dynamic_key repeats by multiplication. The loop version was quadratic:
  covering a 5.6 MB intermediate took 866 ms, now 3 ms, output unchanged.
- _derive_final_key decodes only the prefix the output consumes
- The decrypt path no longer copies or scans the whole artefact: a redundant
  strip(), a full-string int(payload, 16), and three whole-string slices in
  strip_header were removed. Decryption at 512 KiB went from 34 to 87 MB/s.
- Documentation rewritten for 4.2.6; all eleven markdown files are English

Fixed
1. Key derivation is no longer quadratic (_get_dynamic_key and
   _expand_keystream_slow shared the str += pattern)
2. Two hex-intermediate pipelines were asymmetric, so a turbo or raw artefact
   written with mid="hex" could not be read back
3. change_key_password and export_key_text wrote artefacts without a header
4. The constructor accepted encoding="base64" while the header records
   out="base"; both spellings are now accepted
5. A payload that is not valid hexadecimal reports E010 (MalformedCiphertext)
   rather than E011 (DecryptionFailed)
6. The TUI encoding menu referenced a missing translation

Security
- Format break from pre-4.2.6 artefacts, documented rather than accidental
- fast is not a weaker mode: the frozen vectors assert identical output
- The header adds no secrecy and is not authenticated; an application that must
  detect header tampering should MAC the whole artefact

Performance
- raw with the default bt intermediate: ~60 MB/s encryption, ~70 MB/s
  decryption, ciphertext 2.00x the payload
- Throughput holds steady from 64 KiB to 1 GiB
- The original implementation managed 0.08 MB/s
2026-09-27 22:06:34 +08:00

280 lines
12 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
ChaosCrypt bilingual UI strings.
The interactive TUI reads all of its labels from here. The library core and
the CLI do not depend on this module, so importing ``chaoscrypt`` carries no
translation overhead.
English is the default language; Chinese is available as an alternative.
"""
from __future__ import annotations
from typing import Dict
__all__ = ["TEXTS", "Lang", "t", "available_langs", "LANG_NAMES", "DEFAULT_LANG"]
#: Display names for the supported languages.
LANG_NAMES = {
"en": "English",
"zh": "中文",
}
#: Language used unless the caller asks for something else.
DEFAULT_LANG = "en"
class Lang:
"""Language identifiers."""
EN = "en"
ZH = "zh"
TEXTS: Dict[str, Dict[str, str]] = {
# ------------------------------------------------------------- general
"app_title": {
"en": "ChaosCrypt - Chaos Obfuscation Cipher",
"zh": "ChaosCrypt · 混沌混淆加密",
},
"warn_banner": {
"en": (
"ChaosCrypt is an unaudited, non-standardised cipher. It is intended for\n"
"obfuscation, personal use and study -- not for production systems.\n"
"See SECURITY.md for the design and threat analysis."
),
"zh": (
"ChaosCrypt 未经审计,亦非标准化算法,适用于混淆、个人自用与研究学习,\n"
"不适用于生产系统。设计与威胁分析详见 SECURITY.md。"
),
},
"press_enter": {"en": "Press Enter to continue...", "zh": "按回车继续…"},
"bye": {
"en": "Session ended. Goodbye.",
"zh": "会话结束,再见。",
},
"cancelled": {"en": "Cancelled.", "zh": "已取消。"},
"success": {"en": "OK", "zh": "成功"},
"failed": {"en": "Failed", "zh": "失败"},
"yes": {"en": "Yes", "zh": "是"},
"no": {"en": "No", "zh": "否"},
"none": {"en": "(none)", "zh": "(无)"},
# --------------------------------------------------------------- menu
"menu_choose": {"en": "Select an action", "zh": "请选择操作"},
"menu_encrypt": {"en": "Encrypt text", "zh": "加密文本"},
"menu_decrypt": {"en": "Decrypt text", "zh": "解密文本"},
"menu_encrypt_file": {"en": "Encrypt file", "zh": "加密文件"},
"menu_decrypt_file": {"en": "Decrypt file", "zh": "解密文件"},
"menu_keygen": {"en": "Generate a new key file", "zh": "生成新密钥文件"},
"menu_loadkey": {"en": "Load a key file", "zh": "加载密钥文件"},
"menu_keyinfo": {"en": "Show key information", "zh": "查看当前密钥信息"},
"menu_chpwd": {"en": "Change key file password", "zh": "修改密钥文件密码"},
"menu_lang": {"en": "Switch language", "zh": "切换语言"},
"menu_mode": {"en": "Performance mode", "zh": "性能模式"},
"menu_encoding": {"en": "Output encoding", "zh": "输出编码"},
"info_encoding": {"en": "Output encoding", "zh": "输出编码"},
"encoding_prompt": {"en": "Select output encoding", "zh": "请选择输出编码"},
"encoding_hex_desc": {
"en": "hex - 2 characters per byte, 2.00x expansion",
"zh": "hex - 每字节 2 字符,膨胀 2.00 倍",
},
"encoding_base64_desc": {
"en": "base64 - 4 characters per 3 bytes, 1.33x expansion",
"zh": "base64 - 每 3 字节 4 字符,膨胀 1.33 倍",
},
"encoding_changed": {"en": "Encoding set to", "zh": "输出编码已设为"},
"encoding_note": {
"en": "(only affects turbo and raw; reference and fast always emit hex)",
"zh": "(仅影响 turbo 与 raw;reference 与 fast 恒为 hex)",
},
"info_mode": {"en": "Performance mode", "zh": "性能模式"},
"mode_prompt": {
"en": "Select performance mode",
"zh": "请选择性能模式",
},
"mode_reference_desc": {
"en": "reference - original algorithm, slowest, ciphertext compatible",
"zh": "reference - 原版算法,最慢,密文兼容",
},
"mode_fast_desc": {
"en": "fast - identical ciphertext, C-level bulk operations",
"zh": "fast - 密文相同,C 级批量运算",
},
"mode_turbo_desc": {
"en": "turbo - SHAKE256 keystream, much faster, own ciphertext format",
"zh": "turbo - SHAKE256 密钥流,极快,独立密文格式",
},
"mode_raw_desc": {
"en": "raw - bytes throughout, fastest, smallest output, weakest",
"zh": "raw - 全程字节,最快,输出最小,强度最低",
},
"mode_changed": {"en": "Mode set to", "zh": "模式已设为"},
"menu_about": {"en": "About / security notice", "zh": "关于 / 安全声明"},
"menu_quit": {"en": "Quit", "zh": "退出"},
"menu_prompt": {"en": "Enter option", "zh": "请输入选项编号"},
"menu_invalid": {
"en": "Unrecognised option. Please try again.",
"zh": "无效选项,请重新输入。",
},
# -------------------------------------------------------------- input
"prompt_key_file": {"en": "Key file path", "zh": "密钥文件路径"},
"prompt_key_password": {"en": "Key file password", "zh": "密钥文件密码"},
"prompt_key_password_new": {
"en": "New key file password",
"zh": "设置密钥文件密码",
},
"prompt_key_password_confirm": {
"en": "Confirm password",
"zh": "再次输入密码确认",
},
"prompt_key_password_old": {
"en": "Current key file password",
"zh": "原密钥文件密码",
},
"prompt_user_password": {
"en": "Encryption password",
"zh": "加密密码",
},
"prompt_plaintext": {"en": "Plaintext", "zh": "请输入明文"},
"prompt_ciphertext": {"en": "Ciphertext", "zh": "请输入密文"},
"prompt_src_file": {"en": "Source file", "zh": "源文件路径"},
"prompt_dst_file": {
"en": "Output path (blank uses the default)",
"zh": "输出路径(留空使用默认)",
},
"prompt_overwrite": {
"en": "That key file already exists. Overwrite it? (y/n)",
"zh": "密钥文件已存在,是否覆盖?(y/n)",
},
"prompt_create_if_missing": {
"en": "No key file found. Create one now? (y/n)",
"zh": "密钥文件不存在,是否现在生成?(y/n)",
},
# ------------------------------------------------------------ results
"result_ciphertext": {"en": "Ciphertext", "zh": "密文"},
"result_plaintext": {"en": "Plaintext", "zh": "明文"},
"key_saved": {"en": "Key file written to", "zh": "密钥已保存到"},
"key_loaded": {"en": "Key file loaded", "zh": "密钥已加载"},
"key_password_changed": {
"en": "Key file password updated",
"zh": "密钥文件密码已修改",
},
"file_written": {"en": "Written to", "zh": "已写入文件"},
"no_key_loaded": {
"en": "No key loaded yet.",
"zh": "当前未加载密钥。",
},
# --------------------------------------------------------------- info
"info_key_file": {"en": "Key file", "zh": "密钥文件"},
"info_version": {"en": "Format version", "zh": "格式版本"},
"info_upper": {"en": "Uppercase map", "zh": "大写映射表"},
"info_lower": {"en": "Lowercase map", "zh": "小写映射表"},
"info_digit": {"en": "Digit map", "zh": "数字映射表"},
"info_equal": {"en": "Padding map", "zh": "等号映射表"},
"info_flip": {"en": "Flip pattern", "zh": "翻转模式"},
"info_long_key": {"en": "Long key length", "zh": "长密钥长度"},
"info_short_key": {"en": "Short key length", "zh": "短密钥长度"},
"info_loaded": {"en": "State", "zh": "密钥状态"},
"characters": {"en": "chars", "zh": "字符"},
# -------------------------------------------------------------- about
"about_body": {
"en": (
"ChaosCrypt - Chaos Obfuscation Cipher\n"
"=====================================\n"
"\n"
"Scope\n"
" ChaosCrypt is an unaudited, non-standardised construction. It is\n"
" built for obfuscation, personal use and study. Production systems,\n"
" regulated data and long-lived secrets belong on an audited,\n"
" standardised cipher instead.\n"
"\n"
"Design\n"
" The construction deliberately trades algebraic structure for\n"
" randomness. Each layer is chosen to remove the kind of structure\n"
" that classical cryptanalysis exploits:\n"
"\n"
" * Random bijective substitution over both letter cases, derived\n"
" from a key space of 26! x 26!.\n"
" * A conditional case-flip stage, adding a non-linear step that\n"
" has no linear approximation.\n"
" * Whole-message reversal, destroying local correlation.\n"
" * XOR against a 4096-nibble long key.\n"
" * XOR against a keystream derived from the encryption password\n"
" mixed with a 512-nibble short key.\n"
"\n"
" Combined key space is on the order of 10^5621. Differential,\n"
" linear and algebraic cryptanalysis have no effective path because\n"
" the substitutions are table lookups rather than algebraic\n"
" operations. There is no number-theoretic structure, so Shor's\n"
" algorithm does not apply; Grover's algorithm leaves a search space\n"
" far beyond physically realisable limits.\n"
"\n"
" Known- and chosen-plaintext attacks cannot recover the full key,\n"
" nor derive the keystream of any other session.\n"
"\n"
" See SECURITY.md for the complete derivation and analysis.\n"
"\n"
"Licence\n"
" MIT. Provided as-is, without warranty of any kind.\n"
),
"zh": (
"ChaosCrypt · 混沌混淆加密\n"
"=========================\n"
"\n"
"适用范围\n"
" ChaosCrypt 未经审计,亦非标准化算法,适用于混淆、个人自用与研究学习。\n"
" 生产系统、受监管数据与长期密钥请改用经过审计的标准化算法。\n"
"\n"
"设计\n"
" 本算法有意以随机性替代代数结构,每一层都用于消除经典密码分析所\n"
" 依赖的结构特征:\n"
"\n"
" * 大小写两套随机双射替换,密钥空间为 26! × 26!。\n"
" * 条件式大小写翻转,引入无线性逼近的非线性步骤。\n"
" * 全文倒序,破坏局部相关性。\n"
" * 与 4096 位十六进制长密钥异或。\n"
" * 与「加密密码 + 512 位短密钥」派生的密钥流异或。\n"
"\n"
" 复合密钥空间量级约为 10^5621。由于替换是查表而非代数运算,\n"
" 差分、线性与代数密码分析均无有效路径。算法不含数论结构,\n"
" Shor 算法不适用;Grover 算法加速后剩余搜索空间仍远超物理可实现范围。\n"
"\n"
" 已知明文与选择明文攻击无法恢复完整密钥,也无法派生其他会话的密钥流。\n"
"\n"
" 完整推导与分析见 SECURITY.md。\n"
"\n"
"许可\n"
" MIT。按原样提供,不提供任何担保。\n"
),
},
# ------------------------------------------------------------- errors
"err_prefix": {"en": "Error", "zh": "错误"},
"err_hint_password": {
"en": "Wrong encryption password, mismatched key file, or altered ciphertext.",
"zh": "加密密码错误、密钥文件不匹配,或密文被篡改。",
},
"err_hint_key_required": {
"en": "Generate or load a key file first.",
"zh": "请先加载或生成密钥文件。",
},
}
def t(key: str, lang: str = DEFAULT_LANG) -> str:
"""Look up a UI string, falling back to English and then to ``key``."""
entry = TEXTS.get(key)
if not entry:
return key
return entry.get(lang) or entry.get(DEFAULT_LANG) or key
def available_langs() -> list:
"""Return the supported language codes."""
return list(LANG_NAMES.keys())