python3 php7,aes-256-gcm_python3_php7

该博客详细介绍了如何在 Python3 和 PHP7 中使用 AES-256-GCM 算法进行加解密操作。提供了一组示例代码,包括 Python3 的 `cryptography` 库和 PHP7 的 `openssl` 函数,演示了如何生成密钥、初始化向量、加密和解密过程。
摘要由CSDN通过智能技术生成

python3

from cryptography.hazmat.primitives.ciphers.aead import AESGCM

import cryptography.exceptions

import binascii

import base64

import os

def encrypt_aes256gcm(key, ciphertext, aad):

'''

aes-256-gcm 加密

key: 为str,hex字符串,64字符(32字节)

aad: 为str,hex字符串,32字符(16字节)

ciphertext: 为bytes, 明文

返回: base64 的密文

'''

aes_gcm_ivlen = 12

key_bytes = binascii.unhexlify(key)

aad_bytes = binascii.unhexlify(aad)

data = ciphertext

iv_bytes = os.urandom(aes_gcm_ivlen)

aesgcm = AESGCM(key_bytes) # tag_length=16

crypt_bytes = aesgcm.encrypt(iv_bytes, data, aad_bytes)

return base64.b64encode(iv_bytes + crypt_bytes)

def decrypt_aes256gcm(key, ciphertext, aad):

'''

aes-256-gcm 解密

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值