mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-19 21:19:47 +00:00
Feature/fix caesar cipher (#1350)
* change var names for better reading * rewrite encrypt function to fix ascii char ranges * fix decrypt function * update formatting (add f-strings) * upd fuctions * add f-string formatting (python3.6+) * add type hints (python3.4+)
This commit is contained in:
parent
3cc3531076
commit
b7fb0630f2
@ -1,61 +1,62 @@
|
|||||||
def encrypt(strng, key):
|
def encrypt(input_string: str, key: int) -> str:
|
||||||
encrypted = ""
|
result = ''
|
||||||
for x in strng:
|
for x in input_string:
|
||||||
indx = (ord(x) + key) % 256
|
if not x.isalpha():
|
||||||
if indx > 126:
|
result += x
|
||||||
indx = indx - 95
|
elif x.isupper():
|
||||||
encrypted = encrypted + chr(indx)
|
result += chr((ord(x) + key - 65) % 26 + 65)
|
||||||
return encrypted
|
elif x.islower():
|
||||||
|
result += chr((ord(x) + key - 97) % 26 + 97)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def decrypt(strng, key):
|
def decrypt(input_string: str, key: int) -> str:
|
||||||
decrypted = ""
|
result = ''
|
||||||
for x in strng:
|
for x in input_string:
|
||||||
indx = (ord(x) - key) % 256
|
if not x.isalpha():
|
||||||
if indx < 32:
|
result += x
|
||||||
indx = indx + 95
|
elif x.isupper():
|
||||||
decrypted = decrypted + chr(indx)
|
result += chr((ord(x) - key - 65) % 26 + 65)
|
||||||
return decrypted
|
elif x.islower():
|
||||||
|
result += chr((ord(x) - key - 97) % 26 + 97)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def brute_force(strng):
|
def brute_force(input_string: str) -> None:
|
||||||
key = 1
|
key = 1
|
||||||
decrypted = ""
|
result = ''
|
||||||
while key <= 94:
|
while key <= 94:
|
||||||
for x in strng:
|
for x in input_string:
|
||||||
indx = (ord(x) - key) % 256
|
indx = (ord(x) - key) % 256
|
||||||
if indx < 32:
|
if indx < 32:
|
||||||
indx = indx + 95
|
indx = indx + 95
|
||||||
decrypted = decrypted + chr(indx)
|
result = result + chr(indx)
|
||||||
print("Key: {}\t| Message: {}".format(key, decrypted))
|
print(f'Key: {key}\t| Message: {result}')
|
||||||
decrypted = ""
|
result = ''
|
||||||
key += 1
|
key += 1
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
while True:
|
while True:
|
||||||
print("-" * 10 + "\n**Menu**\n" + "-" * 10)
|
print(f'{"-" * 10}\n Menu\n{"-", * 10}')
|
||||||
print("1.Encrpyt")
|
print(*["1.Encrpyt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep='\n')
|
||||||
print("2.Decrypt")
|
|
||||||
print("3.BruteForce")
|
|
||||||
print("4.Quit")
|
|
||||||
choice = input("What would you like to do?: ")
|
choice = input("What would you like to do?: ")
|
||||||
if choice not in ["1", "2", "3", "4"]:
|
if choice not in ["1", "2", "3", "4"]:
|
||||||
print("Invalid choice, please enter a valid choice")
|
print("Invalid choice, please enter a valid choice")
|
||||||
elif choice == "1":
|
elif choice == "1":
|
||||||
strng = input("Please enter the string to be encrypted: ")
|
input_string = input("Please enter the string to be encrypted: ")
|
||||||
key = int(input("Please enter off-set between 1-94: "))
|
key = int(input("Please enter off-set between 0-25: "))
|
||||||
if key in range(1, 95):
|
if key in range(1, 95):
|
||||||
print(encrypt(strng.lower(), key))
|
print(encrypt(input_string.lower(), key))
|
||||||
elif choice == "2":
|
elif choice == "2":
|
||||||
strng = input("Please enter the string to be decrypted: ")
|
input_string = input("Please enter the string to be decrypted: ")
|
||||||
key = int(input("Please enter off-set between 1-94: "))
|
key = int(input("Please enter off-set between 1-94: "))
|
||||||
if key in range(1, 95):
|
if key in range(1, 95):
|
||||||
print(decrypt(strng, key))
|
print(decrypt(input_string, key))
|
||||||
elif choice == "3":
|
elif choice == "3":
|
||||||
strng = input("Please enter the string to be decrypted: ")
|
input_string = input("Please enter the string to be decrypted: ")
|
||||||
brute_force(strng)
|
brute_force(input_string)
|
||||||
main()
|
main()
|
||||||
elif choice == "4":
|
elif choice == "4":
|
||||||
print("Goodbye.")
|
print("Goodbye.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user