def breakOpen(self,enc): it = 1 key = it words = 0 limit = 0 s = "" with open(enc, "r+b") as f: while 1: data = f.read(1) if not data : break s += data limit = len(s) # print limit while it <= limit: transposition = Transposition() file = transposition.decipher(enc,it) n = self.compare(file) # print str(it) + " "+ str(n) if n > words: key = it words = n it += 1 print key
def main(): print("Welcome to encryption/decrpytion program.") print() name = str(input("Please enter your name: ")) print("Hi {},".format(name.capitalize())) encryption = { 'Alberti': Alberti, 'Atbash': Atbash, 'Polybius': PolybiusSquare, 'Transposition': Transposition } game = True while game: picked = str( input("Please pick one of the ciphers: \n-{} \n".format('\n-'.join( key for key in encryption.keys())))).capitalize() if picked not in encryption.keys(): continue # Special cases if picked == 'Alberti': start_letter = str( input("Please Enter Albert inner allignment letter: ")) print( "Warning! Alberti decrption is not implemented. If you want to decrypt choose others." ) cipher = Alberti(start_letter) elif picked == 'Transposition': secret_word = str(input("Please Enter Secret word (ex. ZEBRAS): ")) cipher = Transposition(key=secret_word) else: cipher = encryption[picked]() method = str( input("Do you want to encrpyt (en) or decrpyt (de)? ")).lower() if method == 'en': try: text = str(input("What is your text for encryption: \n")) pad = str(input("Please enter the pad number (ex. XMCKL): ")) encrypted = cipher.encrypt(text) print(cipher.one_time_pad(encrypted, pad, method='encrypt')) except TypeError: raise ValueError("Input must be text") elif method == 'de': try: text = str(input("What is your text for decryption: \n")) pad = str(input("Please enter the pad number (ex.XMCKL): ")) text_pad = cipher.one_time_pad(text, pad, method='decrypt') print(cipher.decrypt(text_pad)) except TypeError: raise ValueError("Input must be text") print() cont = str(input("Do you want to continue (yes/no) : ")) if not cont.lower() == 'yes': game = False
script,from_file,key,algorithm,option = argv if algorithm == "c": caesar = Caesar() if option == "c": caesar.cipher(from_file,int(key)) elif option == "d": caesar.decipher(from_file,int(key)) else: print "Option doesn't exist." elif algorithm == "t": transposition = Transposition() if option == "c": transposition.cipher(from_file,int(key)) elif option == "d": transposition.decipher(from_file,int(key)) else: print "Option doesn't exist." elif algorithm == "v": vigenere = Vigenere() if option == "c": vigenere.cipher(from_file,key) elif option == "d": vigenere.decipher(from_file,key)
def main(): symbols = string.ascii_lowercase # first argument is the cipher cipher = sys.argv[1] # second arugment is the key key = sys.argv[2] # 3rd argument is the encrypt/Decrypt mode = sys.argv[3] # 4th argument is the input text input_txt = sys.argv[4] # 5th arugment is the output text output_txt = sys.argv[5] plainText = '' cipherText = '' text = open(input_txt) if mode == 'encrypt': plainText = text.read() if mode == 'decrypt': cipherText = text.read() if cipher == 'caesar' or cipher == 'Caesar': caesar = Caesar(key, symbols) if mode == 'encrypt': cipherText = open(output_txt, 'w') cipherText.write(caesar.encrypt(plainText)) cipherText.close() if mode == 'decrypt': plainText = open(output_txt, 'w') plainText.write(caesar.decrypt(cipherText)) plainText.close() if cipher == 'playfair' or cipher == 'Playfair': playfair = Playfair(key) if mode == 'encrypt': cipherText = open(output_txt, 'w') cipherText.write(playfair.encrypt(plainText)) cipherText.close() if mode == 'decrypt': plainText = open(output_txt, 'w') plainText.write(playfair.decrypt(cipherText)) plainText.close() if cipher == 'vigenere' or cipher == 'Vigenere': vigenere = Vigenere(key, symbols) if mode == 'encrypt': cipherText = open(output_txt, 'w') cipherText.write(vigenere.encrypt(plainText)) cipherText.close() if mode == 'decrypt': plainText = open(output_txt, 'w') plainText.write(vigenere.decrypt(cipherText)) plainText.close() if cipher == 'transposition' or cipher == 'Transposition': transposition = Transposition(key) if mode == 'encrypt': cipherText = open(output_txt, 'w') cipherText.write(transposition.encrypt(plainText)) cipherText.close() if mode == 'decrypt': plainText = open(output_txt, 'w') plainText.write(transposition.decrypt(cipherText)) plainText.close() if cipher == 'railfence' or cipher == 'Railfence': rail = RailFence(key) if mode == 'encrypt': cipherText = open(output_txt, 'w') cipherText.write(rail.encrypt(plainText)) cipherText.close() if mode == 'decrypt': plainText = open(output_txt, 'w') plainText.write(rail.decrypt(cipherText)) plainText.close()
from substitution import Substitution from transposition import Transposition test_message = "ABCD abcd ZYXW" sub_coded_message = Substitution.Encode(test_message, 4) sub_decoded_message = Substitution.Decode(sub_coded_message, 4) print("Encoding by Substitution: " + sub_coded_message) print("Decoding by Substitution: " + sub_decoded_message) trans_coded_message = Transposition.Encode(test_message, 3) print("Encoding by Transposition: " + trans_coded_message) trans_decoded_message = Transposition.Decode(trans_coded_message, 3) print("Decoding by Transposition: " + trans_decoded_message)
def run_cipher(): #print welcome and available ciphers print("Welcome to myCipher. We can encrypt or decrypt using the following ciphers: \n") print(" -Affine Cipher\n") print(" -Caesar Cipher\n") print(" -Keyword Cipher\n") print(" -Transposition Cipher\n") #variables cipher_invalid = True valid_ciphers = ["affine", "caesar", "keyword", "transposition"] e_or_d_invalid = True #Loop until valid cipher is chosen while cipher_invalid: cipher = input("Which cipher would you like to use? ").lower() if cipher in valid_ciphers: cipher_invalid = False else: print("\nNot valid cipher") #loop until encrypt or decrypt is chosen while e_or_d_invalid: encrypt_or_decrypt = input("Excellent choice! Are you encrypting or decrypting? Type E/d ").lower() if encrypt_or_decrypt == "e" or encrypt_or_decrypt == "d": e_or_d_invalid = False else: print("\n Not sure whether to encrypt or decrypt! Type E/d?") #give message to encrypt or decrypt msg = input("What is your message? ") #encrypt or decrypt branching if encrypt_or_decrypt == "e": if cipher == "caesar": e_msg = Caesar().encrypt(msg) elif cipher == "affine": e_msg = Affine().encrypt(msg) elif cipher == "keyword": keyword = input("What is your keyword? ") e_msg = Keyword(keyword).encrypt(msg) elif cipher == "transposition": e_msg = Transposition().encrypt(msg) print(e_msg) else: if cipher == "caesar": d_msg = Caesar().decrypt(msg) elif cipher == "affine": d_msg = Affine().decrypt(msg) elif cipher == "keyword": keyword = input("What is your keyword? ") d_msg = Keyword(keyword).decrypt(msg) elif cipher == "transposition": d_msg = Transposition().decrypt(msg) print(d_msg)