From 17b9cb352b75292d23313c8c46c9ef34260874f2 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Thu, 5 Oct 2023 23:12:49 +0200 Subject: [PATCH] Fix bug with always riv encrypting files --- pubnub/crypto.py | 9 +++++---- tests/acceptance/encryption/steps/then_steps.py | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pubnub/crypto.py b/pubnub/crypto.py index 90559e54..9942573f 100644 --- a/pubnub/crypto.py +++ b/pubnub/crypto.py @@ -85,9 +85,10 @@ def get_secret(self, key): class PubNubFileCrypto(PubNubCryptodome): - def encrypt(self, key, file): + def encrypt(self, key, file, use_random_iv=True): + secret = self.get_secret(key) - initialization_vector = self.get_initialization_vector(use_random_iv=True) + initialization_vector = self.get_initialization_vector(use_random_iv) cipher = AES.new(bytes(secret[0:32], "utf-8"), self.mode, bytes(initialization_vector, 'utf-8')) initialization_vector = bytes(initialization_vector, 'utf-8') @@ -97,9 +98,9 @@ def encrypt(self, key, file): initialization_vector=initialization_vector ) - def decrypt(self, key, file): + def decrypt(self, key, file, use_random_iv=True): secret = self.get_secret(key) - initialization_vector, extracted_file = self.extract_random_iv(file, use_random_iv=True) + initialization_vector, extracted_file = self.extract_random_iv(file, use_random_iv) try: cipher = AES.new(bytes(secret[0:32], "utf-8"), self.mode, initialization_vector) result = unpad(cipher.decrypt(extracted_file), 16) diff --git a/tests/acceptance/encryption/steps/then_steps.py b/tests/acceptance/encryption/steps/then_steps.py index f8af3b37..5c0b50ba 100644 --- a/tests/acceptance/encryption/steps/then_steps.py +++ b/tests/acceptance/encryption/steps/then_steps.py @@ -13,7 +13,7 @@ def step_impl(context: PNContext): config = PNConfiguration() config.cipher_key = context.cipher_key config.use_random_initialization_vector = context.use_random_iv - decrypted_legacy = config.file_crypto.decrypt(context.cipher_key, context.encrypted_file) + decrypted_legacy = config.file_crypto.decrypt(context.cipher_key, context.encrypted_file, context.use_random_iv) assert decrypted_legacy == context.decrypted_file assert context.outcome == 'success'