python数据加密的几种方式

python数据加密的几种方式,第1张

python数据加密的几种方式

python数据加密的几种方式

import base64
from Cryptodome.Cipher import AES
from Cryptodome.Cipher import AES
from Cryptodome import Random
from binascii import a2b_hex


class aescrypt():
    def __init__(self,key,iv,encode_,text):
        self.text = text
        self.encode_ = encode_
        self.key = self.add_16(key)
        self.iv = iv

    def add_16(self,test):
        test = test.encode(self.encode_)
        while len(test) % 16 != 0:
            test += b'x00'
        return test

    def AES_data_ECB(self,text):
        self.model = AES.MODE_ECB
        self.aes = AES.new(self.key, self.model)  # 创建一个aes对象
        text = self.add_16(text)
        self.encrypt_text = self.aes.encrypt(text)
        result = base64.encodebytes(self.encrypt_text).decode().strip()
        print('加密后的数据:{}'.format(result))

        text = base64.decodebytes(result.encode(self.encode_))
        self.decrypt_text = self.aes.decrypt(text)
        data = self.decrypt_text.decode(self.encode_).strip('')
        print('解密后的数据:{}'.format(data))


    def AES_data_CFB(self):
        iv = Random.new().read(AES.block_size)
        mycipher = AES.new(self.key, AES.MODE_CFB, iv)  # 使用 key 和iv 初始化AES 对象, 使用MODE_CFB模式
        ciptext = iv + mycipher.encrypt(text.encode())
        result = base64.b64encode((ciptext)).decode("utf-8")
        print('加密后的数据为:{}'.format(result))

        ciptext = base64.b64decode(result)
        mydecrypt = AES.new(self.key, AES.MODE_CFB, ciptext[:16])
        data = mydecrypt.decrypt(ciptext[16:]).decode()
        print('解密后的数据为:{}'.format(data))

    def AES_data_CBC(self):
        ptor = AES.new(self.key, AES.MODE_CBC, self.iv)
        sdata = self.text.encode('utf-8')
        text = sdata + (16 - len(sdata) % 16) * bytes('', 'gbk')
        encrypted_text = base64.b64encode(ptor.encrypt(text))
        result = str(encrypted_text, encoding="utf8").replace(''', '\'')
        print('加密数据为:{}'.format(result))

        ptor = AES.new(self.key, AES.MODE_CBC, self.iv)
        decrypted_text = ptor.decrypt(base64.b64decode(result))
        data = str(decrypted_text.rstrip(bytes('', 'utf-8')), 'utf-8')
        print('解密后的数据为{}'.format(data))


if __name__ == '__main__':
    text = '{"contiguous_size":0,"graph_id":0,"hash_id":"6007115827502439600","mem_offset":2048,"node_size":10,"ref_node_size":0,"stream_group_size":0,"stream_size":1,"tensor_size":10,"tensors":[{"life_end":1,"life_start":0,"lifelong_value":0,"offset":0,"ori_size":64,"size":512,"tensor_id":0},{"life_end":2,"life_start":1,"lifelong_value":0,"offset":512,"ori_size":64,"size":512,"tensor_id":1},{"life_end":4,"life_start":2,"lifelong_value":0,"offset":0,"ori_size":64,"size":512,"tensor_id":2},{"life_end":6,"life_start":3,"lifelong_value":0,"offset":512,"ori_size":64,"size":512,"tensor_id":3},{"life_end":7,"life_start":4,"lifelong_value":0,"offset":1024,"ori_size":64,"size":512,"tensor_id":4},{"life_end":9,"life_start":5,"lifelong_value":0,"offset":0,"ori_size":4,"size":512,"tensor_id":5},{"life_end":8,"life_start":6,"lifelong_value":0,"offset":1536,"ori_size":4,"size":512,"tensor_id":6},{"life_end":7,"life_start":7,"lifelong_value":0,"offset":512,"ori_size":4,"size":512,"tensor_id":7},{"life_end":8,"life_start":8,"lifelong_value":0,"offset":512,"ori_size":4,"size":512,"tensor_id":8},{"life_end":9,"life_start":9,"lifelong_value":0,"offset":512,"ori_size":4,"size":512,"tensor_id":9}]}'
    iv = Random.new().read(AES.block_size)
    key = 'tq'   #密钥
    test = aescrypt(key, iv, 'gbk',text)
    test.AES_data_CFB()           # 使用AES_CFB加密方式
    test.AES_data_CBC()           # 使用AES_CBC加密方式
    test.AES_data_ECB(text)       # 使用AES_ECB加密方式

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5594773.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存