Python Caesar和Vigenere密码

时间:2018-03-03 03:42:16

标签: python python-3.x encryption caesar-cipher vigenere

对于我的编程课程,我已被指示创建两个程序,每个程序使用相同的两个辅助函数:alphabet_position(letter)rotate_character(char, rot)。一个函数使用Caesar密码来加密具有多个旋转的消息,每个旋转都是从命令行给出的。第二个程序旨在通过将其转换为Vigenere Cipher来更多地使用我的Caesar程序。这两个程序都是为了忽略(即不转动)特殊字符,我也不认为数字很重要。但是,如果输入字母大写,则旋转的输出字母也应该是。

我的代码遇到了几个问题:我的Caesar程序只打印一些大写字母。例如,使用参数Hello, World!4,我收到Lipps, svph!的输出,我无法弄清楚为什么最后一个大写字母不打印。当我尝试以pYthon轮换26时,我的程序不打印任何内容。

我的Vigenere程序也使用大写字母做了一件奇怪的事情:传递的参数为Hello, World!boom,我收到Iszxp, mr!

分配是在48小时内完成的,我有点吓坏了。任何和所有的帮助,批评等都是受欢迎的!

def alphabet_position(letter):
    """receives a letter and returns the 0-based numerical position of that letter within the alphabet."""
    letter = letter.lower()
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    numerical_pos = alphabet.find(letter)

    return numerical_pos

def rotate_character(char, rot):
    """receives a character and an int 'rot', and rotates char by rot number of places to the right."""
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    alpha_dict = dict(enumerate(alphabet))
    new_char = ""
    # ignore and return non-alphabetic characters
    if char not in alphabet:
        return char
    # temporarily convert case to locate in alphabet
    temp_char = char.lower()
    if temp_char in alphabet:
        # get original position of letter
        orig_pos = alphabet_position(temp_char)
        # take char, add rot to its index, mod 25 - 1
        new_pos = (int(orig_pos) + int(rot))
        if new_pos > 25:
            new_pos = (new_pos % 25) - 1
        elif new_pos < 0:
            # avoid negative index error when rot is 26
            new_char = char
        # converting new_pos back to alpha
        else:
            new_char = alpha_dict[new_pos]
        # if original char was upper/lower, return corresponding case for new_char
        if char.islower():
            new_char.lower()
        else:
            new_char.upper()
        return new_char
from helpers import alphabet_position, rotate_character
# ------
# caesar cipher
# ------

def encrypt(text, rot):
    new_text = ""
    for char in text:
        new_char = rotate_character(char, rot)
        new_text += str(new_char)

    return new_text

def main():
    input_text = input("Enter a sentence to encrypt: ")
    input_rot = int(input("Number of rotations: "))

    print(encrypt(input_text, input_rot))

if __name__ == "__main__":
    main()
from helpers import alphabet_position, rotate_character
# ------
# vigenere cipher
# ------
def encrypt(text, rot):
    lower_alphabet = "abcdefghijklmnopqrstuvwxyz"
    upper_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    keystream = 0
    encrypted = ""
    rot.lower()
    for i in range(len(text)):
        keychar = keystream % len(rot)
        if text[i] in lower_alphabet:
            new_char = rotate_character(text[i], alphabet_position(rot[keychar]))
            new_char.lower()
            encrypted += new_char
            keystream += 1
        elif text[i] in upper_alphabet:
            new_char = rotate_character(text[i], alphabet_position(rot[keychar]))
            new_char.upper()
            encrypted += new_char
            keystream += 1            
        else:
            encrypted += text[i]
        # stuck around here... vigenere isn't printing shifted capital letters and I
        # can't figure out why.
    return encrypted

def main():
    input_text = input("Enter a sentence to encrypt: ")
    input_rot = input("Enter your keyword: ")

    print(encrypt(input_text, input_rot))

if __name__ == "__main__":
    main()

编辑:编辑辅助函数以使代码更清晰......现在,当我在凯撒输入pYthon轮换4时,它只打印一个大写字母。我输了。

0 个答案:

没有答案