发布时间:2019-09-26 07:27:17编辑:auto阅读(1662)
以下设置,适用于python3.5及以前的
pip install pycrypto
import base64
from Crypto.Cipher import AES
from Crypto import Random
BS = 16
key = "1234567890123456"
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[:-ord(s[len(s)-1:])]
class AESCipher:
def __init__(self, key):
self.key = key
def encrypt(self, raw):
raw = pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return base64.urlsafe_b64encode(iv + cipher.encrypt(raw))
def decrypt(self, enc):
enc = base64.urlsafe_b64decode(enc.encode('utf-8'))
iv = enc[:BS]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return unpad(cipher.decrypt(enc[BS:]))
a = AESCipher(key=key)
b = a.encrypt(raw='123456')
b1 = b.decode()
print(b1,type(b),type(b1))
c = a.decrypt(enc='N4wGyzPTnggQtUr_gyGcsxMzU136thzPIc8y3mJ2uxg=')
print(c)
python3.6版本的
pip install cryptography
from cryptography.fernet import Fernet
## key = base64.urlsafe_b64encode(os.urandom(32)) 生成key
def encrypt_p(password):
f = Fernet('Ow2Qd11KeZS_ahNOMicpWUr3nu3RjOUYa0_GEuMDlOc=')
p1 = password.encode()
token = f.encrypt(p1)
p2 = token.decode()
return p2
def decrypt_p(password):
f = Fernet('Ow2Qd11KeZS_ahNOMicpWUr3nu3RjOUYa0_GEuMDlOc=')
p1 = password.encode()
token = f.decrypt(p1)
p2 = token.decode()
return p2
上一篇: CentOS7.4 部署 Django
下一篇: windows下python3虚拟环境搭
47756
46264
37145
34652
29241
25899
24769
19873
19435
17928
5725°
6330°
5847°
5898°
7000°
5836°
5856°
6370°
6325°
7691°