欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python aes 加密

程序员文章站 2024-02-01 18:18:10
python 进行aes 加密报错,windows pip install pycryptodome 安装Crypto linux pip install pycrypto ......

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