代码背景:以下是我正在处理的代码。这段代码的目的是使用种子生成一个长伪随机密钥。稍后会使用该密钥进行加密。截至目前,如果使用单个字符,它将吐出一个 23 个字符的字符串,该字符串对该字符是唯一的。希望将密钥的大小扩大到一个可用的大小,从而生成一个伪 OTP。它还设置为处理所有可键入的 ASCII 字符。
它实现这一点的方法是使用修改后的 Vigenere Cipher 和 SEED,将其自身作为第一个密钥,下一个密钥使用自身生成下一个密钥,依此类推。只要此密钥与消息的大小相同或更大,它实际上就是一个 OTP。值得注意的是,我从这个概念的早期实验中发现,[message letter]=/=[key letter] 或者它会锁定那个字符,最终密码会单独加密到那个字母,使其无法解密。或者,至少当我使用标准的 Vigenere 表手动完成时发生了这种情况。
问题:我尝试了两种从单个字符增加种子大小的方法,看看是否可以生成更长的密钥。方法 2 使用 2 个变量作为键,并且在整个运行过程中只生成来自 BKEY 的字母。方法 3 旨在使用任何大小的数组,它可以。然而,产生的键只适用于重复之前的数组长度。
如果有人可以牵手,甚至只是提供一些建议,我将不胜感激。
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()