I need to write a Python script to occasionally encrypt some small text files (~10 Kb)
in order to store them in an insecure environment.

Each file will be encrypted via unique symmetric key and AES 256 CTR cipher.
The key and CTR nonce will then be encrypted via RSA key and appended to the
encrypted file.

Could you please help to clarify the following security concerns:

  1. I currently use the following code for encryption, basing on Python Cryptodome examples:
    key = get_random_bytes(32)
    cipher = AES.new(key, AES.MODE_CTR)
    encr_data = cipher.encrypt(orig_data)
    Is Cryptodome get_random_bytes() secure to be used for AES keys generation?
    If not, which one is the better alternative?

  2. In my current script the initial value of CTR counter is set to default value 0. Is it a valid approach?
    Or it makes the implementation vulnerable, and some more sophisticated approach should be used
    to generate it?

  3. The unencrypted text of each file starts from the same publicly known header which is ~60 bytes.
    So, though each file will be encrypted with a unique key, there will be a ~60 bytes known match
    between open text and encrypted text. Does it give any chance to decrypt the rest content of the file?

  • 1
    With regard to #3 - this is called a 'known plaintext attack'. AES is resistant to known plaintext attacks. See crypto.stackexchange.com/questions/1512/… for more info.
    – mti2935
    Dec 4 '21 at 21:42
  • Use XChaCha20-Poly1305, generate random nonce and keys safely.
    – kelalaka
    Dec 5 '21 at 12:00
  • Also, use Fernet even better documented cryptography.io/en/latest/random-numbers
    – kelalaka
    Dec 5 '21 at 14:40
  • @mti2935, ok, understood, thank you.
    – Maxim
    Dec 6 '21 at 20:11
  • 1
    @kelalaka, thanks for pointing to cryptography.io/en/latest/random-numbers. Regarding cipher mode, for some reasons I need to use AES CTR, so from my side only a correct implementation is needed.
    – Maxim
    Dec 6 '21 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.