|
|
@ -1,3 +1,4 @@ |
|
|
|
|
|
|
|
import math |
|
|
|
from lib.input import * |
|
|
|
from lib.input import * |
|
|
|
from lib.vars import alphabet |
|
|
|
from lib.vars import alphabet |
|
|
|
from lib.vars import listToString |
|
|
|
from lib.vars import listToString |
|
|
@ -19,15 +20,54 @@ def keyToIntTuple(key): |
|
|
|
|
|
|
|
|
|
|
|
def applyKeyToBlock(keyT, blockL): # function to apply a key to a block |
|
|
|
def applyKeyToBlock(keyT, blockL): # function to apply a key to a block |
|
|
|
blockDe = [""] * blockSize |
|
|
|
blockDe = [""] * blockSize |
|
|
|
|
|
|
|
if(in_mode == 0): |
|
|
|
|
|
|
|
for i in range(blockSize): |
|
|
|
|
|
|
|
blockDe[i] = blockL[keyT[i]] # encrypting |
|
|
|
|
|
|
|
elif(in_mode == 1): |
|
|
|
|
|
|
|
for i in range(blockSize): |
|
|
|
|
|
|
|
blockDe[keyT[i]] = blockL[i] # decrypting |
|
|
|
|
|
|
|
|
|
|
|
for i in range(blockSize): |
|
|
|
|
|
|
|
blockDe[i] = blockL[keyT[i]] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(blockDe) |
|
|
|
|
|
|
|
return blockDe |
|
|
|
return blockDe |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def splitInput(inputL, n): |
|
|
|
|
|
|
|
out = [] |
|
|
|
|
|
|
|
inputLen = len(inputL) |
|
|
|
|
|
|
|
x = inputLen / float(n) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last = 0.0 |
|
|
|
|
|
|
|
while last < inputLen: |
|
|
|
|
|
|
|
out.append( inputL[ int(last):int(last + x) ] ) |
|
|
|
|
|
|
|
last += x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getOutput(blocksL): |
|
|
|
|
|
|
|
out = "" |
|
|
|
|
|
|
|
for block in blocksL: |
|
|
|
|
|
|
|
out += listToString(block) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# Do the encrypting/decrypting stuff with the input |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
TXT = list(in_txt) |
|
|
|
KEY = keyToIntTuple(in_key) # define and make the key a tuple so that we can index it |
|
|
|
KEY = keyToIntTuple(in_key) # define and make the key a tuple so that we can index it |
|
|
|
|
|
|
|
|
|
|
|
applyKeyToBlock(keyToIntTuple(in_key), list(in_txt)) |
|
|
|
amountBlocks = len(TXT) / blockSize |
|
|
|
|
|
|
|
if( math.floor(amountBlocks) != math.ceil(amountBlocks) ): |
|
|
|
|
|
|
|
print("Error: Input didn't match key size.") |
|
|
|
|
|
|
|
print("amountBlocks: " + str(amountBlocks)) |
|
|
|
|
|
|
|
exit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blocks = splitInput(TXT, amountBlocks) |
|
|
|
|
|
|
|
BlocksDe = [None] * len(Blocks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
count = -1 |
|
|
|
|
|
|
|
for block in Blocks: |
|
|
|
|
|
|
|
count += 1 |
|
|
|
|
|
|
|
BlocksDe[count] = applyKeyToBlock(KEY, block) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Finally print the output |
|
|
|
|
|
|
|
print( getOutput(BlocksDe) ) |
|
|
|