My encryption works fine, however when I run the decryption code, it doesn't work. When I get to that part of the code, an error comes up -
cipher2 += cipher[(cipher(A)-key1)%(len(cipher2))] TypeError: 'str' object is not callable
I would appreciate it if you took the time to help me.
alphabetL = 'abcdefghijklmnopqrstuvwxyz'
alphabetC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
space = ' '
Ll = len(alphabetL)
Lc = len(alphabetC)
Ln = len(number)
Lall = Ll + Lc + Ln
msgall = alphabetL + alphabetC + number + space
Question1 = input("Hello, please insert the message you want encrypted: ")
key1 = int(input("Please insert the key you want used [Keep between 1 and 26]: "))
cipher = ''
cipher2 = ''
for A in Question1:
if A in alphabetL:
cipher += alphabetL[(alphabetL.index(A)+key1)%Ll]
elif A in alphabetC:
cipher += alphabetC[(alphabetC.index(A)+key1)%Lc]
elif A in number:
cipher += number[(number.index(A)+key1)%Ln]
elif A in space:
cipher += space
else:
print ("Error, please use abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
print (cipher)
Question2 = input("Would you like to decrypt the message? [Y/N]: ")
if Question2 == "Y":
for A in cipher:
cipher2 += cipher[(cipher(A)-key1)%(len(cipher2))]
print (cipher2)
You could create a function for encrypting and decrypting.
alphabetL = 'abcdefghijklmnopqrstuvwxyz'
alphabetC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
space = ' '
Ll = len(alphabetL)
Lc = len(alphabetC)
Ln = len(number)
Lall = Ll + Lc + Ln
msgall = alphabetL + alphabetC + number + space
Question1 = input("Hello, please insert the message you want encrypted: ")
key1 = int(input("Please insert the key you want used [Keep between 1 and 26]: "))
cipher2 = ''
def ceaser_cipher(Question1,key1):
cipher = ''
for A in Question1:
if A in alphabetL:
cipher += alphabetL[(alphabetL.index(A)+key1)%Ll]
elif A in alphabetC:
cipher += alphabetC[(alphabetC.index(A)+key1)%Lc]
elif A in number:
cipher += number[(number.index(A)+key1)%Ln]
elif A in space:
cipher += space
else:
print ("Error, please use abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
return cipher
cipher=ceaser_cipher(Question1,key1)
print (cipher)
Question2 = input("Would you like to decrypt the message? [Y/N]: ")
if Question2 == "Y":
cipher2=ceaser_cipher(cipher,-key1)
print (cipher2)