def decrypt(filename, k_size, pwd): M_SIZE = 4 msg = filename key_size = k_size key = [[184, 18, 29, 158], [123, 45, 253, 202], [254, 124, 64, 32], [86, 54, 48, 68]] m = segmess.segmess(msg) m_size = len(m) print("Decrytpion for %s" % (msg)) print(m) # Inversion: mc1 = m mc2 = m mc3 = m mc4 = m # InvAddRoundKey: for cpt in range(m_size): mc1[cpt] = addRoundKey.addRoundKey(m[cpt], key) # InvMixColumns: for cpt in range(m_size): mc2[cpt] = mixColumns.invMixColumns(mc1[cpt]) # InvShiftRows: for cpt in range(m_size): mc3[cpt] = shiftRows.invShiftRows(mc2[cpt], key_size) # InvSubBytes: for cpt in range(m_size): mc4[cpt] = subBytes.invSubBytes(mc3[cpt]) c_filename = filename + ".clr" segmess.invSegmessClr(mc4, c_filename) print("Decrypted message:") print(mc4)
def go(self): words = keyExpansion(self.key) if self.printMode != OFF: self.printChanges( "initializing state", f"round[0].iinput: {self.state.getStateAsStr()}", ) key = "".join(words[40:44]) self.state = addRoundKey(self.state, key) if self.printMode != OFF: self.printChanges( f"addRoundKey for round {self.currentRound}", f"round[0].ik_sch: {key}", ) self.currentRound -= 1 flip = [9, 8, 7, 6, 5, 4, 3, 2, 1] while self.currentRound > 0: printRound = flip[self.currentRound - 1] if self.printMode != OFF: if self.printMode == GRID: input( f"Press return to advance to round {self.currentRound}" ) print() self.printChanges( f"the start of {self.currentRound}", f"round[{printRound}].istart: {self.state.getStateAsStr()}", ) self.state = invShiftRows(self.state) if self.printMode != OFF: self.printChanges( "shifted rows", f"round[{printRound}].is_row: {self.state.getStateAsStr()}", ) self.state = invSubBytes(self.state) if self.printMode != OFF: self.printChanges( "subbed bytes using sBox", f"round[{printRound}].is_box: {self.state.getStateAsStr()}", ) key = "".join(words[self.currentRound * 4:(self.currentRound + 1) * 4]) self.state = addRoundKey(self.state, key) if self.printMode != OFF: self.printChanges( f"addRoundKey {self.currentRound}", f"round[{printRound}].ik_sch: {key}", ) self.printChanges( f"addRoundKey {self.currentRound}", f"round[{printRound}].ik_add: {self.state.getStateAsStr()}", ) self.state = invMixColumns(self.state, self.f) self.currentRound -= 1 # End of Core Loop (rounds 1-9) if self.printMode != OFF: if self.printMode == GRID: input(f"Press return to advance to round {self.currentRound}") print() self.printChanges( f"the start of {self.currentRound}", f"round[10].istart: {self.state.getStateAsStr()}", ) self.state = invShiftRows(self.state) if self.printMode != OFF: self.printChanges( "final shift rows", f"round[10].is_row: {self.state.getStateAsStr()}", ) self.state = invSubBytes(self.state) if self.printMode != OFF: self.printChanges( "subbed bytes using sBox", f"round[10].is_box: {self.state.getStateAsStr()}", ) key = "".join(words[0:4]) self.state = addRoundKey(self.state, key) if self.printMode != OFF: self.printChanges( "final add round key", f"round[10].ik_sch: {key}", ) self.result = self.state.getStateAsStr() if self.printMode == GRID: print("***********************************************\n" + f"Final State / Cipher Output\n" + "***********************************************") self.state.printStateAsGrid() if self.printMode == CMODE: self.printChanges("", f"round[10].ioutput: {self.result}") if self.printMode == OFF: print("Result: " + self.result)
def crypt(filename, k_size, pwd): M_SIZE = 4 msg = filename key_size = k_size key = [[184, 18, 29, 158], [123, 45, 253, 202], [254, 124, 64, 32], [86, 54, 48, 68], [184, 18, 29, 158], [123, 45, 253, 202], [254, 124, 64, 32], [86, 54, 48, 68]] m = segmess.segmess(msg) m_size = len(m) print("Encrytpion for %s" % (msg)) print(m) # Tempo array definition c1 = m c2 = m c3 = m c4 = m # SubBytes: for cpt in range(m_size): c1[cpt] = subBytes.subBytes(m[cpt]) # ShiftRows: for cpt in range(m_size): c2[cpt] = shiftRows.shiftRows(c1[cpt], key_size) # MixColumns: for cpt in range(m_size): c3[cpt] = mixColumns.mixColumns(c2[cpt]) # AddRoundKey: for cpt in range(m_size): c4[cpt] = addRoundKey.addRoundKey(c3[cpt], key) print("Encrypted message:") print(c4) c_filename = filename + ".chif" segmess.invSegmessChiff(c4, c_filename) # ==================== Test: Inversion =================== print("\nDecryption checking...") mc1 = m mc2 = m mc3 = m mc4 = m # InvAddRoundKey: for cpt in range(m_size): mc1[cpt] = addRoundKey.addRoundKey(c4[cpt], key) # InvMixColumns: for cpt in range(m_size): mc2[cpt] = mixColumns.invMixColumns(c1[cpt]) # InvShiftRows: for cpt in range(m_size): mc3[cpt] = shiftRows.invShiftRows(mc2[cpt], key_size) # InvSubBytes: for cpt in range(m_size): mc4[cpt] = subBytes.invSubBytes(mc3[cpt]) print("Decrypted message:") print(mc4)