欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

栅栏密码(Fence crypto)

发布时间:2023/12/31 编程问答 51 豆豆
生活随笔 收集整理的这篇文章主要介绍了 栅栏密码(Fence crypto) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

栅栏密码(Fence crypto)

  • 加密对象: 所有字符

  • 原理:

    • 该密码是一种移位密码,将明文的顺序按照某种规则打乱

    • 该密码需要一个秘钥,即在加密过程中需要使用到的列数,将明文按顺序排成列,例如:列数为4,明文为: “i will beat you this day”,

      iwi
      llb
      eat
      you
      this
      day
    • 然后按照列顺序组成明文,如表密文为: ileyt  laohdw tuiaib  sy

  • 代码:

    # write by 2021/8/5 # 栅栏密码def encrypt_fence(string, key):ciphertext = ""temp = []for i in range(key):temp.append("")for index, i in enumerate(string):temp[index % key] += i# print("".join(temp))ciphertext = "".join(temp)return ciphertextdef decrypt_fence(string, key):plaintext = ""length = len(string)min_row = length // keymax_num = length % keytemp = []index = 0for i in range(key):if i < max_num:temp.append(string[index:index+min_row+1])index += min_row + 1else:temp.append(string[index:index+min_row])index += min_row# print(temp)for i in range(length):plaintext += temp[i % key][i // key]return plaintextif __name__ == '__main__':key_ = 4ciphertext_ = encrypt_fence("i will beat you this day", key_)plaintext_ = decrypt_fence(ciphertext_, key_)print(f"{plaintext_} : {ciphertext_}")

总结

以上是生活随笔为你收集整理的栅栏密码(Fence crypto)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。