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))
相关推荐
快乐小土豆~~1 分钟前
上传视频时截取正脸照片
前端·音视频·vladmandic
财经资讯数据_灵砚智能2 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月12日
人工智能·python·信息可视化·自然语言处理·ai编程
light blue bird7 分钟前
主从执行端动机模块工序协同组件
jvm·数据库·.net·桌面端
SPC的存折9 分钟前
(自用)LNMP-Redis-Discuz5.0部署指南-openEuler24.03-测试环境
linux·运维·服务器·数据库·redis·缓存
二等饼干~za89866810 分钟前
云罗 GEO 优化系统源码厂家测评报告
大数据·网络·数据库·人工智能·django
276695829211 分钟前
token1005 算法分析
java·前端·javascript·token·token1005·携程酒店·token算法分析
乆夨(jiuze)12 分钟前
记录一个css,实现下划线内容显示,支持文本多行显示
前端·css
堕落年代13 分钟前
Spring 事务提交顺序深度解析:从踩坑到理解原理
数据库·spring·oracle
GISer_Jing14 分钟前
前端视频多模态:编解码、传输、渲染全链路详解
前端·人工智能·音视频
测试秃头怪21 分钟前
python&selenium自动化测试实战项目详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例