1

I am trying to encrypt a PDF file using PyCryptodome and giving some unique ID of the system as key parameter. i.e uuid etc. I want to use AES Encryption along with GCM mode because GCM mode is an Authentic Encryption, as it returns a MAC that is used for authentication. I have following troubles:

  1. I have googled a lot, but can't find a code that encrypts a file. Everybody is encrypting strings.
  2. I want to use GCM mode with AES, so, need an example that uses GCM mode.
  3. In the example given below, after encrypting code is appending iv (Initialization Vector) with the encrypted text and using it while decryption. So my question is, how will I accomplish this while encrypting files.
  4. I don't know how to use the MAC authenticity check while decrypting the file.

This is what I have with me right now, It is also encrypting string, unfortunately:

import base64
import hashlib
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes

key = hashlib.sha256(b"uuid goes here").digest()

def encrypt(raw):
    BS = AES.block_size
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
    raw = base64.b64encode(pad(raw).encode('utf8'))
    iv = get_random_bytes(AES.block_size)
    cipher = AES.new(key= key, mode= AES.MODE_CFB,iv= iv)
    return base64.b64encode(iv + cipher.encrypt(raw))

def decrypt(enc):
    unpad = lambda s: s[:-ord(s[-1:])]
    enc = base64.b64decode(enc)
    iv = enc[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CFB, iv)
    return unpad(base64.b64decode(cipher.decrypt(enc[AES.block_size:])).decode('utf8'))


e = encrypt('I am string, dont you want to use file?')
print(decrypt(e))
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
  • 1
    "Everybody is encrypting strings." So load the file as a string and encrypt it? Or do you want to encrypt a file without loading it first into memory? – Jongware Mar 01 '20 at 15:54
  • @usr2564301, I want to encrypt the file, may be we can read data byte by byte and encrypt it. I am new to this so i am not sure – Zain Arshad Mar 01 '20 at 15:55
  • I suggest to load file, encrypt it, save back. (But not over your original, in case you made a mistake...) – Jongware Mar 01 '20 at 16:00
  • PDF file can have images, graphs etc. I think , I can't just load the file as a string. – Zain Arshad Mar 01 '20 at 16:01
  • A-ha – you think that it only works on strings. (No – [this example from its own homepage](https://www.pycryptodome.org/en/latest/src/examples.html#encrypt-data-with-aes) works on *bytes* as well.) -- edit: sorry, that's for the key. But still, why not try it and see? – Jongware Mar 01 '20 at 16:06
  • I have to check the authentication tag also, so if i am just sending the encrypted file, how will I check the tag ? – Zain Arshad Mar 01 '20 at 16:28
  • Nonce and tag must be stored together with the ciphertext. Exactly this is done in the linked example of @usr2564301. The example actually uses the EAX mode instead of the GCM mode, but this is irrelevant for the storing. In the PyCryptodome documentation there is also a [GCM example](https://pycryptodome.readthedocs.io/en/latest/src/cipher/modern.html#gcm-mode). Consider the note in the documentation that a 12 bytes nonce should be used. – Topaco Mar 02 '20 at 12:15

0 Answers0