栅栏密码(Fence crypto)

简介: 栅栏密码(Fence crypto)

栅栏密码(Fence crypto)

  • 加密对象: 所有字符
  • 原理:
  • 该密码是一种移位密码,将明文的顺序按照某种规则打乱
  • 该密码需要一个秘钥,即在加密过程中需要使用到的列数,将明文按顺序排成列,例如:列数为4,明文为: “i will beat you this day”,
i w i
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 ciphertext
def decrypt_fence(string, key):
   plaintext = ""
   length = len(string)
   min_row = length // key
   max_num = length % key
   temp = []
   index = 0
   for i in range(key):
       if i < max_num:
           temp.append(string[index:index+min_row+1])
           index += min_row + 1
       else:
           temp.append(string[index:index+min_row])
           index += min_row
   # print(temp)
   for i in range(length):
       plaintext += temp[i % key][i // key]
   return plaintext
if __name__ == '__main__':
   key_ = 4
   ciphertext_ = encrypt_fence("i will beat you this day", key_)
   plaintext_ = decrypt_fence(ciphertext_, key_)
   print(f"{plaintext_} : {ciphertext_}")


目录
相关文章
|
6月前
|
Linux 数据安全/隐私保护 Windows
aes加密在linux下会生成随机key的解决办法
aes加密在linux下会生成随机key的解决办法
74 2
|
6月前
|
Linux
linux启动卡一会在random: nonblocking pool is initialized之前
linux启动卡一会在random: nonblocking pool is initialized之前
586 1
|
6月前
|
Linux 网络安全
Linux(16)ssh_exchange_identification: read: Connection reset by peer问题
Linux(16)ssh_exchange_identification: read: Connection reset by peer问题
58 0
|
6月前
|
安全 网络安全
jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 如何处理
【5月更文挑战第24天】jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 如何处理
479 1
|
SQL 关系型数据库 MySQL
MySQL因为hung住,自动重新启动,InnoDB: Semaphore wait has lasted > 600 seconds
从错误日志中检查,有两个信号量等待时间超长,MySQL自动crash了
411 0
|
Linux 数据安全/隐私保护 Windows
AES在windows下正常加解密,Linux下加密正常,解密异常(javax.crypto.BadPaddingException: pad block co
AES在windows下正常加解密,Linux下加密正常,解密异常(javax.crypto.BadPaddingException: pad block co
185 1
|
网络协议 Linux 网络安全
如何保持 SSH 会话不中断?
如何保持 SSH 会话不中断?
|
Linux
Linux重新启动后提示 Failed to load SELinux policy. Freezing系统hung住
重新启动之前修改了selinux的配置,disable selinux,估计多半是修改的时候哪里改错了。
219 0
|
Shell 开发工具 Android开发
如何确认 fastboot unlock 解锁成功,如何确认DM-verity 已关闭
如何确认 fastboot unlock 解锁成功,如何确认DM-verity 已关闭
1004 0
|
Linux 网络安全
Linux下的github 添加秘钥出错:Key is invalid. You must supply a key in OpenSSH public key for
Linux下的github 添加秘钥出错:Key is invalid. You must supply a key in OpenSSH public key for
Linux下的github 添加秘钥出错:Key is invalid. You must supply a key in OpenSSH public key for