I used "AES/CBC/PKCS5Padding" to encrypt some files. But I did not provide any identifier to detect if the file is encrypted or not. Now one way to detect if a file is encrypted or not, that I can think of, is to try to decrypt it and see if there is a padding miss match.

Now my question is,
Is it possible for some random file to match the padding scheme?

  • 1
    $\begingroup$ Be very careful with decrypting and looking for a padding mismatch. This can lead to padding oracle attacks. $\endgroup$ – mikeazo Aug 24 '18 at 13:32
  • $\begingroup$ @mikeazo: want to turn that into an answer? I can't think of anything to add... $\endgroup$ – poncho Aug 24 '18 at 14:28
  • $\begingroup$ If you had used an authenticated-encryption scheme, then checking if the file can be correctly decrypted would be a reliable (if rather inefficient) way to distinguish encrypted and unencrypted files. $\endgroup$ – Ilmari Karonen Aug 25 '18 at 12:09

Yes, it is quite possible that after decryption, a file that was not encrypted falsely appears to be encrypted with correct padding.

For a file of random byte length, there is probability 1/16 (6.25%) that its length is that of a valid encrypted file, and then there is probability just below 1/255 (≈0.392%) that after decryption, the last block ends with 01, or 0202, or 030303, or 04040404...

All in all, nearly one random file (of both random length and content) out of 4080 would wrongly pass the test.


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.