Program returns an "IndexError: string index out of range."
Question: 
    from random import shuffle  
    
    alphabet="abcdefghijklmnopqrstuvwxyz"  
    
    def substitution(alphabet,plaintext):  
    
        # Create array to use to randomise alphabet position  
        randarray=range(0,len(alphabet))  
        shuffle(randarray)  
    
        key="Zebra"  
    
        #Create our substitution dictionary  
        dic={}  
        for i in range(0,len(alphabet)):  
            key+=alphabet[randarray[i]]  
            dic[alphabet[i]]=alphabet[randarray[i]]  
    
        #Convert each letter of plaintext to the corrsponding  
        #encrypted letter in our dictionary creating the cryptext  
        ciphertext=""  
        for l in plaintext:  
            if l in dic:  
                l=dic[l]  
            ciphertext+=l  
        for i in alphabet:  
            print i,  
        print  
        for i in key:  
            print i,  
        print  
        return ciphertext,key  
    
    # This function decodes the ciphertext using the key and creating  
    # the reverse of the dictionary created in substitution to retrieve  
    # the plaintext again  
    def decode(alphabet,ciphertext,key):  
    
        dic={}  
        for i in range(0,len(key)):  
            dic[key[i]]=alphabet[i]  
    
        plaintext=""  
        for l in ciphertext:  
            if l in dic:  
                l=dic[l]  
            plaintext+=l  
    
        return plaintext  
    
    # Example useage  
    plaintext="the cat sat on the mat"  
    ciphertext,key=substitution(alphabet,plaintext)  
    print "Key: ", key  
    print "Plaintext:", plaintext  
    print "Cipertext:", ciphertext  
    print "Decoded  :", decode(alphabet,ciphertext,key)
    

When I run this code, it returns a `"IndexError: String index out of range"`
error. Could someone give me hand troubleshooting it, I can't see the problem.

    
    
    Traceback (most recent call last):
      File"/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py", line 57, in 
        print "Decoded :", decode(alphabet,ciphertext,key)
      File "/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py", line 41, in decode
        dic[key[i]]=alphabet[i] IndexError: string index out of range
    


Answer: The problem lies here:

    
    
    def decode(alphabet,ciphertext,key):  
        dic={}  
        for i in range(0,len(key)):  
            dic[key[i]]=alphabet[i] # this line fails
    

Your `key` at this point is always 31 characters, which is `len('Zebra') +
len(alphabet)`. As `len(alphabet)` is always 26, `alphabet[i]` fails when i >
25.

I believe you've misunderstood what is represented by `key` here.
`substitution` should _produce_ a randomised key, it's not a password or a
salt of any kind. In fact, if you look at [the original article you got this
code from](http://www.stealthcopter.com/blog/2009/12/python-cryptography-
substitution-cipher-improving-on-the-caesar-cipher/), you'll see that `key=""`
in `substitution` and not some-random-value.

