Is it possible to encrypt a file (e.g., a text document) such that when a user decrypts it, he/she be able to just read the file and not be able to change the content of the file?

I mean, the result of decryption be a read-only file. I think any modification can be detected using message authentication code. I am looking for a method to prevent the user from modifying the decrypted file.


A "file" is a convenient metaphor for a sequence of bits on a specific file system. Your copy of foo.bar is not the same as my copy of foo.bar, even if they contain the same data. And short of exploiting a bug in my system (illegal in many jurisdictions) there is no way for anyone else to dictate what I do with my copy of files. So in general, no, this is not possible if I have control over the system the file is on. Conversely, if you control the hardware, software and I/O devices of a system you can give me access to look at the contents of the file but not change it in any way.

However, if the goal is to preserve authenticity the solution is readily available: Use a hash-based cryptographic signature and ensure that anyone or anything that reads the resulting file verifies that the signature belongs to the right person.


Like you mentioned, you could use a Message Authentication Code along with Public/Private key cryptography. This is pretty much how message authentication works (proving a message actually came from who the sender claims they are without being modified), and can be used in your case to verify the file hasn't been changed after being decrypted. For example:

Bob has already gotten Alice's private key and verified it belongs to her

  1. Alice takes her file and hashes it.
  2. Alice takes that hash and encrypts it with her private key. This means that the only way to decrypt the hash is with the opposite key, her public key. Since Bob knows her public key, he can decrypt it.
  3. Alice encrypts her file and the ecnrypted hash with a symmetric cipher (ie. AES), in which Bob knows the password to.
  4. Bob decrypts the file and hash. Let's say he modified it even though Alice told him not to.
  5. Now, Charlie wants a copy of the file. Bob is willing to give him a copy. Bob gives him the file along with the hash.
  6. Charlie wants to make sure that Bob didn't change the file, so he goes and hashes it. Bob also has Alice's public key, so he uses that to decrypt the hash from Alice that Bob included with the file. He then compares them and sees they don't match. This means the file was modified on its way from Alice to Charlie Since Charlie wants to stay safe, he doesn't use that file because he knows it was modified.

This is the basis of Message Signing. It ensures nobody has changed the file in transit. As long as you know the sender's public key, then this system should be foolproof in guaranteeing that nobody changed the file, essentially making it 'read-only'.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.