Skip to content

Commit

Permalink
Updated_caesar_cipher.py
Browse files Browse the repository at this point in the history
Updated
  • Loading branch information
PyDevthon authored May 28, 2018
1 parent 2e74c8e commit f1fe458
Showing 1 changed file with 67 additions and 43 deletions.
110 changes: 67 additions & 43 deletions ciphers/caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -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]: ")

if mode.lower().startswith('e'):
mode = "encrypt"
elif mode.lower().startswith('d'):
mode = "decrypt"

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()
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

main()


0 comments on commit f1fe458

Please sign in to comment.