当前位置:   article > 正文

python3 php7,aes-256-gcm_python3_php7

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

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 解密

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/176576
推荐阅读
相关标签
  

闽ICP备14008679号