Python3 django2.0 字段加密 解密 AES
生活随笔
收集整理的這篇文章主要介紹了
Python3 django2.0 字段加密 解密 AES
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以下設置,適用于python3.5及以前的
pip install pycryptoimport base64 from Crypto.Cipher import AES from Crypto import RandomBS = 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 = keydef 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 cryptographyfrom cryptography.fernet import Fernet## key = base64.urlsafe_b64encode(os.urandom(32)) 生成keydef encrypt_p(password):f = Fernet('Ow2Qd11KeZS_ahNOMicpWUr3nu3RjOUYa0_GEuMDlOc=')p1 = password.encode()token = f.encrypt(p1)p2 = token.decode()return p2def decrypt_p(password):f = Fernet('Ow2Qd11KeZS_ahNOMicpWUr3nu3RjOUYa0_GEuMDlOc=')p1 = password.encode()token = f.decrypt(p1)p2 = token.decode()return p2本文轉自 295631788 51CTO博客,原文鏈接:http://blog.51cto.com/hequan/2054844,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Python3 django2.0 字段加密 解密 AES的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10.23cron10.24chkcon
- 下一篇: TCExam开源在线考试系统