python aes 加密

发布时间:2019-06-04 21:05:07编辑:auto阅读(2258)

    python 进行aes 加密报错,
    windows pip install pycryptodome 安装Crypto

    linux pip install pycrypto

     1 import base64
     2 
     3 from Crypto.Cipher import AES
     4 
     5 
     6 def add_to_16(value):
     7     while len(value) % 16 != 0:
     8         value += '\0'
     9     return str.encode(value) 
    10 
    11 # 加密方法
    12 def encrypt_oracle(self, text):
    13     key = ''
    14     aes = AES.new(self.add_to_16(key), AES.MODE_ECB)
    15     pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)
    16     encrypt_aes = aes.encrypt(self.add_to_16(pad(text)))
    17     encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') 
    18     return encrypted_text
    19 
    20 
    21 # 解密方法
    22 def decrypt_oralce(text):
    23     key = ''
    24     aes = AES.new(add_to_16(key), AES.MODE_ECB)
    25     base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8'))
    26     decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8').replace('\0', '')
    27     return decrypted_text

     

关键字