Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- class diFrequency:
- def getFrequencies(self, message):
- messageLength = len(message)/2
- digraphdicDictionary = {}
- for i in range(0, len(message), 2):
- digraphdic = message[i:i+2]
- if digraphdic not in digraphdicDictionary:
- digraphdicDictionary[digraphdic] = 1
- else:
- digraphdicDictionary[digraphdic] += 1
- numberdigraphdics = len(message)/2.0
- for digraphdic, count in digraphdicDictionary.items():
- digraphdicDictionary[digraphdic] = round((count / numberdigraphdics) * 100, 2)
- frequencies = digraphdicDictionary.items()
- return sorted(frequencies, key=lambda x: -x[1])
- def getdigraphdicFrequencies(self):
- 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)]
- diFreq = diFrequency()
- testmessage = "KFHYY GIGMC EJSST EBOEU GRWJT SDVYK ZOZLI ZKFHX KUUIC WXFWJ GAXQP BQAGV"
- testmessage +="GXDVD GUEVG MIGYK QQPIP SCLLF YPMUL KFHXP MHGME VDKAV YQCEG UEALY YYZSZ MPXZO CTXTR IMDID"
- testmessage +="VDGSX OZFFT SMEDV MEIMD VMPKO UJKOD UBOAX BOORS LPZCW IMDVY GJWMI FQ"
- testmessage = testmessage.replace(' ', '')
- print (testmessage)
- print ("Ciphertext digraphdic frequencies:\n%s\n" % diFreq.getFrequencies(testmessage))
- print ("Common digraphdic frequencies:\n%s\n" % diFreq.getdigraphdicFrequencies())
- #KFHYYGIGMCEJSSTEBOEUGRWJTSDVYKZOZLIZKFHXKUUICWXFWJGAXQPBQAGVGXDVDGUEVGMIGYKQQPIPSCLLFYPMULKFHXPMHGMEVDKAVYQCEGUEALYYYZSZMPXZOCTXTRIMDI#DVDGSXOZFFTSMEDVMEIMDVMPKOUJKODUBOAXBOORSLPZCWIMDVYGJWMIFQ
- #Ciphertext digraphdic frequencies:
- #[('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)]
- #Common digraphdic frequencies:
- #[('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)]
- alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- len(alphabet)
- #26
- alphaDict = {}
- for i, letter in enumerate(alphabet):
- alphaDict[letter] = i
- print(alphaDict)
- #{'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}
- #In [24]:
- def findtransform(encryptpair,decryptpair):
- p1=alphaDict[decryptpair[0]]
- p2=alphaDict[decryptpair[1]]
- c1=alphaDict[encryptpair[0]]
- c2=alphaDict[encryptpair[1]]
- print(p1,p2,c1,c2)
- findtransform('DV','TH')
- #19 7 3 21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement