@@ -21,14 +21,14 @@ class AES_CBCEncrypter(Encrypter):
2121 """
2222 """
2323
24- def __init__ (self , key_len = 32 , key = None , msg_padding = ' PKCS7' ):
24+ def __init__ (self , key_len = 32 , key = None , msg_padding = " PKCS7" ):
2525 Encrypter .__init__ (self )
2626 if key :
2727 self .key = key
2828 else :
2929 self .key = os .urandom (key_len )
3030
31- if msg_padding == ' PKCS7' :
31+ if msg_padding == " PKCS7" :
3232 self .padder = PKCS7 (128 ).padder ()
3333 self .unpadder = PKCS7 (128 ).unpadder ()
3434 else :
@@ -46,18 +46,18 @@ def _mac(self, hash_key, hash_func, auth_data, iv, enc_msg, key_len):
4646 m = h .finalize ()
4747 return m [:key_len ]
4848
49- def encrypt (self , msg , iv = '' , auth_data = b'' ):
49+ def encrypt (self , msg , iv = "" , auth_data = b"" ):
5050 if not iv :
5151 iv = os .urandom (16 )
5252 self .iv = iv
5353 else :
5454 self .iv = iv
5555
56- hash_key , enc_key , key_len , hash_func = get_keys_seclen_dgst (self .key ,
57- iv )
56+ hash_key , enc_key , key_len , hash_func = get_keys_seclen_dgst (self .key , iv )
5857
59- cipher = Cipher (algorithms .AES (enc_key ), modes .CBC (iv ),
60- backend = default_backend ())
58+ cipher = Cipher (
59+ algorithms .AES (enc_key ), modes .CBC (iv ), backend = default_backend ()
60+ )
6161 encryptor = cipher .encryptor ()
6262
6363 pmsg = self .padder .update (msg )
@@ -67,21 +67,22 @@ def encrypt(self, msg, iv='', auth_data=b''):
6767 tag = self ._mac (hash_key , hash_func , auth_data , iv , ct , key_len )
6868 return ct , tag
6969
70- def decrypt (self , msg , iv = '' , auth_data = b'' , tag = b'' , key = None ):
70+ def decrypt (self , msg , iv = "" , auth_data = b"" , tag = b"" , key = None ):
7171 if key is None :
7272 if self .key :
7373 key = self .key
7474 else :
75- raise MissingKey (' No available key' )
75+ raise MissingKey (" No available key" )
7676
7777 hash_key , enc_key , key_len , hash_func = get_keys_seclen_dgst (key , iv )
7878
7979 comp_tag = self ._mac (hash_key , hash_func , auth_data , iv , msg , key_len )
8080 if comp_tag != tag :
81- raise VerificationError (' AES-CBC HMAC' )
81+ raise VerificationError (" AES-CBC HMAC" )
8282
83- cipher = Cipher (algorithms .AES (enc_key ), modes .CBC (iv ),
84- backend = default_backend ())
83+ cipher = Cipher (
84+ algorithms .AES (enc_key ), modes .CBC (iv ), backend = default_backend ()
85+ )
8586 decryptor = cipher .decryptor ()
8687
8788 ctext = decryptor .update (msg )
@@ -102,9 +103,9 @@ def __init__(self, bit_length=0, key=None):
102103
103104 self .key = AESGCM (AESGCM .generate_key (bit_length = bit_length ))
104105 else :
105- raise ValueError (' Need key or key bit length' )
106+ raise ValueError (" Need key or key bit length" )
106107
107- def encrypt (self , msg , iv = '' , auth_data = None ):
108+ def encrypt (self , msg , iv = "" , auth_data = None ):
108109 """
109110 Encrypts and authenticates the data provided as well as authenticating
110111 the associated_data.
@@ -115,11 +116,11 @@ def encrypt(self, msg, iv='', auth_data=None):
115116 :return: The cipher text bytes with the 16 byte tag appended.
116117 """
117118 if not iv :
118- raise ValueError (' Missing Nonce' )
119+ raise ValueError (" Missing Nonce" )
119120
120121 return self .key .encrypt (iv , msg , auth_data )
121122
122- def decrypt (self , cipher_text , iv = '' , auth_data = None , tag = b'' ):
123+ def decrypt (self , cipher_text , iv = "" , auth_data = None , tag = b"" ):
123124 """
124125 Decrypts the data and authenticates the associated_data (if provided).
125126
@@ -130,6 +131,6 @@ def decrypt(self, cipher_text, iv='', auth_data=None, tag=b''):
130131 :return: The original plaintext
131132 """
132133 if not iv :
133- raise ValueError (' Missing Nonce' )
134+ raise ValueError (" Missing Nonce" )
134135
135- return self .key .decrypt (iv , cipher_text + tag , auth_data )
136+ return self .key .decrypt (iv , cipher_text + tag , auth_data )
0 commit comments