---
title: "python aes加密"
author: "scholltop"
date: "2020-09-04T15:37:40Z"
tags: []
categories: ["编程语言"]
slug: "python-aes"
draft: false
lastmod: "2020-09-04T15:38:49Z"
id: 2516612
url: "https://schooltop.iteye.com/blog/2516612"
---

```python

import hashlib
from Crypto.Cipher import AES
import base64

class prpcrypt():
    def __init__(self,key):
        self.key = key  # 因为在python3中AES传入参数的参数类型存在问题，需要更换为 bytearray , 所以使用encode编码格式将其转为字节格式（linux系统可不用指定编码）
        IV = 16 * '\x00'
        self.iv=IV.encode("utf-8")
        self.mode = AES.MODE_CBC
        self.BS = AES.block_size
        self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)
        self.unpad = lambda s: s[0:-ord(s[-1])]

    # 加密
    def encrypt(self, text):
        text = self.pad(text).encode("utf-8")
        cryptor = AES.new(self.key, self.mode, self.iv)
        # 目前AES-128 足够目前使用(CBC加密)
        ciphertext = cryptor.encrypt(text)
        # base64加密
        return base64.b64encode(bytes(ciphertext))

    # 解密
    def decrypt(self, text):
        # base64解密
        text = base64.b64decode(text)
        cryptor = AES.new(self.key, self.mode, self.iv)
        # CBC解密
        plain_text = cryptor.decrypt(text)
        # 去掉补足的空格用strip() 去掉
        return self.unpad(bytes.decode(plain_text).rstrip('\0'))    # 解密字节？？？


def gen_binsha(data):
    shavalue = hashlib.sha256()
    shavalue.update(data)
    return shavalue.digest()

if __name__ == '__main__':
    key='78f40cecf89'
    key=gen_binsha(key.encode('utf-8'))
    pc = prpcrypt(key=key)  # 初始化密钥 和 iv

    text='qwerqwerkkk12345'
    e = pc.encrypt(text)  # 加密
    d = pc.decrypt(e)  # 解密
    print("加密:%s" % e)
    print("解密:%s"% d)
    print("长度:%s"% len(d))
```