""" ChaosCrypt 库模式示例 —— 基本用法。 运行前请先安装:pip install chaoscrypt 或把 src/ 加入 sys.path 后直接运行本文件。 """ import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) from chaoscrypt import ChaosCrypt, ChaosCryptError KEY_FILE = "example.key" KEY_PASSWORD = "this-is-my-key-file-password" USER_PASSWORD = "this-is-my-encryption-password" def main() -> None: cc = ChaosCrypt() # ---------------------------------------------------------------- 1. 生成密钥 print("=" * 60) print("1. 生成 / 加载密钥文件") print("=" * 60) if not os.path.exists(KEY_FILE): cc.create_key(KEY_PASSWORD, KEY_FILE) print(f"✅ 已生成密钥文件: {KEY_FILE}") else: cc.load(KEY_FILE, KEY_PASSWORD) print(f"✅ 已加载已有密钥文件: {KEY_FILE}") # ---------------------------------------------------------------- 2. 文本加解密 print() print("=" * 60) print("2. 文本加密 / 解密") print("=" * 60) secret = "我的银行卡密码是 123456 —— 开玩笑的 :)" cipher = cc.encrypt(secret, USER_PASSWORD) print(f"明文: {secret}") print(f"密文: {cipher[:80]}…" if len(cipher) > 80 else f"密文: {cipher}") plain = cc.decrypt(cipher, USER_PASSWORD) print(f"还原: {plain}") assert plain == secret # ---------------------------------------------------------------- 3. 二进制 print() print("=" * 60) print("3. 二进制数据加解密(无损往返)") print("=" * 60) raw = os.urandom(1024) ct = cc.encrypt_bytes(raw, USER_PASSWORD) back = cc.decrypt_bytes(ct, USER_PASSWORD) print(f"原始字节: {len(raw)} 字节") print(f"密文长度: {len(ct)} 字符") print(f"往返一致: {back == raw}") assert back == raw # ---------------------------------------------------------------- 4. 文件 print() print("=" * 60) print("4. 文件加解密") print("=" * 60) with open("example_data.txt", "w", encoding="utf-8") as f: f.write("这是一份需要保密的文件内容。\n" * 10) enc_path = cc.encrypt_file("example_data.txt", USER_PASSWORD, "example_data.txt.cc") print(f"✅ 加密: example_data.txt → {enc_path}") dec_path = cc.decrypt_file("example_data.txt.cc", USER_PASSWORD, "example_data.restored.txt") print(f"✅ 解密: {enc_path} → {dec_path}") with open("example_data.restored.txt", encoding="utf-8") as f: restored = f.read() assert restored.startswith("这是一份需要保密的文件内容。") print("✅ 文件内容验证通过") # ---------------------------------------------------------------- 5. 错误处理 print() print("=" * 60) print("5. 统一错误类演示") print("=" * 60) try: cc.decrypt(cipher, "wrong-password") except ChaosCryptError as e: print(f"捕获到错误: {e}") print(f" 错误代码: {e.code}") print(f" 错误信息: {e.info}") try: ChaosCrypt().load("nonexistent.key", KEY_PASSWORD) except ChaosCryptError as e: print(f"捕获到错误: {e}") # ---------------------------------------------------------------- 6. 便捷函数 print() print("=" * 60) print("6. 模块级便捷函数") print("=" * 60) import chaoscrypt ct2 = chaoscrypt.encrypt("quick call", USER_PASSWORD, KEY_FILE, KEY_PASSWORD) pt2 = chaoscrypt.decrypt(ct2, USER_PASSWORD, KEY_FILE, KEY_PASSWORD) print(f"chaoscrypt.encrypt/decrypt → {pt2!r}") # ---------------------------------------------------------------- 清理 print() for f in ("example_data.txt", "example_data.txt.cc", "example_data.restored.txt"): if os.path.exists(f): os.remove(f) print("✅ 示例完成(临时文件已清理,密钥文件保留)") if __name__ == "__main__": main()