0

I am trying to create a one-time pad-esque cipher in python. To do this I am converting all of the plain text and then the key to ASCII code, then adding them together, then converting them back to regular characters. However, I have gotten stuck when I try to add the two numbers together. Here is my code:

#Creating initial variables
plainText = str(input('Input your plain text \n --> ')).upper()
key = str(input('Input your key. Make sure it is as long as your plain text.\n --> ')).upper()

#>Only allowing key if it is the same size as the plain text
if len(key) != len(plainText):
    print('Invalid key. Check key length.')
    key = str(input('Input your key. Make sure it is as long as your plain text. \n --> ')).upper()
else:
    plainAscii=[ord(i) for i in plainText]
    keyAscii=[ord(k) for k in key]

print (plainAscii)
print (keyAscii)

#Adding the values together and putting them into a new list
cipherText=[]
for i in range(0, len(key)):
    x = 1
    while x <= len(key):
        item = plainAscii[x] + keyAscii[x]
        cipherText.append(item)
        x = x + 1
print(cipherText)

I am printing the lists as I go along for testing. However it only returns this after printing the first two lists:

 Traceback (most recent call last):
  File "/Users/chuckii/Desktop/onetimepad.py", line 21, in <module>
    item = plainAscii[x] + keyAscii[x]
IndexError: list index out of range

Please ignore my juvenile username, I made it when I was 10. Thanks in advance.

2
  • Are you trying to do some Vigenere encryption with a one-time pad? Commented Jan 26, 2016 at 20:36
  • I did, and I completed it - I'm a very happy chappy. Commented Jan 26, 2016 at 20:55

1 Answer 1

1

Edit with more efficiency:

cipherText=[]

for i in range(len(key)):
    for x in range(len(key)):
        item = plainAscii[x] + keyAscii[x]
        cipherText.append(item)

print(cipherText)

Should solve it!

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately this doesn't solve it. I changed x to 0 and got the same thing returned to me.
Change your <= in while x <= len(key) to < too. That should solve it.
Thank you so much! It's going better now
Updated answer with the new information and a little optimization. Gives the same output, and is a little easier to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.