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))
相关推荐
mr_LuoWei20094 分钟前
python工具:python代码知识库笔记
数据库·python
weixin_395448914 分钟前
cursor日志
人工智能·python·机器学习
_果果然6 分钟前
这 7 个免费 Lottie 动画网站,帮你省下一个设计师的工资
前端
QT.qtqtqtqtqt8 分钟前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
这周也會开心14 分钟前
Redis数据类型的底层实现和数据持久化
数据库·redis·缓存
ん贤15 分钟前
一次批量删除引发的死锁,最终我选择不加锁
数据库·安全·go·死锁
天天爱吃肉821822 分钟前
【跨界封神|周杰伦×王传福(陶晶莹主持):音乐创作与新能源NVH测试,底层逻辑竟完全同源!(新人必看入行指南)】
python·嵌入式硬件·算法·汽车
数据知道25 分钟前
PostgreSQL 核心原理:系统内部的对象寻址机制(OID 对象标识符)
数据库·postgresql
岱宗夫up35 分钟前
Python 数据分析入门
开发语言·python·数据分析
Aliex_git36 分钟前
跨域请求笔记
前端·网络·笔记·学习