crypto.py 821 B

1234567891011121314151617181920
  1. """AES-256-GCM utilities for QQBot scan-to-configure credential decryption."""
  2. from __future__ import annotations
  3. import base64
  4. import os
  5. def generate_bind_key() -> str:
  6. """Random 256-bit AES key (base64) passed to ``create_bind_task`` so the server
  7. encrypts the bot's *client_secret*; only this CLI holds the key."""
  8. return base64.b64encode(os.urandom(32)).decode()
  9. def decrypt_secret(encrypted_base64: str, key_base64: str) -> str:
  10. """Decrypt ``bot_encrypt_secret`` (base64 of ``IV(12) ‖ ciphertext ‖ tag(16)``) to a UTF-8 string."""
  11. from cryptography.hazmat.primitives.ciphers.aead import AESGCM
  12. raw = base64.b64decode(encrypted_base64) # AESGCM expects ciphertext + tag concatenated
  13. return AESGCM(base64.b64decode(key_base64)).decrypt(raw[:12], raw[12:], None).decode("utf-8")