自生成密钥程序

时间:2018-09-17 23:55:28

标签: python encryption one-time-pad

代码背景: 下面是我正在处理的代码。该代码的目的是使用种子来生成一个长的伪随机密钥。稍后将使用该密钥进行加密。从目前来看,如果使用单个字符,它将吐出对该字符唯一的23个字符串。希望将密钥的大小扩展到可用大小,以生成伪OTP。它还设置为可以处理所有可键入的ASCII字符。

它的实现方法是使用经过修改的Vigenere密码,将SEED本身作为第一个密钥,而下一个密钥使用自身来生成下一个密钥,依此类推。只要此密钥与消息的大小相同或更大,它实际上就是一个OTP。值得注意的是,我从早期对该概念的试验中发现,[消息字母] = / = [密钥字母]或它将锁定该字符,最终密码仅加密到该字母,因此无法解密。或者,至少那是我使用标准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()

0 个答案:

没有答案