Python 中的 Vigenère 密码函数

Spe*_*e43 5 python encryption function python-3.x vigenere

看完这篇关于 Vigenere Cipher 的教程后,我(希望)理解了它的基本概念。我们想为字符串分配一个键,然后将字符串中的每个字母移动键中每个字母的(从 0 开始的)字母位置值。所以当以培根为重点时,

Meet me in the park at eleven am
baco nb ac onb acon ba conbac on
Run Code Online (Sandbox Code Playgroud)

变成

Negh zf av huf pcfx bt gzrwep oz
Run Code Online (Sandbox Code Playgroud)

当我从头开始编写 Vigenere Cipher 时,我只知道第一步是将密钥分配给字符串。当我这样做时,我想识别每个字符是否是 alpha 以便我可以保留字符串中的任何特殊字符(!、@、# 等)(如果有的话)。

text = input("Enter some text:")

def encrypt(text):

#key = bacon
encrypted = []
baconvalue = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3, 'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7, 'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N': 13, 'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16, 'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19, 't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22, 'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25 }


for letter in text:

#assign 'bacon' to text to get rotation value for each character
#preserve alpha characters

        if letter.isalpha():  

#character in string rotates x amount according to the corresponding value of char in bacon

            encrypted.append(letter, baconvalue)        

        else:    

            encrypted.append(letter)            

        return ''.join(encrypted)

print(encrypt(text,))
Run Code Online (Sandbox Code Playgroud)

但是正如您所看到的,就如何将培根分配给字符串而言,我不知道从哪里开始。我至少在正确的轨道上吗?如果可以的话请帮忙。谢谢你。

Spe*_*e43 3

使用用户输入的值作为键对字符串进行索引的方法是创建一个变量starting_index并将其设置为= 0。这样,迭代将从字符串中的第一个字符开始,您将能够rotation使用alphabet_pos之前创建的字典生成一个值。

使用您的rotate函数通过您创建的新旋转变量来旋转字母。如果在您的字典中找到该字母,该encrypt函数将附加这个新字母并继续,直到到达键中的最后一个索引值。然后它将把加密的字母连接在一起。

alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3, 'd':3,
'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7, 'h':7, 'I':8, 'i':8,
'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N': 13,
'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16, 'q':16, 'R':17, 'r':17,
'S':18, 's':18, 'T':19, 't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22,
'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25 }

def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3,
'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6, 'H':7, 'h':7, 'I':8,
'i':8, 'J':9, 'j':9, 'K':10, 'k':10, 'L':11, 'l':11, 'M':12, 'm':12,
'N': 13, 'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16, 'q':16,
'R':17, 'r':17, 'S':18, 's':18, 'T':19, 't':19, 'U':20, 'u':20, 'V':21,
'v':21, 'W':22, 'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25
}
    pos = alphabet_pos[letter]
    return pos

def rotate(letter, rot):
    shift = 97 if letter.islower() else 65
    return chr((ord(letter) + rot - shift) % 26 + shift)

def encrypt(text, key):
    encrypted = []    
    starting_index = 0
    for letter in text:
    # if it's alphanumerical, keep it that way
    # find alphabet position
        rotation = alphabet_position(key[starting_index])
    # if it's a space or non-alphabetical character, append and move on
        if not letter in alphabet_pos:
            encrypted.append(letter)
        elif letter.isalpha():            
            encrypted.append(rotate(letter, rotation))             

    #if we've reached last index, reset to zero, otherwise + by 1
        if starting_index == (len(key) - 1): 
            starting_index = 0
        else: 
            starting_index += 1

    return ''.join(encrypted)    

text = input("Enter some text:")
key = input("Enter a key:")

print(encrypt(text,key))
Run Code Online (Sandbox Code Playgroud)