def enc(): # Get the text T = ptext.get("1.0", "end")[:-1] S = stego.get("1.0", "end")[:-1] # Get the selected cipher C = code.get() # Clean the text provided # If we are using ASCII the true message doesn't have to be modified S = preptext(S, silent=True) if C != "ASCII": T = preptext(T, silent=True) # We use a dictionary as basically a as a switch statement # They keys are the names of the cipher while the values are the cipher # functions that we imported codeDict = { "morse": binaryMorseCode, "bacon": baconCode, "prefix": prefixCode, "ASCII": ASCII } # Blank the ctext box then put the text in it ctext.delete("1.0", "end") try: ctext.insert("insert", baconCipher(T, codeDict[C], S, decode=False)) except Exception as e: ctext.insert("insert", str(e))
def vigenereCrackerExample(): textfile = open( 'C:\\Users\\Alexander\\Documents\\GitHub\\ClassicCrypto\\SampleText\\text1.txt', 'r') ptext = preptext(textfile.readline(), silent=True) ctext = vigenere(ptext, "ZEBRAS") vigenereCracker(ctext)
def columnarTransportCrackerExample(): print(""" For single columnar transport the length of the key must be a factor of the length of the ciphertext. This brute force attack first calculates those factors then tries every possible key for each of those lengths. This is not practical if the length of the key is more than about nine. """) textfile = open( 'C:\\Users\\Alexander\\Documents\\GitHub\\ClassicCrypto\\SampleText\\text1.txt', 'r') ptext = preptext(textfile.readline(), silent=False) ctext = columnarTransport(ptext, "MYSTIC") columnarTransportCracker(ctext)
def wordCodeExample(): textfile = open( 'C:\\Users\\Alexander\\Documents\\GitHub\\ClassicCrypto\\SampleText\\Text3.txt', 'r') ptext = "" for i in textfile.readlines(): ptext += preptext(i, keepSpaces=True, silent=True) + " " ptext = ptext[:1113] print("\n") print(ptext) print(len(ptext)) print("\n") ctext = wordCode(ptext) print(ctext) print(len(ctext))
def hillCipherCrackerExample(): print(""" The simple version of the Hill Cipher is very hard to break with access only to the ciphertext. In order to attack it some piece of text from the plaintext, known as a "crib", is needed. Here we will try to break the 3x3 version of the cipher using a few different cribs. In order to make this work we have to try the crib in many positions and do a relatively large amount of calculation. As a result we will try our attack on just the first 600 characters. If that fails we can try with the next section. The minimum crib length is 9 but that will only work if the crib perfectly lines up with a block. To ensure that we can decrypt the crib must have a length of 17. A crib that long is not easy to get. Suppose that we known this text is about sugar cane and we know the author has the particular oddity of calling mollases "saccharine juice" from other things we have seen written. We will try the following likely phrases: THEWESTINDIES THEISLANDS THEMOLASSES THESUGARCANE SACCHARINEJUICE """) # Load up the text to use textfile = open( 'C:\\Users\\Alexander\\Documents\\GitHub\\ClassicCrypto\\SampleText\\text1.txt', 'r') ptext = preptext(textfile.readline(), silent=True) ptext = ptext[:600] key = createMatrixKey(3) ctext = hillCipher(ptext, key) print("Cipher Text:") print(ctext, "\n\n") cribs = [ "THEWESTINDIES", "THEISLANDS", "THEMOLASSES", "THESUGARCANE", "SACCHARINEJUICE" ] for crib in cribs: print("Using the crib: {}\n".format(crib)) hillCipherCracker(ctext, crib, 3) print("\n\n")
def frequencyTableExample(): from Ciphers.UtilityFunctions import preptext textfile = open('C:\\Users\\Alexander\\Documents\\GitHub\\ClassicCrypto\\SampleText\\text1.txt', 'r') ptext = preptext(textfile.readline(),silent=True) print(""" Example of frequency analysis of a body of text. Lets see what the most common letters are. Then the most common bigrams and trigrams. """) for i,title in zip(range(1,4),["Letters","Bigrams","Trigrams"]): print("Most Common {}".format(title)) tab = frequencyTable(ptext,n=i) T = tab[:7] for fr in T: print(fr[0]," ",fr[1]) print() #frequencyTableExample()
def enc(): # Get the text from the ptext box T = ptext.get("1.0", "end")[:-1] T = preptext(T) # Get the key from the key box K = key.get("1.0", "end")[:-1] # Blank the ctext and code groups boxes ctext.delete("1.0", "end") cdgrps.delete("1.0", "end") # Try encrypting try: tx = nomenclator(T, int(K), decode=False) except Exception as e: ctext.insert("insert", str(e)) ctext.insert("insert", tx) for i in PrintCodes(int(K)): cdgrps.insert("insert", i) cdgrps.insert("insert", "\n")
# These tests are extremely simple and are used to confirm that decoding works # properly. import sys sys.path.append("C:\\Users\\Alexander\\Documents\\GitHub\\ClassicCrypto") # Some stuff we need for testing from Ciphers.UtilityFunctions import decodetest, preptext, playfairPrep # Load up the text to use textfile = open( 'C:\\Users\\Alexander\\Documents\\GitHub\\ClassicCrypto\\SampleText\\text1.txt', 'r') ptext = preptext(textfile.readline()) print() # Import the various ciphers # Monoalphabetic substitution from Ciphers.Simple import affine, caesar, substitution # Variation on the Playfair Cipher from Ciphers.Playfair import playfair, twoSquare, fourSquare # Rotor machines from Ciphers.RotorMachine import enigma, SIGABA, M209 # Variations on the polybius square from Ciphers.Polybius import polybiusSquare, ADFGX, ADFGVX, bifid, trifid # Transposition ciphers
def gettext(): T = ptext.get("1.0", "end")[:-1] T = preptext(T, silent=True) return T