PC微信协议之AES-192-GCM算法

复制代码
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import constant_time
from cryptography.hazmat.backends import default_backend
import os
import binascii



# ===============================
# AES-192-GCM 加密函数
# ===============================
def aes_192_gcm_encrypt(key: bytes, plaintext: bytes, associated_data: bytes = b""):
    """
    使用 AES-192-GCM 进行加密
    :param key: 24 字节密钥
    :param plaintext: 明文数据
    :param associated_data: 附加认证数据(可选)
    :return: (nonce, ciphertext, tag)
    """
    if len(key) != 24:
        raise ValueError("AES-192 需要 24 字节密钥")

    # 生成 12 字节随机 nonce(GCM 推荐长度)
    nonce = os.urandom(12)

    encryptor = Cipher(
        algorithms.AES(key),
        modes.GCM(nonce),
        backend=default_backend()
    ).encryptor()

    # 添加 AAD(可选)
    encryptor.authenticate_additional_data(associated_data)

    # 执行加密
    ciphertext = encryptor.update(plaintext) + encryptor.finalize()

    # 返回 nonce、密文、tag
    return nonce, ciphertext, encryptor.tag


# ===============================
# AES-192-GCM 解密函数
# ===============================
def aes_192_gcm_decrypt(key: bytes, nonce: bytes, tag: bytes, ciphertext: bytes, associated_data: bytes = b""):
    """
    使用 AES-192-GCM 进行解密
    :param key: 24 字节密钥
    :param nonce: 加密时的随机 nonce
    :param tag: GCM 认证标签
    :param ciphertext: 密文数据
    :param associated_data: 附加认证数据(可选)
    :return: plaintext
    """
    decryptor = Cipher(
        algorithms.AES(key),
        modes.GCM(nonce, tag),
        backend=default_backend()
    ).decryptor()

    decryptor.authenticate_additional_data(associated_data)

    # 执行解密
    plaintext = decryptor.update(ciphertext) + decryptor.finalize()
    return plaintext


# ===============================
# 示例测试
# ===============================
if __name__ == "__main__":

    key_hex = "35FE9BD1DFF7FD1939382935F4AEEF62E2C7895D8F441305"
    key = bytes.fromhex(key_hex)

    plaintext = b"Hello, AES-192-GCM Encryption!"
    aad = b"optional_authenticated_data"

    print("原文:", plaintext)

    # 加密
    nonce, ciphertext, tag = aes_192_gcm_encrypt(key, plaintext, aad)
    print("Nonce:", nonce.hex())
    print("Ciphertext:", ciphertext.hex())
    print("Tag:", tag.hex())

    # 解密
    decrypted = aes_192_gcm_decrypt(key, nonce, tag, ciphertext, aad)
    print("解密结果:", decrypted)

    # 校验
    print("是否一致:", constant_time.bytes_eq(plaintext, decrypted))
相关推荐
一水鉴天几秒前
整体设计 定稿 之24 dashboard.html 增加三层次动态记录体系仪表盘 之2 程序 (Q208 之1)
前端·html
_杨瀚博8 分钟前
微信支付集成_JSAPI
前端
polaris_tl8 分钟前
react beginwork
前端
Boxsc_midnight10 分钟前
【规范驱动的开发方式】之【spec-kit】 的安装入门指南
人工智能·python·深度学习·软件工程·设计规范
玩转数据库管理工具FOR DBLENS13 分钟前
DBLens:开启数据库管理新纪元——永久免费,智能高效的国产化开发利器
数据结构·数据库·测试工具·数据库开发
条件漫步15 分钟前
Miniconda config channels的查看、删除、添加
python
爱笑的眼睛1118 分钟前
深入解析PyTorch nn模块:超越基础模型构建的高级技巧与实践
java·人工智能·python·ai
亮子AI18 分钟前
【css】列表的标号怎么实现居中对齐?
前端·css
芝麻馅汤圆儿19 分钟前
sockperf 工具
linux·服务器·数据库
IndulgeCui19 分钟前
金仓数据库征文_使用KDTS迁移mysql至金仓数据库问题处理记录分享
数据库