Skip to content

Commit

Permalink
Fix bug with always riv encrypting files
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Oct 5, 2023
1 parent 1abbf3a commit 17b9cb3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions pubnub/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/encryption/steps/then_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 17b9cb3

Please sign in to comment.