简单的密码功能,格式化输出

时间:2015-06-11 13:56:06

标签: python encryption

我写了一个小密码程序用来发送有趣的秘密消息,它工作得很好,但我想略微改变输出。代码是

base_alfa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]

shift_4_alfa = ["e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", " "]

def encrypt_4(text):
    alfa_dir = []
    encrypted_text = []
    for i in text:
        if i in base_alfa:
            alfa_dir.append(base_alfa.index(i))
    for j in alfa_dir:
        encrypted_text.append(shift_8_alfa[j])
    return "".join(encrypted_text)

将以完全相同的间距打印字符。例如加密(“这很难读”)==“xlmw mw hmjjmgypx xs vieh”

我想要的是输出以五个为一组重新组合字符,如下所示:“xlmwm whmjj mgypx svieh”

关于实现这一目标的最佳方法的任何想法?

1 个答案:

答案 0 :(得分:0)

这样的事情:

groups = []
group = []
for c in "xlmw mw hmjjmgypx xs vieh".replace(" ", ""):
    group.append(c)
    if len(group) == 5:
        groups.append("".join(group))
        group = []
if group:
    groups.append("".join(group))
print(" ".join(groups))