From f1fe4583562a1840af9b1128f33114272d7f1ed8 Mon Sep 17 00:00:00 2001 From: PyDevthon <34104945+PyDevthon@users.noreply.github.com> Date: Mon, 28 May 2018 19:16:02 +0530 Subject: [PATCH 1/2] Updated_caesar_cipher.py Updated --- ciphers/caesar_cipher.py | 104 ++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index a53dc5857..4560db03b 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,45 +1,69 @@ -from __future__ import print_function -# The Caesar Cipher Algorithm +def encrypt(strng, key): + encrypted = '' + for x in strng: + indx = (ord(x) + key) % 256 + if indx > 126: + indx = indx - 95 + encrypted = encrypted + chr(indx) + return encrypted + + +def decrypt(strng, key): + decrypted = '' + for x in strng: + indx = (ord(x) - key) % 256 + if indx < 32: + indx = indx + 95 + decrypted = decrypted + chr(indx) + return decrypted + +def brute_force(strng): + key = 1 + decrypted = '' + while key != 96: + for x in strng: + indx = (ord(x) - key) % 256 + if indx < 32: + indx = indx + 95 + decrypted = decrypted + chr(indx) + print(decrypted) + decrypted = '' + key += 1 + return None + def main(): - message = input("Enter message: ") - key = int(input("Key [1-26]: ")) - mode = input("Encrypt or Decrypt [e/d]: ") + print("**Menu**") + print("1.Encrpyt") + print("2.Decrypt") + print("3.BruteForce") + print("4.Quit") + while True: + choice = input("what would you like to do") + if choice not in ['1', '2', '3', '4']: + print ("Invalid choice") + elif choice == '1': + strng = input("Please enter the string to be ecrypted:") + while True: + key = int(input("Please enter off-set between 1-94")) + if key > 0 and key <= 94: + print (encrypt(strng, key)) + main() + elif choice == '2': + strng = input("Please enter the string to be decrypted:") + while True: + key = int(input("Please enter off-set between 1-94")) + if key > 0 and key <= 94: + print(decrypt(strng, key)) + main() + elif choice == '3': + strng = input("Please enter the string to be decrypted:") + brute_force(strng) + main() + elif choice == '4': + print ("GoodBye.") + break - if mode.lower().startswith('e'): - mode = "encrypt" - elif mode.lower().startswith('d'): - mode = "decrypt" +main() - translated = encdec(message, key, mode) - if mode == "encrypt": - print(("Encryption:", translated)) - elif mode == "decrypt": - print(("Decryption:", translated)) - -def encdec(message, key, mode): - message = message.upper() - translated = "" - LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - for symbol in message: - if symbol in LETTERS: - num = LETTERS.find(symbol) - if mode == "encrypt": - num = num + key - elif mode == "decrypt": - num = num - key - if num >= len(LETTERS): - num -= len(LETTERS) - elif num < 0: - num += len(LETTERS) - - translated += LETTERS[num] - else: - translated += symbol - return translated - -if __name__ == '__main__': - import doctest - doctest.testmod() - main() From 13617225cafdec85cb067c5c7badca28d408eb92 Mon Sep 17 00:00:00 2001 From: Harshil Date: Mon, 28 May 2018 23:25:48 +0200 Subject: [PATCH 2/2] small improvements! --- ciphers/caesar_cipher.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 4560db03b..6cd35e73d 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,3 +1,4 @@ +import sys def encrypt(strng, key): encrypted = '' for x in strng: @@ -20,50 +21,48 @@ def decrypt(strng, key): def brute_force(strng): key = 1 decrypted = '' - while key != 96: + while key <= 94: for x in strng: indx = (ord(x) - key) % 256 if indx < 32: indx = indx + 95 decrypted = decrypted + chr(indx) - print(decrypted) + print("Key: {}\t| Message: {}".format(key, decrypted)) decrypted = '' key += 1 return None def main(): - print("**Menu**") + print('-' * 10 + "\n**Menu**\n" + '-' * 10) print("1.Encrpyt") print("2.Decrypt") print("3.BruteForce") print("4.Quit") while True: - choice = input("what would you like to do") + choice = input("What would you like to do?: ") if choice not in ['1', '2', '3', '4']: print ("Invalid choice") elif choice == '1': - strng = input("Please enter the string to be ecrypted:") + strng = input("Please enter the string to be ecrypted: ") while True: - key = int(input("Please enter off-set between 1-94")) - if key > 0 and key <= 94: + key = int(input("Please enter off-set between 1-94: ")) + if key in range(1, 95): print (encrypt(strng, key)) main() elif choice == '2': - strng = input("Please enter the string to be decrypted:") + strng = input("Please enter the string to be decrypted: ") while True: - key = int(input("Please enter off-set between 1-94")) + key = int(input("Please enter off-set between 1-94: ")) if key > 0 and key <= 94: print(decrypt(strng, key)) main() elif choice == '3': - strng = input("Please enter the string to be decrypted:") + strng = input("Please enter the string to be decrypted: ") brute_force(strng) main() elif choice == '4': - print ("GoodBye.") - break + print ("Goodbye.") + sys.exit() main() - -