Python/ciphers/caesar_cipher.py

70 lines
1.8 KiB
Python
Raw Normal View History

2018-05-28 13:46:02 +00:00
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
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
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()