Ejemplo n.º 1
0
    def testConstructor(self):
        value1 = bytearray(b'CendhikaImantoro')
        block1 = Block(value1, Block.BYTE)
        print("Block 1")
        print("length : ", block1.length)
        print("byte : ", bytes(block1.byte))
        for i in range(8):
            print(block1.bit[0][i], "      ", block1.bit[1][i])

        block2 = Block((block1.bit[0], block1.bit[1], block1.length),
                       Block.BIT)

        print("Block 2")
        print("length : ", block2.length)
        print("byte : ", bytes(block2.byte))
        for i in range(8):
            print(block2.bit[0][i], "      ", block2.bit[1][i])

        value1 = bytearray(b'Ade Surya R')
        block1 = Block(value1, Block.BYTE)
        print("Block 1")
        print("length : ", block1.length)
        print("byte : ", bytes(block1.byte))
        for i in range(8):
            print(block1.bit[0][i], "      ", block1.bit[1][i])

        block2 = Block((block1.bit[0], block1.bit[1], block1.length),
                       Block.BIT)

        print("Block 2")
        print("length : ", block2.length)
        print("byte : ", bytes(block2.byte))
        for i in range(8):
            print(block2.bit[0][i], "      ", block2.bit[1][i])
Ejemplo n.º 2
0
    def decrypt(self, chiperByte, length):

        #transform into bit matrix
        chiperBit = Block(chiperByte, Block.BYTE)

        decrypted = self.chiperEngine.decrypt(chiperBit.bit)
        blockBit = Block((decrypted[0], decrypted[1], length), Block.BIT)
        blockByte = blockBit.byte[:length]

        return blockByte
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
    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)

        encrypted = self.chiperEngine.encrypt(blockBit.bit)
        chiperBit = Block((encrypted[0], encrypted[1], 16), Block.BIT)
        chiperByte = chiperBit.byte

        return chiperByte, length
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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