# Partial encryption of a file?

I want to partially encrypt a large block of data using AES. Meaning: given plaintext[0:1100], I would like to encrypt plaintext[0:1023]. The plaintext is generally machine code, but might have snippets of text.

Does this expose what I encrypted to any realistic challenges in the context of my application (for example: copy protection)?

• The answer depends on the nature of the plaintext, and how that makes it possible to deduce something useful about the plaintext from plaintext[1024:1100] (which if I get the question correctly is not encrypted).
– fgrieu
Oct 17 '13 at 11:57
• It depends largely on the mode of operation used and if you want to be able to decrypt parts with random access. If you use ECB (not recommended), then an attacker can identify two identical blocks easily.
– tylo
Oct 17 '13 at 12:30
• @tylo No need to decrypt with random access. Oct 18 '13 at 22:16
• Do you also want to protect the encrypted data from modification (e.g. bit flipping)? And do you also want to extend the protection to the unencrypted part, so that changing it will make decryption fail? Apr 11 '19 at 11:41

No, it doesn't have to expose information. Of course, any data in the last part of the file can be used to deduct information about the encrypted plaintext. Furthermore, it seems you want to perform in-place encryption. That's fine, but ciphers generally require an IV to make sure that the ciphertext is random even on identical plaintext. Using an IV generally means expanding the ciphertext size compared to the plaintext size.

There is one mode of operation that is perfect for encryption like this: CTR or counter mode (it may have even different names in other libraries). Here a counter is encrypted, and the resulting block is part of the key stream. You can simply XOR that key stream with the plaintext at the same offset to create the ciphertext. There is one big "but" (yo' mamma): CTR mode is completely unsafe if the counter is ever repeated, and the initial counter value is the IV. So you do need to store the IV somewhere where it can be related to the ciphertext. Possibilities are storage at the beginning or end of the file or in the meta-data of the (modern) file system.

It is also possible to calculate the IV, e.g. by performing a SHA-256 of the full path of the file name, and then taking the leftmost 128 bits. But note that any movement of the file will lead to corrupted plaintext (and possibly no way to decrypt if the path cannot be reconstructed).

If copy protection is used then leaving part of the file encrypted may indeed destroy the copy protection, if the bytes are taken into account e.g. for signature generation, using a hash over the encrypted data. However, without more info about copy protection (which comes in many forms) then we cannot tell for sure. Of course if you can fully restore the file before copyright protection takes place I presume you are secure.

It doesn't matter what the plaintext contains, as long as it can be represented as a sequence of bytes. That's what all modern encryption schemes process as their input and output.

It's perfectly OK to encrypt only some of the data, as long as you can tell which parts are encrypted and which are not. It's easiest if you have one contiguous chunk of bytes to encrypt, because most encryption APIs assume that. You can have unencrypted data before and/or after the encrypted part, and just not pass it to the encryption / decryption code.

With modern AEAD encryption schemes, you can even have both your encrypted and unencrypted data protected against tampering, so that decryption will fail if either part is modified. (You'd do that by including the unencrypted parts as associated data when encrypting and decrypting. You can also include other associated data if you want, like the file name for example.) Note that just encrypting the data with a non-authenticated scheme like plain CTR mode will not protect it against tampering. CTR mode in particular is highly malleable, allowing an attacker to flip arbitrary bits in the plaintext simply by flipping the corresponding bits of ciphertext.

For maximum security and "foolproofness", I would recommend using AES-SIV (or possibly AES-GCM-SIV). It's based on CTR mode, and shares its advantages such as the possibility of parallel and/or out-of-order decryption, but it also includes an authentication layer that turns it into a proper modern AEAD scheme. And it's designed to be "misuse-resistant", such that (unlike e.g. normal CTR mode) it will retain all of its security properties (other than hiding whether or not two encrypted files have identical plaintext) even if the nonce is repeated (or not used at all!).

SIV mode does require you to find a place to store its "synthetic IV" (a 16 byte string for AES-SIV, computed from the plaintext and its associated data) somewhere in your encrypted format. You can e.g. prepend or append it to the file, just as you'd do with the IV for basic CTR mode. But you need to store some kind of a tag, nonce and/or IV with any secure modern encryption scheme, so SIV mode is no worse in that regard. (If you also want to include a nonce for extra security, you obviously need to store that somewhere too. But if your plaintext and/or the unencrypted associated data already contains some unique version identifier or timestamp, then with SIV mode you don't really need a separate nonce.)

(For completeness, let me note the one major drawback of SIV mode: it's a two-pass encryption mode, meaning that it has to store the data to be encrypted in memory or on disk and process it twice, first to compute the synthetic IV and then to actually encrypt it. This makes SIV somewhat awkward to use for things like, say, encrypting streaming video. But for a 1 kb file, it's no problem at all.)

As for its suitability for copy protection, I really cannot say anything specific without more context. Let me just note in general that reliable copy protection is hard (some would say impossible), and there are no simple or foolproof crypto solutions for it.