栅栏密码(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_}")


目录
相关文章
|
7月前
|
Linux
linux启动卡一会在random: nonblocking pool is initialized之前
linux启动卡一会在random: nonblocking pool is initialized之前
643 1
|
7月前
|
Linux 网络安全
Linux(16)ssh_exchange_identification: read: Connection reset by peer问题
Linux(16)ssh_exchange_identification: read: Connection reset by peer问题
64 0
|
7月前
|
安全 网络安全
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> 如何处理
609 1
|
7月前
|
安全 网络安全
jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha>问题处理方法
【5月更文挑战第10天】jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha>问题处理方法
320 0
|
Linux 数据安全/隐私保护 Windows
AES在windows下正常加解密,Linux下加密正常,解密异常(javax.crypto.BadPaddingException: pad block co
AES在windows下正常加解密,Linux下加密正常,解密异常(javax.crypto.BadPaddingException: pad block co
206 1
|
Go 数据库 数据安全/隐私保护
Go实现随机加盐密码认证
Go实现随机加盐密码认证
315 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
真正解决:gpg --verify sig: 无法检查签名:找不到公钥
真正解决:gpg --verify sig: 无法检查签名:找不到公钥
361 0
|
Java 数据安全/隐私保护
cas server +cas client 单点登录配置实例
cas server 配置 首先你要下一个 cas server 。如果你要有所了解的话,可以下载一个cas server source。 使用ide 打开 cas server ,maven 构建,jetty 运行。
1984 0