linux 下使用AES时安装pycrypto模块,pip install pycrypto
```
from Crypto.Cipherimport AES
from binasciiimport b2a_hex, a2b_hex
from Cryptoimport Random
import base64
import json
class AesEncry(object):
# aes秘钥 ,可根据自身需要手动生成
key ="aes_keysaes_keysaes_keys"
def encrypt(self, data):
data = json.dumps(data)
mode = AES.MODE_ECB
padding =lambda s: s + (16 -len(s) %16) *chr(16 -len(s) %16)
cryptos = AES.new(self.key.encode("utf-8"), mode)
cipher_text = cryptos.encrypt(padding(data).encode("utf-8"))
return base64.b64encode(cipher_text).decode("utf-8")
def decrypt(self, data):
cryptos = AES.new(self.key.encode("utf-8"), AES.MODE_ECB)
decrpytBytes = base64.b64decode(data)
meg = cryptos.decrypt(decrpytBytes).decode('utf-8')
return meg[:-ord(meg[-1])]
aes_encry_util = AesEncry()
#明文
data ="mypwd_test"
#加密
encry_data = aes_encry_util.encrypt(data)
print(encry_data)
# 对密文进行解密
decry_data = aes_encry_util.decrypt(encry_data)
print(decry_data)
```
如上便完成了利用python进行AES的ECB加密
问题背景:
m3u8文件加密时,使用“from Crypto.Cipher import AES”相关函数解密:
#EXT-X-KEY 记录了加密的方式,一般是AES-128以及加密的KEY信息
出现问题:
from Crypto.Cipher import AES
pip install Crypto
出错
解决办法:
安装crypto库(首字母c是小写)
pip install crypto
进入python的库管理位置,site-packages文件夹,找到crypto,将其首字母c改为大写
判断是否解决的方式:
from Crypto.Cipher import AES
不会报错,说明成功。
备注:
如果在C:\Python36\Lib\site-packages\Crypto目录下没有找到:\Cipher目录。
可以尝试安装pycryptodome库 或 pycrypto库:
pip install pycryptodome
pip install pycrypto (安装这个库,基本会失败,会报错)