from datetime import datetime
from cryptography.fernet import Fernet


def generate_key():
    """生成一个Fernet密钥"""
    return Fernet.generate_key()


def encrypt_data(key, data):
    """使用给定的密钥加密数据"""
    cipher_suite = Fernet(key)
    encrypted_data = cipher_suite.encrypt(data.encode())
    return encrypted_data


def decrypt_data(key, encrypted_data):
    """使用给定的密钥解密数据"""
    cipher_suite = Fernet(key)
    decrypted_data = cipher_suite.decrypt(encrypted_data)
    return decrypted_data.decode()


def get_timestamp(date_str):
    """获取任意日期时间戳"""
    dt = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
    return int(dt.timestamp())


# 示例使用
if __name__ == "__main__":
    # 生成时间戳
    date_str = "2026-02-10 12:30:45"
    timestamp = get_timestamp(date_str)
    print(timestamp)

    # 生成密钥
    key = generate_key()
    print(f"生成的密钥: {key}")

    # 加密数据
    data = "1770697845"
    encrypted_data = encrypt_data(key, data)
    print(f"加密后的数据: {encrypted_data}")

    # 解密数据
    key = b'Gx5RnQWMxSry43W7nVJsGS-H8kB6Jct_ImaKh3bpa0A='
    encrypted_data = b'gAAAAABnqqn7UZvGZZQCXxpNlcwguR7zFz3KOjQaGRRxzSU430dveA72ZQB0lMkw6JX2ACjaZ7kGMYhdrlNJwZ8mmUTnqQ1fGg=='
    try:
        decrypted_data = decrypt_data(key, encrypted_data)
        print(f"解密后的数据: {decrypted_data}")
    except Exception as e:
        print(f"解密失败: {e}")
