I am new to programming and I am trying to write a Vigenère encryption cipher using python. The idea is very simple, and my function, however, is on this line:
( if((BinKey[i] == 'b')or(BinKey[i+1] == 'b')): )
It seems that I have a problem with the index, and I cannot figure out how to fix it. Error message:
IndexError: string index out of range
I tried replacing the index i+1 another variable equal to i+1 , since I thought that perhaps python would increment the value of i again, but it still wouldn't work.
So my questions are:
How to fix the problem and what did I do wrong?
Looking at my code, what can I learn to improve my programming skills?
I want to create a simple interface for my program (which will contain all the encryption ciphers), and all I came up with from Google is pyqt, but it just seems like too much work for a very simple interface, so is there an easier way to build interface? (I work with Eclipse Indigo and pydev with Python3.x)
Vigenère encryption function (which contains the string that causes the problem):
def Viegner_Encyption_Cipher(Key,String): EncryptedMessage = "" i = 0 j = 0 BinKey = Bin_It(Key) BinString = Bin_It(String) BinKeyLengh = len(BinKey) BinStringLengh = len(BinString) while ((BinKeyLengh > i) and (BinStringLengh > j)): if((BinKey[i] == 'b')or(BinKey[i+1] == 'b')): EncryptedMessage = EncryptedMessage + BinKey[i] else: EncryptedMessage = EncryptedMessage + Xor(BinKey[i],BinString[j]) i = i + 1 j = j + 1 if (i == BinKeyLengh): i = i+j return EncryptedMessage
This is the Bin_It function:
def Bin_It(String): TheBin = "" for Charactere in String: TheBin = TheBin + bin(ord(Charactere)) return TheBin
And finally, this is the Xor function:
def Xor(a,b): xor = (int(a) and not int(b)) or (not int(a) and int(b)) if xor: return chr(1) else: return chr(0)
hamza source share