decrypted the cyphertext, basic-decrypt is a mess

This commit is contained in:
Christoph J. Scherr 2023-04-22 12:08:57 +02:00
parent 8b5895c628
commit 314b12c287
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
2 changed files with 51 additions and 8 deletions

View File

@ -1,5 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""
This script is really bad and hacky, don't expect it to do anything.
you also need the chiffrat.txt file in the same directory.
License: MIT
author: Christoph J. Scherr <software@cscherr.de>
"""
MAX_IN_LINE = 16 MAX_IN_LINE = 16
DE_MOST_COMMON = ['E', 'N', 'I', 'R', 'A'] DE_MOST_COMMON = ['E', 'N', 'I', 'R', 'A']
EN_MOST_COMMON = ['E', 'T', 'A', 'O', 'I'] EN_MOST_COMMON = ['E', 'T', 'A', 'O', 'I']
@ -23,7 +31,7 @@ def main():
print('=' * 80) print('=' * 80)
print("Trying to find key from frequencies") print("Trying to find key from frequencies")
print('=' * 80) print('=' * 80)
key = find_key(freq, DE_MOST_COMMON) key = find_key(freq, DE_MOST_COMMON, intarr)
if not key: if not key:
print("The key could not be determined.") print("The key could not be determined.")
if key: if key:
@ -42,7 +50,7 @@ def freq_analysis(intarr: list):
freql[item] += 1 freql[item] += 1
return dict(sorted(freql.items(), key=lambda item: item[1], reverse=True)) return dict(sorted(freql.items(), key=lambda item: item[1], reverse=True))
def find_key(freq: dict, language_reference: list): def find_key(freq: dict, language_reference: list, full_text: list):
# caesar # caesar
caesar_keys = [0] * len(language_reference) caesar_keys = [0] * len(language_reference)
for index, common_char in enumerate(language_reference): for index, common_char in enumerate(language_reference):
@ -61,11 +69,30 @@ def find_key(freq: dict, language_reference: list):
print("Caesar keys ambiguos: %s" % caesar_keys) print("Caesar keys ambiguos: %s" % caesar_keys)
# was not caesar encrypted, continue with XOR checks # was not caesar encrypted, continue with XOR checks
# XOR # XOR
xor_keys = [0] * len(language_reference) #for xor_key in range(0, 2**7):
for index, common_char in enumerate(language_reference): # decrypt = bytes(xor_key ^ common_char for common_char in full_text)
# this is probably garbage # try:
item = list(freq.values())[index] # print(("decry for kex %x:\t%s" % (xor_key, decrypt.decode(errors="backslashreplace"))).replace("\n", ""))
xor_keys[index] = item ^ ord(common_char) # except:
# print(("decry for kex %x:\t%s" % (xor_key, decrypt.hex())).replace("\n", ""))
# -> manual review of the output of the above confirmed that the key for my cyphertext is 0x15
THE_KEY = 0x15
# dump the text:
decrypted: list = []
for c in full_text:
decrypted.append((c ^ THE_KEY))
print("dump of text repr:")
output = ""
for c in decrypted:
output += chr(c)
print(output)
print("dumping decrypted with 0x15")
print(dump_intarr(decrypted))
exit()
return THE_KEY
all_same = False all_same = False
for i in range(1, len(xor_keys)): for i in range(1, len(xor_keys)):
@ -79,6 +106,8 @@ def find_key(freq: dict, language_reference: list):
# ???????? # ????????
print("xor keys ambiguos: %s" % xor_keys) print("xor keys ambiguos: %s" % xor_keys)
return False
def dump_frequencies(frequencies, chars_in_text: int = -1, print_top_x = 5): def dump_frequencies(frequencies, chars_in_text: int = -1, print_top_x = 5):
for i in range(print_top_x): for i in range(print_top_x):

View File

@ -7,6 +7,7 @@ version control at: https://git.cscherr.de/PlexSheep/python-dhbw/src/branch/mast
License: MIT License: MIT
""" """
import math import math
import argparse
import random import random
DEFINED_INITIAL = bytearray(b'\xa5\xa5\xa5\xa5\x5a\x5a\x5a\x5a\x55\x55\x55\x55\xaa\xaa\xaa\xaa') DEFINED_INITIAL = bytearray(b'\xa5\xa5\xa5\xa5\x5a\x5a\x5a\x5a\x55\x55\x55\x55\xaa\xaa\xaa\xaa')
@ -93,8 +94,21 @@ def first_preimage():
trash_hash(input).hex(), trash_hash(THE_HASH_ORIGIN).hex()) trash_hash(input).hex(), trash_hash(THE_HASH_ORIGIN).hex())
def main(): def main():
first_preimage() parser = argparse.ArgumentParser(prog="trash hash", description='implements a bad hash and shows how to break it. No option for preimage attack, --hash to get a hash.')
parser.add_argument('--hash', type=str,
help='an input that should be hashed')
args = parser.parse_args()
if args.hash:
my_bytes: bytearray = bytearray(str.encode(args.hash))
hashed = trash_hash(my_bytes)
print("hash for \"%s\" is:\n%s" % (args.hash, hashed.hex()))
else:
first_preimage()
"""
Don't use this, too inefficient
"""
def bruteForce() -> bool: def bruteForce() -> bool:
payload_a = bytearray(b"AAAA") payload_a = bytearray(b"AAAA")
foundCollision = False foundCollision = False