Refactor as function.

master
jowj 6 years ago
parent 784873a00d
commit 018a5344dd

@ -1,33 +1,38 @@
# single-byte xor cipher # single-byte xor cipher
from binascii import unhexlify from binascii import unhexlify
hexed = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
unHexed = unhexlify(hexed)
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] def xor_SingleByte(hexed):
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
# generate a dict of possible answers and their decryption keys unHexed = unhexlify(hexed)
tableOfPossibleAnswers = {} # generate a dict of possible answers and their decryption keys
for code in range(256):
possibleAnswer = ''.join(chr(byte ^ code) for byte in unHexed) tableOfPossibleAnswers = {}
if possibleAnswer.isprintable(): for code in range(256):
tableOfPossibleAnswers[code] = possibleAnswer possibleAnswer = ''.join(chr(byte ^ code) for byte in unHexed)
if possibleAnswer.isprintable():
# weight the answers using a simple english alphabet list tableOfPossibleAnswers[code] = possibleAnswer
weightedCodes = {}
for key,value in tableOfPossibleAnswers.items(): # weight the answers using a simple english alphabet list
weightedCodes[value] = 0 weightedCodes = {}
for letter in alphabet: for key,value in tableOfPossibleAnswers.items():
if letter in value: weightedCodes[value] = 0
weightedCodes[value] += 1 for letter in alphabet:
if letter in value:
# find the highest value and return it and its corresponding key weightedCodes[value] += 1
maxValue = max(weightedCodes.values())
answerValue = [] # find the highest value and return it and its corresponding key
for key in weightedCodes: maxValue = max(weightedCodes.values())
if weightedCodes[key] == maxValue: answerValue = []
answerValue = key for key in weightedCodes:
if weightedCodes[key] == maxValue:
# now i need to use that to key into the original tableOfPossibleAnswers to return the answer and the decryption key. answerValue = key
for key,value in tableOfPossibleAnswers.items(): # now i need to use that to key into the original tableOfPossibleAnswers to return the answer and the decryption key.
if tableOfPossibleAnswers[key] == answerValue:
print(key, tableOfPossibleAnswers[key]) for key,value in tableOfPossibleAnswers.items():
if tableOfPossibleAnswers[key] == answerValue:
print(key, tableOfPossibleAnswers[key])
xor_SingleByte("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
Loading…
Cancel
Save