Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Encoded Text Decyphering Project
- # The encryption process is done like this. Each letter in the alphabet is given a number based on its offset from "a",
- # plus some additional offset. So for example if the additional offset is 10, then 'a' is numbered 10, 'b' is 11 etc.
- # The resulting number is doubled. Each character in an odd position within the text, is replaced with this 2-digit number.
- # Then each character at an even position within the text, is similary replaced by the 2-digit number, but these are inserted in reverse order,
- # so for example for an 8-character piece of text, the 2nd character is replaced with the number derived from the 8th character in the text,
- # the 3rd is replaced by the number from the 6th character etc.
- intercept = "1242263422322626121816404638362648184618341226364644" # Encoded text
- LetterDistr = "?qjzxvkwyfbghmpduclsntoirae" # letter distribution in English. Right characters are commoner
- fwd = []
- back = []
- # build two lists, one with odd numbered pairs going forwards, and 2nd with even numbered pairs going backwards
- for x in range(0, len(intercept), 4):
- f = intercept[x: x+2]
- b = intercept[x+2: x + 4]
- fwd.append(int(f) // 2) # divide by 2
- back = [int(b) // 2] + back # arrange integers backwards
- combo = []
- # Combine the 2 lists
- for x in range(len(fwd)):
- combo.append(fwd[x])
- combo.append(back[x])
- bestscore = 0
- bestoffset = 0
- bestresult = ""
- # For each possible offset 0 thru 25, decode the test, and decide which offset gives an output most like English
- for offset in range(26):
- res = ""
- score = 0
- for i in combo:
- i -= offset
- if i < 0: # wrap around the alphabet
- i += 26
- ch = chr(i + ord('a')) # add offset to the ascii value of 'a'
- res += ch
- if ch in LetterDistr:
- score += LetterDistr.index(ch) # accumulate a score based on letter frequency in English
- if score > bestscore:
- bestscore = score
- bestoffset = offset
- bestresult = res
- print(f"The best offset was {bestoffset} which gave the following text\n{bestresult}")
- # Output:-
- #
- # The best offset was 5 which gave the following text
- # bringbiebedisonptesimlimsq
Add Comment
Please, Sign In to add comment