菜鸟

Python AES_ECB_PKCS5加密代码

2018-01-11  本文已影响619人  慢慢慢慢热
#!/usr/bin/env python
# coding=utf-8

# Created by slowchen on 2018/1/10 13:48.

import base64
import re

from Crypto.Cipher import AES


class AESECB:
    def __init__(self, key):
        self.key = key
        self.mode = AES.MODE_ECB
        self.bs = 16  # block size
        self.PADDING = lambda s: s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
    
    def encrypt(self, text):
        generator = AES.new(self.key, self.mode)  # ECB模式无需向量iv
        try:
            crypt = generator.encrypt(self.PADDING(text))
            crypted_str = base64.b64encode(crypt)
            result = crypted_str.decode()
        except Exception:
            result = '加密失败,请确认输入是否有误!'
        return result
    
    def decrypt(self, text):
        generator = AES.new(self.key, self.mode)  # ECB模式无需向量iv
        text += (len(text) % 4) * '='
        try:
            decrpyt_bytes = base64.b64decode(text)
            meg = generator.decrypt(decrpyt_bytes)
            # 去除解码后的非法字符
            result = re.compile('[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f\n\r\t]').sub('', meg.decode())
        except Exception:
            result = '解密失败,请确认输入是否有误!'
        return result


if __name__ == '__main__':
    aes = AESECB('this is aes key!')
    print(aes.encrypt('牛'))
    print(aes.encrypt('pythonaes123456'))
    print(aes.decrypt('bvN4eERKQBOSXu5EpZ+rBw=='))
    print(aes.decrypt('yzm34N/bEY8kVJNeS93Gv1svJI77YPjaK1+mW+/A4FY='))  # python is very good
上一篇下一篇

猜你喜欢

热点阅读