Пример #1
0
def encryptFeistel(key, block):
    key = key.to_bytes(6, byteorder='big')
    lengthDiff = len(key) - len(block)
    if lengthDiff > 0:
        key = key[lengthDiff:]
    keyInt = []
    for el in key:
        keyInt.append(el)
    return operations.xorBlock(keyInt, block)
Пример #2
0
def encrypt(key, block, encryptFunction):
    internalKeys = generateRoundKeys(key)
    li = block[:len(block) // 2]
    ri = block[len(block) // 2:]
    for i in range(16):
        oldRi = ri
        ri = operations.xorBlock(li, encryptFunction(internalKeys[i], ri))
        li = oldRi
    return li + ri
Пример #3
0
def decrypt(key, block, decryptFunction):
    internalKeys = generateRoundKeys(key)
    internalKeys = internalKeys[::-1]
    li = block[:len(block) // 2]
    ri = block[len(block) // 2:]
    for i in range(16):
        oldLi = li
        li = operations.xorBlock(ri, decryptFunction(internalKeys[i], li))
        ri = oldLi
    return li + ri
Пример #4
0
def encrypt(key, blocks, encryptFunction):
    cipherBlocks = []
    blockSize = len(blocks[0])
    ctr = 0
    for i, block in enumerate(blocks):
        ctrBlock = ctr.to_bytes(blockSize, byteorder='big')
        encryptedCtrBlock = encryptFunction(key[i], ctrBlock)
        cipherBlock = operations.xorBlock(encryptedCtrBlock, block)
        cipherBlocks.append(cipherBlock)
        ctr += 1
    return cipherBlocks
Пример #5
0
def switchKey(key, block):
    return operations.xorBlock(key, block)