def testCounter(self): tk = Toolkit() for i in range(5): hashVal = tk.counter(i) for j in range(8): print(hashVal[0][j], hashVal[1][j]) print()
def testRoundKey(self): tk = Toolkit() out = tk.roundKey("Cendhika Imantoro - Ade Surya Ramadhani") for i in range(len(out)): for j in range(8): print(out[i][j]) print()
def decrypt(self, blockByte, length): self.initQueue() chiperBit = Block(blockByte, Block.BYTE) decrypted = self.chiperEngine.decrypt(self.queue) tk = Toolkit() input = (tk.xorFunction(chiperBit.bit[0], decrypted[0]), tk.xorFunction(chiperBit.bit[1], decrypted[1])) blockBit = Block((input[0], input[1], 16), Block.BIT) blockByte = blockBit.byte self.queue = blockByte return blockByte[:length], length
def testTransposition(self): key = [[int(i == 5) for i in range(8)] for j in range(8)] for i in range(8): print(key[i]) print() input = [[int(i > 0 and j > 0 and i < 7 and j < 7) for i in range(8)] for j in range(8)] tk = Toolkit() out = tk.transposition(key, input) for i in range(8): print(out[i])
def testSubtitution(self): key = [[8 * j + i for i in range(8)] for j in range(8)] for i in range(8): print(key[i]) print() input = [[int(i > 0 and j > 0 and i < 7 and j < 7) for i in range(8)] for j in range(8)] tk = Toolkit() out = tk.subtitution(key, input) for i in range(8): print(out[i])
def testRoundFunction(self): key = [[(8 * j + i + int(i > 0 and j > 0 and i < 7 and j < 7)) % 64 for i in range(8)] for j in range(8)] for i in range(8): print(key[i]) print() input = [[int(i > 0 and j > 0 and i < 7 and j < 7) for i in range(8)] for j in range(8)] tk = Toolkit() out = tk.roundFunction(key, input) for i in range(8): print(out[i])
def encrypt(self, blockByte): self.initQueue() length = len(blockByte) #assertion if length > 16: print("KABOOOMM!!!! Block too big") blockBit = Block(blockByte, Block.BYTE) encrypted = self.chiperEngine.encrypt(self.queue) tk = Toolkit() input = (tk.xorFunction(blockBit.bit[0], encrypted[0]), tk.xorFunction(blockBit.bit[1], encrypted[1])) chiperBit = Block((input[0], input[1], 16), Block.BIT) chiperByte = chiperBit.byte self.queue = chiperByte return chiperByte, length
def decrypt(self, chiperByte, length): #transform into bit matrix chiperBit = Block(chiperByte, Block.BYTE) if (self.firstDecrypt): decrypted = self.chiperEngine.decrypt(chiperBit.bit) blockBit = Block((decrypted[0],decrypted[1], length), Block.BIT) blockByte = blockBit.byte[:length] self.firstDecrypt = False else: decrypted = self.chiperEngine.decrypt(chiperBit.bit) tk = Toolkit() decrypted = (tk.xorFunction(decrypted[0], self.prevChiper[0]), tk.xorFunction(decrypted[1], self.prevChiper[1])) blockBit = Block((decrypted[0], decrypted[1], length), Block.BIT) blockByte = blockBit.byte[:length] self.prevChiper = chiperBit.bit return blockByte, length
def encrypt(self, blockByte): length = len(blockByte) #assertion if length > 16: print("KABOOOMM!!!! Block too big") #transform into bit matrix blockBit = Block(blockByte, Block.BYTE) if (self.firstEncrypt): encrypted = self.chiperEngine.encrypt(blockBit.bit) chiperBit = Block((encrypted[0],encrypted[1],16), Block.BIT) chiperByte = chiperBit.byte self.firstEncrypt = False else: tk = Toolkit() input = (tk.xorFunction(blockBit.bit[0], self.prevChiper[0]), tk.xorFunction(blockBit.bit[1], self.prevChiper[1])) encrypted = self.chiperEngine.encrypt(input) chiperBit = Block((encrypted[0], encrypted[1], 16), Block.BIT) chiperByte = chiperBit.byte self.prevChiper = encrypted return chiperByte, length
def __init__(self, key): tk = Toolkit() self.fn = FeistelNetwork(tk.roundKey(key), tk.roundFunction, tk.xorFunction)