mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
Merge pull request #305 from PyDevthon/patch-1
Updated_caesar_cipher.py
This commit is contained in:
commit
192ba078dc
|
@ -1,45 +1,68 @@
|
||||||
from __future__ import print_function
|
import sys
|
||||||
# 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 <= 94:
|
||||||
|
for x in strng:
|
||||||
|
indx = (ord(x) - key) % 256
|
||||||
|
if indx < 32:
|
||||||
|
indx = indx + 95
|
||||||
|
decrypted = decrypted + chr(indx)
|
||||||
|
print("Key: {}\t| Message: {}".format(key, decrypted))
|
||||||
|
decrypted = ''
|
||||||
|
key += 1
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
message = input("Enter message: ")
|
print('-' * 10 + "\n**Menu**\n" + '-' * 10)
|
||||||
key = int(input("Key [1-26]: "))
|
print("1.Encrpyt")
|
||||||
mode = input("Encrypt or Decrypt [e/d]: ")
|
print("2.Decrypt")
|
||||||
|
print("3.BruteForce")
|
||||||
if mode.lower().startswith('e'):
|
print("4.Quit")
|
||||||
mode = "encrypt"
|
while True:
|
||||||
elif mode.lower().startswith('d'):
|
choice = input("What would you like to do?: ")
|
||||||
mode = "decrypt"
|
if choice not in ['1', '2', '3', '4']:
|
||||||
|
print ("Invalid choice")
|
||||||
translated = encdec(message, key, mode)
|
elif choice == '1':
|
||||||
if mode == "encrypt":
|
strng = input("Please enter the string to be ecrypted: ")
|
||||||
print(("Encryption:", translated))
|
while True:
|
||||||
elif mode == "decrypt":
|
key = int(input("Please enter off-set between 1-94: "))
|
||||||
print(("Decryption:", translated))
|
if key in range(1, 95):
|
||||||
|
print (encrypt(strng, key))
|
||||||
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()
|
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.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user