2019-10-18 05:36:52 +00:00
|
|
|
def encrypt(input_string: str, key: int) -> str:
|
2019-10-22 17:13:48 +00:00
|
|
|
result = ""
|
2019-10-18 05:36:52 +00:00
|
|
|
for x in input_string:
|
|
|
|
if not x.isalpha():
|
|
|
|
result += x
|
|
|
|
elif x.isupper():
|
|
|
|
result += chr((ord(x) + key - 65) % 26 + 65)
|
|
|
|
elif x.islower():
|
|
|
|
result += chr((ord(x) + key - 97) % 26 + 97)
|
|
|
|
return result
|
2018-05-28 13:46:02 +00:00
|
|
|
|
|
|
|
|
2019-10-18 05:36:52 +00:00
|
|
|
def decrypt(input_string: str, key: int) -> str:
|
2019-10-22 17:13:48 +00:00
|
|
|
result = ""
|
2019-10-18 05:36:52 +00:00
|
|
|
for x in input_string:
|
|
|
|
if not x.isalpha():
|
|
|
|
result += x
|
|
|
|
elif x.isupper():
|
|
|
|
result += chr((ord(x) - key - 65) % 26 + 65)
|
|
|
|
elif x.islower():
|
|
|
|
result += chr((ord(x) - key - 97) % 26 + 97)
|
|
|
|
return result
|
2018-05-28 13:46:02 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-10-18 05:36:52 +00:00
|
|
|
def brute_force(input_string: str) -> None:
|
2018-05-28 13:46:02 +00:00
|
|
|
key = 1
|
2019-10-22 17:13:48 +00:00
|
|
|
result = ""
|
2018-05-28 21:25:48 +00:00
|
|
|
while key <= 94:
|
2019-10-18 05:36:52 +00:00
|
|
|
for x in input_string:
|
2018-05-28 13:46:02 +00:00
|
|
|
indx = (ord(x) - key) % 256
|
|
|
|
if indx < 32:
|
|
|
|
indx = indx + 95
|
2019-10-18 05:36:52 +00:00
|
|
|
result = result + chr(indx)
|
2019-10-22 17:13:48 +00:00
|
|
|
print(f"Key: {key}\t| Message: {result}")
|
|
|
|
result = ""
|
2018-05-28 13:46:02 +00:00
|
|
|
key += 1
|
|
|
|
return None
|
|
|
|
|
2016-07-29 07:00:38 +00:00
|
|
|
|
2016-08-02 17:46:55 +00:00
|
|
|
def main():
|
2018-05-28 13:46:02 +00:00
|
|
|
while True:
|
2019-10-18 05:36:52 +00:00
|
|
|
print(f'{"-" * 10}\n Menu\n{"-", * 10}')
|
2019-10-22 17:13:48 +00:00
|
|
|
print(*["1.Encrpyt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n")
|
2018-10-20 19:45:08 +00:00
|
|
|
choice = input("What would you like to do?: ")
|
2019-10-05 05:14:13 +00:00
|
|
|
if choice not in ["1", "2", "3", "4"]:
|
2019-08-06 10:14:23 +00:00
|
|
|
print("Invalid choice, please enter a valid choice")
|
2019-10-05 05:14:13 +00:00
|
|
|
elif choice == "1":
|
2019-10-18 05:36:52 +00:00
|
|
|
input_string = input("Please enter the string to be encrypted: ")
|
|
|
|
key = int(input("Please enter off-set between 0-25: "))
|
2018-10-27 12:14:35 +00:00
|
|
|
if key in range(1, 95):
|
2019-10-18 05:36:52 +00:00
|
|
|
print(encrypt(input_string.lower(), key))
|
2019-10-05 05:14:13 +00:00
|
|
|
elif choice == "2":
|
2019-10-18 05:36:52 +00:00
|
|
|
input_string = input("Please enter the string to be decrypted: ")
|
2018-10-27 12:14:35 +00:00
|
|
|
key = int(input("Please enter off-set between 1-94: "))
|
2019-10-05 05:14:13 +00:00
|
|
|
if key in range(1, 95):
|
2019-10-18 05:36:52 +00:00
|
|
|
print(decrypt(input_string, key))
|
2019-10-05 05:14:13 +00:00
|
|
|
elif choice == "3":
|
2019-10-18 05:36:52 +00:00
|
|
|
input_string = input("Please enter the string to be decrypted: ")
|
|
|
|
brute_force(input_string)
|
2018-05-28 13:46:02 +00:00
|
|
|
main()
|
2019-10-05 05:14:13 +00:00
|
|
|
elif choice == "4":
|
2019-08-06 10:14:23 +00:00
|
|
|
print("Goodbye.")
|
2018-10-27 12:14:35 +00:00
|
|
|
break
|
2019-07-08 15:27:51 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2019-07-08 15:27:51 +00:00
|
|
|
main()
|