Advertisement
UF6

3.3 Problem 3/5

UF6
Jul 12th, 2016
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. import string
  2.  
  3. class diFrequency:
  4.    
  5.     def getFrequencies(self, message):
  6.         messageLength = len(message)/2
  7.        
  8.         digraphdicDictionary = {}
  9.         for i in range(0, len(message), 2):
  10.             digraphdic = message[i:i+2]
  11.             if digraphdic not in digraphdicDictionary:
  12.                 digraphdicDictionary[digraphdic] = 1                
  13.             else:
  14.                 digraphdicDictionary[digraphdic] += 1
  15.            
  16.        
  17.         numberdigraphdics = len(message)/2.0
  18.        
  19.        
  20.         for digraphdic, count in digraphdicDictionary.items():
  21.             digraphdicDictionary[digraphdic] = round((count / numberdigraphdics) * 100, 2)
  22.        
  23.         frequencies = digraphdicDictionary.items()
  24.        
  25.         return sorted(frequencies, key=lambda x: -x[1])
  26.        
  27.     def getdigraphdicFrequencies(self):
  28.            
  29.             return [('TH', 1.52), ('HE', 1.28), ('IN', 0.94), ('ER', 0.94), ('AN', 0.82), ('RE', 0.68), ('ND', 0.63), ('AT', 0.59), ('ON', 0.57)]
  30.  
  31. diFreq = diFrequency()
  32.  
  33. testmessage = "KFHYY GIGMC EJSST EBOEU GRWJT SDVYK ZOZLI ZKFHX KUUIC WXFWJ GAXQP BQAGV"
  34. testmessage +="GXDVD GUEVG MIGYK QQPIP SCLLF YPMUL KFHXP MHGME VDKAV YQCEG UEALY YYZSZ MPXZO CTXTR IMDID"
  35. testmessage +="VDGSX OZFFT SMEDV MEIMD VMPKO UJKOD UBOAX BOORS LPZCW IMDVY GJWMI FQ"
  36. testmessage = testmessage.replace(' ', '')
  37.  
  38. print (testmessage)
  39. print ("Ciphertext digraphdic frequencies:\n%s\n" % diFreq.getFrequencies(testmessage))
  40. print ("Common digraphdic frequencies:\n%s\n" % diFreq.getdigraphdicFrequencies())
  41.  
  42. #KFHYYGIGMCEJSSTEBOEUGRWJTSDVYKZOZLIZKFHXKUUICWXFWJGAXQPBQAGVGXDVDGUEVGMIGYKQQPIPSCLLFYPMULKFHXPMHGMEVDKAVYQCEGUEALYYYZSZMPXZOCTXTRIMDI#DVDGSXOZFFTSMEDVMEIMDVMPKOUJKODUBOAXBOORSLPZCWIMDVYGJWMIFQ
  43. #Ciphertext digraphdic frequencies:
  44. #[('DV', 6.25), ('KF', 3.12), ('ME', 3.12), ('IM', 3.12), ('BO', 3.12), ('PM', 2.08), ('KO', 2.08), ('DG', 2.08), ('MP', 2.08), ('TS', 2.08), ('UE', 2.08), ('CW', 2.08), ('HX', 2.08), ('YG', 2.08), ('MI', 2.08), ('WJ', 2.08), ('GR', 1.04), ('IG', 1.04), ('XF', 1.04), #('SC', 1.04), ('KQ', 1.04), ('UL', 1.04), ('KU', 1.04), ('ZO', 1.04), ('GY', 1.04), ('FF', 1.04), ('FQ', 1.04), ('EG', 1.04), ('HG', 1.04), ('VG', 1.04), ('SZ', 1.04), ('VD', 1.04), ('DI', 1.04), ('UI', 1.04), ('ZL', 1.04), ('KA', 1.04), ('YY', 1.04), ('SX', 1.04), #('PB', 1.04), ('PZ', 1.04), ('TX', 1.04), ('AL', 1.04), ('UJ', 1.04), ('TR', 1.04), ('GA', 1.04), ('OR', 1.04), ('IP', 1.04), ('HY', #1.04), ('QC', 1.04), ('AX', 1.04), ('OC', 1.04), ('EU', 1.04), ('GV', 1.04), ('SS', 1.04), ('XQ', 1.04), ('QP', 1.04), ('FY', 1.04), #('XZ', 1.04), ('OZ', 1.04), ('TE', 1.04), ('GX', 1.04), ('DU', 1.04), ('JW', 1.04), ('MC', 1.04), ('VY', 1.04), ('LL', 1.04), ('YK', #1.04), ('EJ', 1.04), ('QA', 1.04), ('YZ', 1.04), ('SL', 1.04), ('IZ', 1.04)]
  45.  
  46. #Common digraphdic frequencies:
  47. #[('TH', 1.52), ('HE', 1.28), ('IN', 0.94), ('ER', 0.94), ('AN', 0.82), ('RE', 0.68), ('ND', 0.63), ('AT', 0.59), ('ON', 0.57)]
  48.  
  49. alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  50. len(alphabet)
  51. #26
  52.  
  53. alphaDict = {}
  54. for i, letter in enumerate(alphabet):
  55.     alphaDict[letter] = i
  56.  
  57. print(alphaDict)
  58.  
  59. #{'M': 12, 'R': 17, 'U': 20, 'F': 5, 'S': 18, 'O': 14, 'A': 0, 'T': 19, 'W': 22, 'N': 13, 'B': 1, 'Q': 16, 'X': 23, 'Z': 25, 'H': 7, #'Y': 24, 'K': 10, 'E': 4, 'D': 3, 'I': 8, 'L': 11, 'J': 9, 'G': 6, 'V': 21, 'P': 15, 'C': 2}
  60. #In [24]:
  61.  
  62. def findtransform(encryptpair,decryptpair):
  63.     p1=alphaDict[decryptpair[0]]
  64.     p2=alphaDict[decryptpair[1]]
  65.     c1=alphaDict[encryptpair[0]]
  66.     c2=alphaDict[encryptpair[1]]
  67.     print(p1,p2,c1,c2)
  68. findtransform('DV','TH')
  69. #19 7 3 21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement