@@ -159,7 +159,7 @@ def __add_to_keystore__(self, index, key, id):
159159 else :
160160 keystore [index ][store_id ] = key .hex ()
161161
162- def __encrypt_keys_from_keystore__ (self , index ):
162+ def __encrypt_keys_from_keystore__ (self , index , plaintext_length = - 1 ):
163163 keystore = self .setup ['keystore' ]
164164 password = index [1 ]
165165 if index [0 ] == KS_OBFUSCATE :
@@ -201,10 +201,14 @@ def __encrypt_keys_from_keystore__(self, index):
201201 else :
202202 iv = get_random_bytes (16 )
203203 cipher = AES .new (kdfkey , AES .MODE_CBC , iv )
204- # use it to encrypt the AES-256 key
205- plaintext = json .dumps (keystore [index ]).encode ()
204+ # use it to encrypt the AES-256 key(s)
205+ plaintext = json .dumps (keystore [index ])
206+ # add spaces to plaintext to make keystores indistinguishable
207+ if len (plaintext ) < plaintext_length :
208+ plaintext += ' ' * (plaintext_length - len (plaintext ))
209+ plaintext_encoded = plaintext .encode ()
206210 # plaintext must be padded to be a multiple of 16 bytes
207- plaintext_padded = pad (plaintext , 16 , style = 'pkcs7' )
211+ plaintext_padded = pad (plaintext_encoded , 16 , style = 'pkcs7' )
208212 ciphertext = cipher .encrypt (plaintext_padded )
209213
210214 if iterations > 1 : #don't calculate entropy for obfuscate passwords
@@ -878,17 +882,24 @@ def on_page_context(self, context, page, config, **kwargs):
878882 if obfuscate_id not in self .setup ['keystore' ][index2 ].keys ():
879883 self .setup ['keystore' ][index2 ][obfuscate_id ] = keystore [index ][obfuscate_id ]
880884
885+ #find longest keystore
886+ max_keystore_length = 0
887+ for index in self .setup ['keystore' ]:
888+ keystore_length = len (json .dumps (self .setup ['keystore' ][index ]))
889+ if keystore_length > max_keystore_length :
890+ max_keystore_length = keystore_length
891+
881892 # Encrypt all keys to keystore
882893 # It just encrypts once, but needs to run on every page
883894 for index in self .setup ['keystore' ]:
884895 if index [0 ] == KS_OBFUSCATE :
885896 pass
886897 elif index [0 ] == KS_PASSWORD :
887898 if index not in self .setup ['keystore_password' ]:
888- self .setup ['keystore_password' ][index ] = ';' .join (self .__encrypt_keys_from_keystore__ (index ))
899+ self .setup ['keystore_password' ][index ] = ';' .join (self .__encrypt_keys_from_keystore__ (index , max_keystore_length ))
889900 else :
890901 if index not in self .setup ['keystore_userpass' ]:
891- self .setup ['keystore_userpass' ][index ] = ';' .join (self .__encrypt_keys_from_keystore__ (index ))
902+ self .setup ['keystore_userpass' ][index ] = ';' .join (self .__encrypt_keys_from_keystore__ (index , max_keystore_length ))
892903
893904 if hasattr (page , 'encryptcontent' ):
894905
0 commit comments