refactor rot and zero_width stuff

master
Felix Pankratz 2 years ago
parent 71b3fd1697
commit 9f8924929d

@ -3,6 +3,8 @@ import sys, string, types, hashlib, base64, re, binascii
import operator import operator
from collections import Counter 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)] LOWER_LETTERS = [chr(x) for x in range(97, 123)]
UPPER_LETTERS = [chr(x) for x in range(65, 91)] UPPER_LETTERS = [chr(x) for x in range(65, 91)]
COMMON_LETTERS = 'ETAOIN SHRDLU' COMMON_LETTERS = 'ETAOIN SHRDLU'
@ -56,7 +58,6 @@ def xorBrutePrompt():
for i, j in sortedResults: for i, j in sortedResults:
print(i) print(i)
def xorSingleBrute(encoded='', maxresults=20, keybytes=1): def xorSingleBrute(encoded='', maxresults=20, keybytes=1):
resultDict = dict() resultDict = dict()
if(encoded == ''): if(encoded == ''):
@ -82,22 +83,6 @@ def fixedxor(string1 = '', string2 = ''):
result = hex1 ^ hex2 result = hex1 ^ hex2
print(hex(result)) 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): def translate(inputString, inputType, outputType):
result = '' result = ''
if(inputType == outputType): if(inputType == outputType):
@ -113,7 +98,7 @@ def translate(inputString, inputType, outputType):
result += byte + ' ' result += byte + ' '
elif(outputType == 2): elif(outputType == 2):
result += ord(char) + ' ' result += str(ord(char)) + ' '
elif(outputType == 3): elif(outputType == 3):
result += oct(ord(char)) + ' ' result += oct(ord(char)) + ' '
elif(outputType == 4): elif(outputType == 4):
@ -144,35 +129,6 @@ def translate(inputString, inputType, outputType):
result = chr(inputString) result = chr(inputString)
return result 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): def split_len(seq, length):
return [seq[i:i+length] for i in range(0, 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): for i in range(0, 26):
print(rot(userInput, i)) print(rot(userInput, i))
else: else:
print(rot(userInput, choice)) print(rot(userInput, int(choice)))
def translatePrompt(): def translatePrompt():
print('1: Binary') print('1: Binary')
@ -324,10 +280,10 @@ def main():
xorRepeating() xorRepeating()
elif(choice == '10'): elif(choice == '10'):
i = input('String:') i = input('String:')
zeroWidthString(i) print(zero_width_string(i))
elif(choice == '11'): elif(choice == '11'):
i = input('String:') i = input('String:')
resolveZeroWidthString(i) print(zero_width_string_decode(i))
print('Thank you for flying with PankiCrypt Airlines!') print('Thank you for flying with PankiCrypt Airlines!')

@ -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()
Loading…
Cancel
Save