栅栏密码(Fence crypto)
生活随笔
收集整理的這篇文章主要介紹了
栅栏密码(Fence crypto)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
柵欄密碼(Fence crypto)
-
加密對象: 所有字符
-
原理:
-
該密碼是一種移位密碼,將明文的順序按照某種規則打亂
-
該密碼需要一個秘鑰,即在加密過程中需要使用到的列數,將明文按順序排成列,例如:列數為4,明文為: “i will beat you this day”,
iwi l l b e a t y o u t h i s d a y -
然后按照列順序組成明文,如表密文為: 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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于QT实现的数独游戏DPLL的SAT求
- 下一篇: 苹果 macOS 12.4 RC 发布,