ctf decryptor

This commit is contained in:
tabledevil
2019-10-17 23:30:20 +02:00
parent 707ad160a6
commit 531dc32af7

View File

@@ -1,8 +1,9 @@
import pprint
import math
# secret="OUHRSTHFSOENOFETURFELIRFTSNEMOEEMELNTARETOKCAETBFIHFTTTNMEELEEOHYBAERORCRSEDNCEUUTHITOYRSTEDSBEIEOTNLRMOEFPOHHAYLAGXYISNIARAUABGBURILFERPEEHTECDINNDITHFFIEHTKESYTDHEREOALGNABSMWEHVEFSOAMETAOCRFTAHEOFSINAMEOTRNGRINTHFFIEHTIEGMELNTSTEOMCOHEOWTEWREAIDANHTRARARTEHEETVFIYREAHVSAONDPROSTRAEUOYCTTTHWISANMUHETENTIISEDHETSUSENTEITNG OOLEEB L"
# first_col_key="EJALMVWUSTRPOBY" # missing 1 char
# second_row_key="GHPTYPAMTAPQRNDHD" # missing 4 chars one of which is 'D'
secret="OUHRSTHFSOENOFETURFELIRFTSNEMOEEMELNTARETOKCAETBFIHFTTTNMEELEEOHYBAERORCRSEDNCEUUTHITOYRSTEDSBEIEOTNLRMOEFPOHHAYLAGXYISNIARAUABGBURILFERPEEHTECDINNDITHFFIEHTKESYTDHEREOALGNABSMWEHVEFSOAMETAOCRFTAHEOFSINAMEOTRNGRINTHFFIEHTIEGMELNTSTEOMCOHEOWTEWREAIDANHTRARARTEHEETVFIYREAHVSAONDPROSTRAEUOYCTTTHWISANMUHETENTIISEDHETSUSENTEITNG OOLEEB L"
col_key="EJALMVWUSTRPOBY_" # (16)missing 1 char
row_key="GHPTYPAMTAPQRNDHD____" # (21) missing 4 chars one of which is 'D'
# KLINGON_ALPHABET="ABDEHIJLMNOPQRSTUVWY"
# HIERACH_ALPHABET="ABDFGHIJKMNPQRSTUWYZ"
# cleartext="ABDEFGHIJKLMOPQRSTUVWXYZ"
@@ -10,36 +11,97 @@ import math
# my_row_key="HALLOABBIEGALE"
def rows(text,row_key):
key_length=len(row_key)
row_length=math.ceil(len(text)/key_length)
rows=[text[i:i+key_length] for i in range(0,len(text),key_length)]
return mosh(rows,row_key)
def cols(text,col_key):
key_length=len(col_key)
col_length=math.ceil(len(text)/key_length)
cols=[ "" for char in col_key ]
cursor=0
for c in text:
cols[cursor%key_length]+=c
cursor += 1
return cols
#As we know from the cold wars in the galaxy with the KLI, the Mondoshiva used the double transposition cipher with first/inner column and second/outer row transformation for encryption to secure the most powerful information. Furthermore, they usually used the longer keys for the rows
def get_index_key(key):
def cell_length(text_length,key_length):
return math.ceil(text_length/key_length)
def padded_length(text_length,key_length):
return cell_length(text_length,key_length)*key_length
def revert_key(key):
return [x[0] for x in sorted(enumerate(key), key=lambda x: x[1])]
def mosh(text,key):
tmp=sorted(zip(text,key), key=lambda x: x[1])
return [x[0] for x in tmp]
def cols_encode(text,cols_key):
return mosh(cols(text,cols_key),cols_key)
#def cols_2_rows
def cols(text,key_length):
# col_length=cell_length(len(text),key_length)
columns=[ "" for i in range(0,key_length) ]
cursor=0
for c in text:
columns[cursor%key_length]+=c
cursor += 1
return columns
def cols_decode(text,cols_key):
rows=rows(text)
reorderd=mosh(rows,cols_key)
return reorderd
def rows(text,key_length):
# row_length=math.ceil(len(text)/key_length)
rows=[text[i:i+key_length] for i in range(0,len(text),key_length)]
return rows
def cols_to_str(a):
max_length=max([len(i) for i in a] )
result=""
for i in range(0,max_length):
for x in a:
try:
result+=x[i]
except:
pass
return result
def rows_to_str(a):
return "".join(a)
def pcols(a):
print("COLUMS:")
text=cols_to_str(a)
split_text=rows(text,len(a))
for x in split_text:
print(x)
def prows(a,header=None):
print("ROWS:")
counter=0
for x in a:
if header:
heading="{}".format(header[counter]).ljust(5)
else:
heading="{}".format(counter).ljust(5)
counter+=1
print("%s : %s"%(heading,x))
def encode(text,key):
text=text.ljust(math.ceil(len(text)/len(key))*len(key),'_')
print("encoding <%s>"%text)
columnized_text=cols(text,len(key))
pcols(columnized_text)
shuffled_colums=mosh(columnized_text,key)
pcols(shuffled_colums)
return rows_to_str(shuffled_colums)
def decode(text,key):
print("decoding <%s>"%text)
row_data=rows(text,cell_length(len(text), len(key)))
prows(row_data)
reorderd=mosh(row_data,revert_key(key))
prows(reorderd)
return cols_to_str(reorderd)
def decross(text,key_rows,key_cols):
#revert row transformation
matrix_cells=len(key_rows)*len(key_cols)
if len(text) != matrix_cells:
print("!!TEXT HAD TO BE PADDED!!")
text=text.ljust(matrix_cells,'_')
#generate rows with a length of the column-key
matrix=rows(text,len(key_cols))
prows(matrix,key_rows)
decross(secret,row_key,col_key)