I think you have to use +=
for example cypherText += cypherText+char
, because when you use =
you are resetting the whole string.
Another this is, I've already used a decoder so ill show you the code I've used and maybe you can change it to fit you, here it is:
key += 1
newMessage = ''
for character in message:
if character in noCapAlphabet:
position = noCapAlphabet.find(character)
newPosition = (position - key) % 26
newCharacter = noCapAlphabet[newPosition]
newMessage += newCharacter
elif character in number:
position = number.find(character)
newPosition = (position - key) % 10
newCharacter = number[newPosition]
newMessage += newCharacter
elif character in capAlphabet:
position = capAlphabet.find(character)
newPosition = (position - key) % 26
newCharacter = capAlphabet[newPosition]
newMessage += newCharacter
else:
newMessage += character
noCapAlphabet
, number
and capAlphabet
are arrays
Edit, the function can work as well:
message = 'ifmmp'
noCapAlphabet = 'abcdefghijklmnopqrstuvwxyz'
capAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
def decrypt(message):
key = 1
newMessage = ''
for character in message:
if character in noCapAlphabet:
position = noCapAlphabet.find(character)
newPosition = (position - key) % 26
newCharacter = noCapAlphabet[newPosition]
newMessage += newCharacter
elif character in number:
position = number.find(character)
newPosition = (position - key) % 10
newCharacter = number[newPosition]
newMessage += newCharacter
elif character in capAlphabet:
position = capAlphabet.find(character)
newPosition = (position - key) % 26
newCharacter = capAlphabet[newPosition]
newMessage += newCharacter
else:
newMessage += character
return newMessage
print(decrypt(message))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…