Skip to content

PersistenceDecryptionError

Ray Luo edited this page Mar 9, 2023 · 2 revisions

A persistence instance's load() method, for example, FilePersistenceWithDataProtection.load(), may raise the PersistenceDecryptionError exception. This exception means the data decryption was unsuccessful. The error trace would contain its inner exception OSError and some technical details, which may or may not be actionable for app developer to troubleshoot.

For example, on Windows platform, a Windows password reset may cause the old encrypted data become inaccessible.

In general, depends on your data's importance, app developer may choose some different pattern to recover from PersistenceDecryptionError.

  1. When the data is valuable, and we suspect the decryption failure is likely caused by trying to read a preexisting plaintext data file with same name, app developer can use the following pattern to convert it into an encrypted data file on-the-fly.
filename = "my_data.bin"
encrypted_persistence = FilePersistenceWithDataProtection(filename)
try:
    valuable_data = encrypted_persistence.load()
except PersistenceDecryptionError:
    valuable_data = FilePersistence(filename).load()
    encrypted_persistence.save(valuable_data)
  1. When the data is expendable, app developer may choose to simply reset the data file.
filename = "my_data.bin"
encrypted_persistence = FilePersistenceWithDataProtection(filename)
try:
    expendable_data = encrypted_persistence.load()
except PersistenceDecryptionError:
    os.remove(filename)
    expendable_data = None
Clone this wiki locally