python 解密中国换位密码。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 解密中国换位密码。相关的知识,希望对你有一定的参考价值。

# Solving Chinese Columnar Transposition Cipher

list_of_words = ['KLTNS AUEIA RDNHC', 'NABEK TYRBO', 'ENDTW YSILA',
                 'OJS BET SVE', 'RASRF EUQUO', 'TAB EGI SLL', 'KMNE SUOL',
                 'OONC DRAR LOII ANTS', 'IENIREA NTSETBL', 'OSAJAHM NKCLECI']


def solve_chinese_cipher(cipher_text):
        """ Given encrypted cipher_text, returns decrypted secret_message."""
        components = cipher_text.split(' ')
        # print components

        num_components = len(components)
        # print "num_components = ", num_components

        component_length = len(components[0])
        # print "component_length = ", component_length

        cipher_matrix = []
        for component in components:
            cipher_matrix.append(list(component))

        decrease_i = False
        total_letters = component_length * num_components

        count_letters = 1
        once_more = False
        i = 0
        j = component_length - 1
        secret_message = [cipher_matrix[i][j]]
        index = 0
        while j >= 0:
            while i >= 0:
                if once_more:
                    secret_message.append(cipher_matrix[i][j])
                    count_letters += 1

                if decrease_i:
                    i -= 1
                else:
                    i += 1

                if i == 0:

                    secret_message.append(cipher_matrix[i][j])
                    count_letters += 1
                    decrease_i = False
                    if not once_more:
                        once_more = True
                    j -= 1

                elif i == (num_components - 1):

                    secret_message.append(cipher_matrix[i][j])
                    count_letters += 1
                    decrease_i = True
                    if not once_more:
                        once_more = True
                    j -= 1

                else:
                    secret_message.append(cipher_matrix[i][j])
                    count_letters += 1
                    once_more = False

                if count_letters == total_letters:
                    break

            if j == 0 and (count_letters == total_letters):
                break

        index += 1
        secret_message = "".join(secret_message)
        return secret_message


def main():

    for cipher_text in list_of_words:
        secret_message = solve_chinese_cipher(cipher_text)
        print secret_message


if __name__ == "__main__":
    main()

以上是关于python 解密中国换位密码。的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 2075. 解码斜向换位密码

SM3加密与解密校验

python-凯撒密码加解密

2021-05-07

对称加密算法之DES介绍

Python中的密码加密和解密