0

我正在尝试在 python 中实现简化的 DES 以用于学习目的,但我无法弄清楚如何根据“时间表”进行排列。本质上,我有一个具有适当排列的元组,我需要移位到正确的位置。

例如,使用一个键:

K = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001

将第 57 位移动到第一位点,将第 49 位移动到第二位点,依此类推...

K+ = 1111000 0110011 0010101 0101111 0101010 1011001 1001111 0001111

当前代码:

def keyGen(key):
    PC1table = (57, 49, 41, 33, 25, 17,  9,
                 1, 58, 50, 42, 34, 26, 18,
                10,  2, 59, 51, 43, 35, 27,
                19, 11,  3, 60, 52, 44, 36,
                63, 55, 47, 39, 31, 23, 15,
                 7, 62, 54, 46, 38, 30, 22,
                14,  6, 61, 53, 45, 37, 29,
                21, 13,  5, 28, 20, 12,  4)

    keyBinary = bin(int(key, 16))[2:].zfill(64)
    print keyBinary
    permute(PC1table, keyBinary)

def permute(permutation, permuteInput):
    elements = list(enumerate(permutation))
    for bit in permuteInput:
         ***magic bitshifting goes here***


keyGen("133457799BBCDFF1")

我认为可行的逻辑是枚举排列的元组,对于我的旧密钥的每一位,在枚举中查找与该位对应的索引,并移位适当的次数,但我只是可以'不知道如何去做。可能是我从错误的角度处理问题,但任何指导将不胜感激!

4

1 回答 1

0

好的,我最终想出了一种方法来完成这项工作,尽管这可能不是最有效的方法......

在调用函数之前,将二进制数转换为列表:

keyBinary = bin(int(key, 16))[2:].zfill(64)
keyBinary = [int(i) for i in keyBinary]

Kplus = permute(PC1table, keyBinary)

def permute(mapping, permuteInput):
    permuteOutput = []

    for i in range(len(mapping)):
        permuteOutput.append(permuteInput[mapping[i % 56] - 1])

    return permuteOutput

如果有人有更好的方法来解决这个问题,我很想看看你的解决方案!

于 2013-02-13T00:34:48.520 回答