AES-GCM으로 RAM에 맞지 않는 큰 파일 암호화

Nov 22 2020

이 코드는 myfileRAM에 맞는 파일에서 작동합니다 .

import Crypto.Random, Crypto.Cipher.AES   # pip install pycryptodome

nonce = Crypto.Random.new().read(16)
key = Crypto.Random.new().read(16)  # in reality, use a key derivation function, etc. ouf of topic here
cipher = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_GCM, nonce=nonce)

out = io.BytesIO()
with open('myfile', 'rb') as g:
    s = g.read()
ciphertext, tag = cipher.encrypt_and_digest(s)
out.write(nonce)
out.write(ciphertext)
out.write(tag)

그러나이 기술을 사용하여 64GB 파일을 암호화하는 방법은 무엇입니까?

분명히 g.read(...)는 128MB와 같이 더 작은 버퍼 크기를 사용해야합니다.

그렇다면 암호화 부분에서는 어떻게 작동합니까? (ciphertext, tag)각 128MB 청크에 대해 a 를 유지해야합니까 ?

아니면 tag전체 파일에 대해 하나만 가질 수 있습니까?

답변

Basj Nov 22 2020 at 20:30

@ PresidentJamesK.Polk의 의견에서 언급했듯이 이것이 해결책 인 것 같습니다.

out.write(nonce)
while True:
    block = g.read(65536)
    if not block:
        break
    out.write(cipher.encrypt(block))
out.write(cipher.digest())  # 16-byte tag at the end of the file

유일한 문제는 암호 해독을 위해이 파일을 다시 읽을 때 끝에서 16 바이트를 빼는 것이 약간 짜증 난다는 것 입니다.

또는 이렇게해야 할 수도 있습니다.

out.write(nonce)
out.seek(16, 1)  # go forward of 16 bytes, placeholder for tag
while True:
   ...
   ...
out.seek(16)
out.write(cipher.digest())  # write the tag at offset #16 of the output file

?