Skip to content

Updated caesar_cipher.py #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()