嵿尖势力 2022-10-18 16:20 采纳率: 64.3%
浏览 46
已结题

信息安全,Cryptography密码学

假设我们要使用带加密密钥3的铁路围栏密码解密消息“daxsarineaistestsmoy”。请显示您如何继续操作的步骤。

img

  • 写回答

1条回答 默认 最新

  • 游一游走一走 2022-10-18 16:34
    关注

    drtiansexamissoeasy

    # Python3 program to illustrate
    # Rail Fence Cipher Encryption
    # and Decryption
    
    # function to encrypt a message
    def encryptRailFence(text, key):
    
        # create the matrix to cipher
        # plain text key = rows ,
        # length(text) = columns
        # filling the rail matrix
        # to distinguish filled
        # spaces from blank ones
        rail = [['\n' for i in range(len(text))]
                for j in range(key)]
    
        # to find the direction
        dir_down = False
        row, col = 0, 0
    
        for i in range(len(text)):
    
            # check the direction of flow
            # reverse the direction if we've just
            # filled the top or bottom rail
            if (row == 0) or (row == key - 1):
                dir_down = not dir_down
    
            # fill the corresponding alphabet
            rail[row][col] = text[i]
            col += 1
    
            # find the next row using
            # direction flag
            if dir_down:
                row += 1
            else:
                row -= 1
        # now we can construct the cipher
        # using the rail matrix
        result = []
        for i in range(key):
            for j in range(len(text)):
                if rail[i][j] != '\n':
                    result.append(rail[i][j])
        return("" . join(result))
    
    # This function receives cipher-text
    # and key and returns the original
    # text after decryption
    def decryptRailFence(cipher, key):
    
        # create the matrix to cipher
        # plain text key = rows ,
        # length(text) = columns
        # filling the rail matrix to
        # distinguish filled spaces
        # from blank ones
        rail = [['\n' for i in range(len(cipher))]
                for j in range(key)]
    
        # to find the direction
        dir_down = None
        row, col = 0, 0
    
        # mark the places with '*'
        for i in range(len(cipher)):
            if row == 0:
                dir_down = True
            if row == key - 1:
                dir_down = False
    
            # place the marker
            rail[row][col] = '*'
            col += 1
    
            # find the next row
            # using direction flag
            if dir_down:
                row += 1
            else:
                row -= 1
    
        # now we can construct the
        # fill the rail matrix
        index = 0
        for i in range(key):
            for j in range(len(cipher)):
                if ((rail[i][j] == '*') and
                        (index < len(cipher))):
                    rail[i][j] = cipher[index]
                    index += 1
    
        # now read the matrix in
        # zig-zag manner to construct
        # the resultant text
        result = []
        row, col = 0, 0
        for i in range(len(cipher)):
    
            # check the direction of flow
            if row == 0:
                dir_down = True
            if row == key-1:
                dir_down = False
    
            # place the marker
            if (rail[row][col] != '*'):
                result.append(rail[row][col])
                col += 1
    
            # find the next row using
            # direction flag
            if dir_down:
                row += 1
            else:
                row -= 1
        return("".join(result))
    
    # Driver code
    if __name__ == "__main__":
        print(decryptRailFence("daxsarineaisestsmoy", 3))
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 创建了问题 10月18日

悬赏问题

  • ¥15 evo评估时曲线出现问题
  • ¥15 eNSP拓扑图配置实验
  • ¥20 有没有兄弟会替换fmod studio的.bank文件内的音效?
  • ¥20 如何解决访问网址被屏蔽的情况
  • ¥15 本地运行的vue2项目,使用内网穿透,前端界面一直转圈圈
  • ¥20 powerbuilder datawindow控件导出Excel数据,可不可以不自动覆盖原数据,而是在后面新插入入数据。
  • ¥100 无轴承永磁同步电机控制
  • ¥15 eps里添加本地倾斜模型
  • ¥15 telegram 问题
  • ¥15 nrf52810-c三个a 程序