I'm going to make a long story short. It's been a while that I want to implement my own AES encryption/decryption program. The encryption program went well and encrypting without any error or strange output (Since I have compared my program's output with a working commercial one and result was the same).
Wikipedia was (is) my guide in this implementation within which I read "A set of reverse rounds are applied to transform ciphertext back into the original plaintext using the same encryption key."
There are couple of modules that I implemented:
I also implemented couple of reverse implementation of the above modules:
NOTE: I didn't implement reverse round key since, It's XOR ing the plaintext with the encryption key, and reverse of XOR is XOR itself (correct me if I am wrong)
So I putted this modules in the reverse order that I did encryption, but never I got my plain-text back:
expandkey128(key);
rev_subbytes(data);
rev_shiftrows(data);
addroundkey(data,key,10);
for(int i = 9; i>= 1; i--) {
rev_subbytes(data);
rev_shiftrows(data);
rev_mixColum(data);
addroundkey(data,key,i);
}
addroundkey(data,key,0);
// Please note that I also did from 0 to 10 ,
// instead of 10 to 0 and didn't workout
And also I thought , maybe I should not implement reverse model of the modules, maybe I have to use those modules that I did encryption with, only in reverse order; well guess what? didn't work! :
expandkey128(key);
addroundkey(data,key,0);
for(int i = 1; i<= 9; i++) {
subbytes(data);
shiftrows(data);
mixColum(data);
addroundkey(data,key,i);
}
subbytes(data);
shiftrows(data);
addroundkey(data,key,10);
So here is the question: what is wrong? || what is the correct sequence of applying these so called modules or functions if you will?
Your order of operations seems wrong. I think you want this:
expandkey128(key);
addroundkey(data,key,10);
rev_shiftrows(data);
rev_subbytes(data);
for(int i = 9; i>= 1; i--) {
addroundkey(data,key,i);
rev_mixColumn(data);
rev_shiftrows(data);
rev_subbytes(data);
}
addroundkey(data,key,0);
For more details, see my stick figure explanation of AES with its accompanying reference implementation.
WARNING: As mentioned in Act 3, Scene 2, there be dragons in writing your own AES implementation for production use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With