From 9f8924929dcb0f25ae98e3f22cfc826126c86549 Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Thu, 12 Jan 2023 18:05:17 +0100 Subject: [PATCH] refactor rot and zero_width stuff --- crypttool.py | 56 +++++--------------------------------------- string_operations.py | 40 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 50 deletions(-) mode change 100644 => 100755 crypttool.py create mode 100644 string_operations.py diff --git a/crypttool.py b/crypttool.py old mode 100644 new mode 100755 index c0accc4..fd37964 --- a/crypttool.py +++ b/crypttool.py @@ -3,6 +3,8 @@ import sys, string, types, hashlib, base64, re, binascii import operator from collections import Counter +from string_operations import rot, zero_width_string, zero_width_string_decode + LOWER_LETTERS = [chr(x) for x in range(97, 123)] UPPER_LETTERS = [chr(x) for x in range(65, 91)] COMMON_LETTERS = 'ETAOIN SHRDLU' @@ -56,7 +58,6 @@ def xorBrutePrompt(): for i, j in sortedResults: print(i) - def xorSingleBrute(encoded='', maxresults=20, keybytes=1): resultDict = dict() if(encoded == ''): @@ -82,22 +83,6 @@ def fixedxor(string1 = '', string2 = ''): result = hex1 ^ hex2 print(hex(result)) -def rot(inputString, amount): - outputString = '' - amount = int(amount) - for char in inputString: - resultChar = '' - if char.isupper(): - index = UPPER_LETTERS.index(char) - resultChar = UPPER_LETTERS[(index + amount) % len(UPPER_LETTERS)] - elif char.islower(): - index = LOWER_LETTERS.index(char) - resultChar = LOWER_LETTERS[(index + amount) % len(LOWER_LETTERS)] - else: - resultChar = char - outputString += resultChar - return outputString - def translate(inputString, inputType, outputType): result = '' if(inputType == outputType): @@ -113,7 +98,7 @@ def translate(inputString, inputType, outputType): result += byte + ' ' elif(outputType == 2): - result += ord(char) + ' ' + result += str(ord(char)) + ' ' elif(outputType == 3): result += oct(ord(char)) + ' ' elif(outputType == 4): @@ -144,35 +129,6 @@ def translate(inputString, inputType, outputType): result = chr(inputString) return result -def zeroWidthString(inputstring): - resultstring = '>' - #binary = translate(inputstring, 5, 1) - binary = string2bin(inputstring) - binaryArray = split_len(binary, 8) - for byte in binaryArray: - for bit in byte: - if(bit == '1'): - resultstring+= u'\u200b' #zero-width space - else: - resultstring+= u'\u200d' #zero-width joiner - resultstring += '<' - print(resultstring) - -def resolveZeroWidthString(inputstring): - charfound = False - binarystring = '' - resultstring = '' - #inputstring = inputstring.decode('utf-8') - for char in inputstring: - if char == u'\u200b': - binarystring += '1' - elif char == u'\u200d': - binarystring += '0' - bytelist = split_len(binarystring, 8) - for byte in bytelist: - resultstring += translate(byte, 1, 5) - print(resultstring) - def split_len(seq, length): return [seq[i:i+length] for i in range(0, len(seq), length)] @@ -243,7 +199,7 @@ def rotPrompt(): for i in range(0, 26): print(rot(userInput, i)) else: - print(rot(userInput, choice)) + print(rot(userInput, int(choice))) def translatePrompt(): print('1: Binary') @@ -324,10 +280,10 @@ def main(): xorRepeating() elif(choice == '10'): i = input('String:') - zeroWidthString(i) + print(zero_width_string(i)) elif(choice == '11'): i = input('String:') - resolveZeroWidthString(i) + print(zero_width_string_decode(i)) print('Thank you for flying with PankiCrypt Airlines!') diff --git a/string_operations.py b/string_operations.py new file mode 100644 index 0000000..0ed1148 --- /dev/null +++ b/string_operations.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +""" +Functions that modify and return strings. +""" + +import string + +def rot(in_str, n): + ''' + return rot_n of in_str + ''' + return in_str.translate( + in_str.maketrans( + string.ascii_lowercase, + string.ascii_lowercase[n:] + string.ascii_lowercase[:n] + )).translate( + in_str.maketrans( + string.ascii_uppercase, + string.ascii_uppercase[n:] + string.ascii_uppercase[:n] + )) + +def string2bin(in_str): + return ''.join(format(ord(x), 'b').zfill(8) for x in in_str) + +def bin2string(in_str): + return ''.join(chr(int(in_str[_*8:_*8+8], 2)) for _ in range(len(in_str)//8)) + +def zero_width_string(in_str): + ''' + return the ASCII string encoded in non printing spaces + ''' + return ''.join(u'\u200b' if x == '1' else u'\u200d' for x in string2bin(in_str)) + +def zero_width_string_decode(in_str): + return bin2string(''.join('1' if x == '\u200b' else '0' for x in in_str)) + +def main(): + pass +if __name__ == '__main__': + main()