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))
相关推荐
起名时在学Aiifox8 分钟前
前端文件下载功能深度解析:从基础实现到企业级方案
前端·vue.js·typescript
wanglei20070823 分钟前
生产者消费者
开发语言·python
ChineHe24 分钟前
Redis数据类型篇002_详解Strings核心命令与存储结构
数据库·redis·缓存
清水白石00844 分钟前
《从零到进阶:Pydantic v1 与 v2 的核心差异与零成本校验实现原理》
数据库·python
昵称已被吞噬~‘(*@﹏@*)’~1 小时前
【RL+空战】学习记录03:基于JSBSim构造简易空空导弹模型,并结合python接口调用测试
开发语言·人工智能·python·学习·深度强化学习·jsbsim·空战
电商API&Tina1 小时前
京东 API 数据采集接口接入与行业分析
运维·服务器·网络·数据库·django·php
2501_941877981 小时前
从配置热更新到运行时自适应的互联网工程语法演进与多语言实践随笔分享
开发语言·前端·python
云上凯歌1 小时前
01 ruoyi-vue-pro框架架构剖析
前端·vue.js·架构
酩酊仙人1 小时前
fastmcp构建mcp server和client
python·ai·mcp
柠檬叶子C1 小时前
PostgreSQL 忘记 postgres 密码怎么办?(已解决)
数据库·postgresql