Is it possible to recover an AES encrypted file that was decrypted with incorrect key/password? I am new to cryptography and have been experimenting with AES to encrypt files. I was testing some code I found online which allows the user to enter the directory of the files that they went to encrypt/decrypt, the program then asks for the user to input the “password” or “key” to be used for the encryption/decryption. 

My problem is that I accidently inputted the wrong password/key when I was trying to decrypt the file, and as I hadn’t added a way for the program to detect incorrect passwords it tried to decrypt the file and so I was left with a file that was still in an encrypted/corrupted state.

I am wondering if it is possible to recover the original encrypted file. I now know the correct password that I should have first used to decrypt the file as well as the incorrect password that was used.

I have found another page where a user had a similar problem here Repair AES-128 decrypted file however this user did not specify that they knew the “correct” password that would have worked for the decryption, whereas I do.

The encryption function used in the script used a random IV as well as AES mode CBC. 

Any help appreciated thanks
Note: This is answer version 2. I hastily answered the question incorrectly before.



It's not possible without brute forcing the IV. While encryption is reversible, encryption is generally one-to-many operation and decryption a many-to-one operation. This is because using the same key pair IV accross multiple messages can compromise security. For example if you send the same message twice then a one-to-one deterministic algorithm would leak the fact that you said the same thing twice. The part of the IV in any encryption algorithm that cannot be reused is called a "nonce" for number-used-only-once.

One way to generate distinct nonce values is to start with zero and increment by one every time you encrypt. If at some point you forget the counter you have to rekey. You could recover data in this case.

A random nonce is a memoryless way to prevent reuse. It needs to have enough entropy to prevent the same random value being generated twice. (Large enough to prevent birthday collisions.) This means a random IV is too long to brute force if you lose it.

However if you knew the IV and key, you could reverse CBC encryption. If it's deleted and unpredictable you cannot. CBC mode is deterministic and invertible for a given/known key/IV pair. It has the property $E_{K,IV}^{-1}(E_{K,IV}(M))$ = M (normal operation). Where $E$ is encryption and $E^{-1}$ is decryption. Additionally $E_{K,IV}(E_{K,IV}^{-1}(M)) = M$. It's not something you would normally do, but you could do it to recover the ciphertext file. 

I assume you encrypted plaintext, deleted the original file, then wrote the ciphertext to disk. Then in the decryption process you encrypted with the wrong key, deleted the ciphertext (that included your IV), then wrote the wrong plaintext to disk. That process would mean you lost the IV because the random IV is only stored in the ciphertext file.

When you use encryption you should also use message authentication. You check if the MAC tag attached to a message is correct before trying to use decrypted data. Using authentication would have prevented your mistake.