加密柱状转置密码

2024-03-15

我试图弄清楚如何在给定明文大写字符串和任意长度的数字密钥的情况下加密 Python 中的柱状转置密码。例如,如果键是 3124 并且字符串是“IHAVETWOCATS”,它将像这样组织字符串:

3124
IHAV
ETWO
CATS

然后先返回第1列中的字符,然后返回第2列中的字符,以此类推,直到最后返回加密后的字符串'HTAAWTIECVOS'。到目前为止,我知道我需要使用累加器,并且我一直在考虑使用字典的想法,但我完全陷入困境。这些是我尝试过的一些功能:

def columnar(plaintext,key):
    cipher=''
    acc=0
    for i in range(len(key)):
        while acc<(len(plaintext)/len(key)):
            cipher=cipher+plaintext[i+acc*5]
            acc=acc+1
    return(cipher)

^这仅返回几个字母,而不是适当长度的字符串。

def columnar(plaintext,key) values={} seqlist=[] nextvalue=1 indices=rand(len(key)) for letter in plaintext: for i in indices: if letter==key[i]: values[i]=nextvalue nextvalue=nextvalue+1 for i in indices: seqlist.append(values[i]) return seqlist

^上述函数返回 KeyError: 0 错误。 非常感谢您的帮助!


def encode(txt,key):
    sz = len(key)  # how big are the columns 
    cols = list(map("".join,zip(*zip(*[iter(txt)]*sz)))) # list partitioned into columns
    return "".join([cols[key.index(str(c))] for c in range(1,sz+1)])



encoded = encode("IHAVETWOCATS","3124")
print encoded

我可能会这样做

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

加密柱状转置密码 的相关文章

随机推荐