File size: 1,264 Bytes
691fc0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
import base64
import hashlib
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

def derive_key(key_str: str) -> bytes:
    """Derive 32-byte key from a passphrase using SHA-256."""
    if not isinstance(key_str, str) or not key_str:
        raise ValueError("key_str must be a non-empty string")
    return hashlib.sha256(key_str.encode("utf-8")).digest()

def encrypt_text(plaintext: str, key: bytes, iv: bytes) -> dict:
    """Encrypt plaintext with AES-256-GCM. Returns dict with iv, ciphertext, tag (base64)."""
    if plaintext is None:
        plaintext = ""
    aes = AESGCM(key)
    ct = aes.encrypt(iv, plaintext.encode("utf-8"), None)
    ciphertext, tag = ct[:-16], ct[-16:]
    return {
        "iv": base64.b64encode(iv).decode("ascii"),
        "ciphertext": base64.b64encode(ciphertext).decode("ascii"),
        "tag": base64.b64encode(tag).decode("ascii"),
    }

def decrypt_text(enc: dict, key: bytes) -> str:
    """Decrypt dict with iv/ciphertext/tag (base64)."""
    iv = base64.b64decode(enc["iv"])
    ciphertext = base64.b64decode(enc["ciphertext"])
    tag = base64.b64decode(enc["tag"])
    aes = AESGCM(key)
    plaintext = aes.decrypt(iv, ciphertext + tag, None)
    return plaintext.decode("utf-8")