原創文章,歡迎轉發朋友圈,轉載請註明出處python
cryptography是python語言中很是著名的加解密庫,在算法層面提供了高層次的抽象,使用起來很是簡單、直觀,pythonic,同時還保留了各類不一樣算法的低級別接口,保留靈活性。算法
咱們知道加密通常分爲對稱加密(Symmetric Key Encryption)和非對稱加密(Asymmetric Key Encryption)。,各自對應多種不一樣的算法,每種算法又有不一樣的密鑰位長要求,另外還涉及到不一樣的分組加密模式,以及末尾補齊方式。所以須要高層次的抽象,把這些參數封裝起來,讓咱們使用時,不用關心這麼多參數,只要知道這麼用足夠安全就夠了。安全
對稱加密又分爲分組加密和序列加密,本文只討論對稱分組加密。dom
主流對稱分組加密算法:DES、3DES、AES編碼
主流對稱分組加密模式:ECB、CBC、CFB、OFB加密
主流填充標準:PKCS七、ISO 1012六、ANSI X.92三、Zero paddingurl
在cryptography庫中,對稱加密算法的抽象是fernet模塊,包括了對數據的加解密以及簽名驗證功能,以及密鑰過時機制。 spa
該模塊採用以下定義:code
>>> import os >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes >>> from cryptography.hazmat.backends import default_backend >>> backend = default_backend() >>> key = os.urandom(32) >>> iv = os.urandom(16) >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) >>> encryptor = cipher.encryptor() >>> ct = encryptor.update(b"a secret message") + encryptor.finalize() >>> decryptor = cipher.decryptor() >>> decryptor.update(ct) + decryptor.finalize() 'a secret message'
@classmethod def generate_key(cls): return base64.urlsafe_b64encode(os.urandom(32))
self._signing_key = key[:16] self._encryption_key = key[16:] self._backend = backend
basic_parts = ( b"\x80" + struct.pack(">Q", current_time) + iv + ciphertext )
def encrypt(self, data): current_time = int(time.time()) iv = os.urandom(16) return self._encrypt_from_parts(data, current_time, iv) def _encrypt_from_parts(self, data, current_time, iv): if not isinstance(data, bytes): raise TypeError("data must be bytes.") padder = padding.PKCS7(algorithms.AES.block_size).padder() padded_data = padder.update(data) + padder.finalize() encryptor = Cipher( algorithms.AES(self._encryption_key), modes.CBC(iv), self._backend ).encryptor() ciphertext = encryptor.update(padded_data) + encryptor.finalize() basic_parts = ( b"\x80" + struct.pack(">Q", current_time) + iv + ciphertext ) h = HMAC(self._signing_key, hashes.SHA256(), backend=self._backend) h.update(basic_parts) hmac = h.finalize() return base64.urlsafe_b64encode(basic_parts + hmac)
def decrypt(self, token, ttl=None): if not isinstance(token, bytes): raise TypeError("token must be bytes.") current_time = int(time.time()) try: data = base64.urlsafe_b64decode(token) except (TypeError, binascii.Error): raise InvalidToken if not data or six.indexbytes(data, 0) != 0x80: raise InvalidToken try: timestamp, = struct.unpack(">Q", data[1:9]) except struct.error: raise InvalidToken if ttl is not None: if timestamp + ttl < current_time: raise InvalidToken if current_time + _MAX_CLOCK_SKEW < timestamp: raise InvalidToken h = HMAC(self._signing_key, hashes.SHA256(), backend=self._backend) h.update(data[:-32]) try: h.verify(data[-32:]) except InvalidSignature: raise InvalidToken iv = data[9:25] ciphertext = data[25:-32] decryptor = Cipher( algorithms.AES(self._encryption_key), modes.CBC(iv), self._backend ).decryptor() plaintext_padded = decryptor.update(ciphertext) try: plaintext_padded += decryptor.finalize() except ValueError: raise InvalidToken unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder() unpadded = unpadder.update(plaintext_padded) try: unpadded += unpadder.finalize() except ValueError: raise InvalidToken return unpadded