BACKGROUND OF CODE: Below is a code that I'm working on. The purpose of this code is to use a seed to generate a long pseudo-random key. Work on using that key for encryption comes later. As of current, if a single character is used, it will spit out a 23 character string that is unique to that to that character. The hope is to expand the size of the key to a usable size, so as to generate a psuedo-OTP. It is also set up to handle all typable ASCII characters.
The way it achieves this is by using a modified Vigenere Cipher with the SEED with itself as the first key and the next key uses itself to generate the next key, and so on and so forth. As long as this key is the same size or greater of the message, it is effectively a OTP. Of note that I found out from early experimentation with this concept is that [message letter]=/=[key letter] or it will lock in that character, and eventually the cipher encrypts to that letter alone, making it impossible to decrypt. Or, at least that's what happened when I did it by hand using a standard Vigenere table.
THE ISSUE: I have tried two methods of increasing the size of the seed from a single character, to see if I can generate longer keys. Method 2 uses 2 variables for keys and only produces the letter from BKEY for the entirety of the run. Method 3 is designed to use arrays of any size, which it can. However the key that is produces only goes for the length of the array before repeating.
If anyone can held a hand, or even just offer some suggestions, I'd be grateful.
MINLIMIT = 32
MAXLIMIT = 126
SPAN = 94
#This converts the letter to an integer between 1 and 94.
def GETKEY(KEYLET):
KEY = KEYLET - 31
return KEY
#This checks to see if the encrypted character is with the bounds of the
#available characters and corrects them if they aren't.
def CHECK(CIPHER):
if (CIPHER > MAXLIMIT):
CIPHER -= SPAN
elif (CIPHER < MINLIMIT):
CIPHER += SPAN
return CIPHER
#This combines the message character with the key character, sends the
#result to be checked, before sending it to be printed.
def ENCRYPT(LETTER,KEYLET):
KEY = GETKEY(KEYLET)
if (KEY != 1):
ENCODE = LETTER + KEY
else:
ENCODE = LETTER - 3
CIPHER = CHECK(ENCODE)
return CIPHER
#Creates a key from a single seed. Length is set by Main. SEED is
#controlled here as KEY.
def TESTSINGLE(COUNT):
KEY = ord('a')
while (COUNT != 0):
KEY = ENCRYPT(KEY,KEY)
print(chr(KEY),end = '')
COUNT = COUNT -1
#Tries to create a key from two different seeds (AKEY and BKEY) by
#alternating them between even and odd iterations. Non-functional.
def TESTMULTIPLE(COUNT):
AKEY = ord('a')
BKEY = ord('b')
while (COUNT != 0):
if (COUNT%2 == 1):
CKEY = ENCRYPT(AKEY,AKEY)
print(CKEY)
else:
CKEY = ENCRYPT(BKEY,BKEY)
print(CKEY)
print(chr(BKEY),end = '')
COUNT = COUNT - 1
#Uses an array as seed to generate key. The array is LONGKEY, and size can
#be changed simply by adding/removing elements. The code will cope with
#any changes.
def TESTARRAY(COUNT):
LONGKEY = ['a','c']
LENGTH = len(LONGKEY)
CKEY = 0
while (COUNT != 0):
POINT = COUNT%LENGTH
CKEY = ord(LONGKEY[POINT])
CKEY = ENCRYPT(CKEY,CKEY)
print(chr(CKEY),end = '')
COUNT = COUNT - 1
#COUNT changes the length of the key to be generated. SELECT changes which
#method to be used. Currently, the values must be adjusted in the code,
#but it is trivial to set up a user prompt.
def MAIN():
COUNT = 24
SELECT = 2
if(SELECT == 1):
TESTSINGLE(COUNT)
elif(SELECT == 2):
TESTMULTIPLE(COUNT)
elif(SELECT == 3):
TESTARRAY(COUNT)
print('')
MAIN()