def ShiftRows(inState): rows = toRows(inState) outState = [] for x in range(4): word = narwhal( rows[x] ) for y in range(x): word = rotWord(word) outState.append(word) return toState(outState)
def rotWord(inWord, inv=False): outWord = narwhal( inWord ) if inv: f = outWord.pop() outWord = [f] + outWord else: f = outWord[0] outWord.remove(f) outWord.append(f) return outWord
def InvCipher(inState, key, Nb, Nk, Nr, l=False): state = narwhal( inState ) w = KeyExpansion(key, Nb=Nb, Nk=Nk, Nr=Nr) if l: print "State: " + printState(state) state = AddRoundKey(state, w[Nr*Nb:(Nr+1)*Nb]) if l: print "AddRoundKey: " + printState(state) sleep(1) rng = range(1, Nr) rng.reverse() for x in rng: if l: print "ROUND {round}".format(round=str(x)) state = InvShiftRows(state) if l: print "InvShiftRows: " + printState(state) state = InvSubBytes(state) if l: print "InvSubBytes: " + printState(state) state = AddRoundKey(state, w[x*Nb:(x+1)*Nb]) if l: print "AddRoundKey: " + printState(state) state = InvMixColumns(state) if l: print "InvMixColumns: " + printState(state) sleep(1) state = InvShiftRows(state) if l: print "\nFINAL ROUND" print "InvShiftRows: " + printState(state) state = InvSubBytes(state) if l: print "InvSubBytes: " + printState(state) state = AddRoundKey(state, w[0:Nb]) if l: print "AddRoundKey (FINAL DECIPHERED TEXT): " + printState(state) return state