acclivity

pyJustinBieberDecryptionExercise

May 18th, 2022 (edited)
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. # Encoded Text Decyphering Project
  2.  
  3. # The encryption process is done like this. Each letter in the alphabet is given a number based on its offset from "a",
  4. # plus some additional offset. So for example if the additional offset is 10, then 'a' is numbered 10, 'b' is 11 etc.
  5. # The resulting number is doubled. Each character in an odd position within the text, is replaced with this 2-digit number.
  6. # 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,
  7. # 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,
  8. # the 3rd is replaced by the number from the 6th character etc.
  9.  
  10. intercept = "1242263422322626121816404638362648184618341226364644"          # Encoded text
  11.  
  12. LetterDistr = "?qjzxvkwyfbghmpduclsntoirae"         # letter distribution in English. Right characters are commoner
  13.  
  14. fwd = []
  15. back = []
  16. # build two lists, one with odd numbered pairs going forwards, and 2nd with even numbered pairs going backwards
  17. for x in range(0, len(intercept), 4):
  18.     f = intercept[x: x+2]
  19.     b = intercept[x+2: x + 4]
  20.     fwd.append(int(f) // 2)         # divide by 2
  21.     back = [int(b) // 2] + back     # arrange integers backwards
  22.  
  23. combo = []
  24. # Combine the 2 lists
  25. for x in range(len(fwd)):
  26.     combo.append(fwd[x])
  27.     combo.append(back[x])
  28.  
  29. bestscore = 0
  30. bestoffset = 0
  31. bestresult = ""
  32. # For each possible offset 0 thru 25, decode the test, and decide which offset gives an output most like English
  33. for offset in range(26):
  34.     res = ""
  35.     score = 0
  36.     for i in combo:
  37.         i -= offset
  38.         if i < 0:                   # wrap around the alphabet
  39.             i += 26
  40.         ch = chr(i + ord('a'))      # add offset to the ascii value of 'a'
  41.         res += ch
  42.         if ch in LetterDistr:
  43.             score += LetterDistr.index(ch)      # accumulate a score based on letter frequency in English
  44.     if score > bestscore:
  45.         bestscore = score
  46.         bestoffset = offset
  47.         bestresult = res
  48. print(f"The best offset was {bestoffset} which gave the following text\n{bestresult}")
  49.  
  50. # Output:-
  51. #
  52. # The best offset was 5 which gave the following text
  53. # bringbiebedisonptesimlimsq
  54.  
  55.  
  56.  
  57.  
  58.  
Add Comment
Please, Sign In to add comment