You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现TLS 1.2 AES-256-GCM解密失败,请求排查问题

TLS 1.2 AES-256-GCM解密:C转Python后无法得到预期明文

我正在将一段基于OpenSSL实现的TLS 1.2 AES-256-GCM解密C代码转换为Python代码,但始终无法得到预期明文,请求帮忙排查问题。

C代码实现

ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
EVP_CIPHER_CTX_init(ctx);
c = EVP_get_cipherbyname(SN_aes_256_gcm);

ret = EVP_CipherInit_ex(ctx, c, NULL, key, NULL, (which & SSL3_CC_WRITE));
if(ret == -1) return 1;
ret = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IV_FIXED, 4, iv);
if(ret == -1) return 1;
dump_str("ctx iv", ctx->iv, 12);
memcpy(buf, &seq, sizeof(long));
buf[8] = type;
buf[9] = (unsigned char)(version >> 8);
buf[10] = (unsigned char)(version);
buf[11] = len >> 8;
buf[12] = len & 0xff;
dump_str("buf", buf, 13);
ret = EVP_CIPHER_CTX_ctrl(ctx, 22, 13, buf);
//printf("EVP_CIPHER_CTX_ctrl %d\n", ret);
if(ret == -1) return 1;
dump_str("enc", enc, len);
ret = EVP_Cipher(ctx, enc, enc, len);
//printf("EVP_Cipher %d\n", ret);
if(ret == -1) return 1;
dump_str("nors", enc, len);
dump_str("nor", enc + 8, len - 8);

相关参数

密文(enc): 742d3775849dbe54cd35f70c42df452185ad27c819e1ee9106802f74f4d4abd5905ffd047dfd19a8
密钥(key): 5FC67C85CA6AA4E79F034F277EA9F710F915BAE314A7E04D1DE1F326D25F3CD8
原始IV(iv): 5251251ED83828792E7684A75CA78403
处理后IV(ctx iv): 5251251E0000000000000000
附加数据(buf): 00000000000000001603030028
预期明文: 1400000C21F3D8A6DEE21AE9B96A563806802F74F4D4ABD5905FFD047DFD19A8

当前Python代码

from Crypto.Cipher import AES
key = bytes.fromhex('5FC67C85CA6AA4E79F034F277EA9F710F915BAE314A7E04D1DE1F326D25F3CD8')
data = bytes.fromhex('742d3775849dbe54cd35f70c42df452185ad27c819e1ee9106802f74f4d4abd5905ffd047dfd19a8')
iv = bytes.fromhex('5251251E0000000000000000')
additional_data = bytes.fromhex('000000000000001603030028')
cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
cipher.update(additional_data)
plaintext = cipher.decrypt(data)
print(plaintext[8:].hex())

我尝试了多种Python AES解密方法,均未得到预期结果,恳请帮忙找出问题所在。

内容的提问来源于stack exchange,提问作者Mien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 06:07:09