I would like to build a cypher with python which decoding text by repeatedly travel through the numbers of the shifter from left to right and then from right to left and shift the letter in the alphabet by the corresponding number.
Example:
- shifter: 123
- text:
i like python - process: i+1=j, space, l+2=n, i+3=l, k+3=n, e+2=g, space, p+1=q, y+1=z, t+2=v, h+3=j, o+3=q, n+2=p
- result:
j nlng qzvjqp
The code so far:
import string
numbers = ""
x = 3
while x < 10000:
numbers = numbers + str(x)
x += 1
shift = 221
#string.ascii_lowercase
letters = string.ascii_letters + string.punctuation + numbers
text = (raw_input("Please enter text"))
encoded = ''
for letter in text:
if letter == ' ':
encoded = encoded + ' '
else:
x = letters.index(letter) + shift
encoded = encoded + letters[x]
print encoded
It uses so far a basic coding method. I would love to know how is it possible to implement the above described encoding system in the code.
Here is your version optimized (and working):
Edit The part with numbers was really wrong, because it added duplicate letters to your encoding sequence. This means that an encoded text cannot be decoded uniquely.
Edit2: If you want secure en/decryption, you need a modern algorithm like AES. If you want it implement on your own or understand the code, RC4 is a simple (but still secure if used correctly) alternative.
Edit3: Here is Vigenere in an optimized version: