初学Python,想通过练习密码的加解密来逐渐掌握python的基本使用,下面是代码
'''
栅栏密码
可以根据num分栏
'''
import math
def encryption(plaintext,num):
tmp_dict = {}
#初始化字典
for i in range(num):
tmp_dict[i] = []
#根据num 进行分组赋值(核心代码)
for i in range(len(plaintext)):
tmp_dict[i%num].append(plaintext[i])
#从tmp_dict遍历出密文然后进行拼接
ciphtertext = []
for j in tmp_dict.values():
j = ''.join(j)
ciphtertext.append(j)
ciphtertext = ''.join(ciphtertext)
print(f'密文为: {ciphtertext}')
def decryption(ciphertext,num):
plaintext = []
t = int(len(ciphertext)/num)
r = len(ciphertext) % num
b = list(range(1,r+1))
c = len(range(num)) - len(b)
#用0补充b,使其长度和num一样
for i in range(c):
b.append(0)
# print(r,b)
if r != 0:
for i,j in zip(range(num),b):
a = 1
#余数最多只有9
if j == 1:
plaintext.append(ciphertext[0 + t * i:a + t + t * i])
elif j >= 2:
plaintext.append(ciphertext[0 + (j-1) * a + t * i:j * a + t + t * i])
else:
# print(f'b,j,r分别为{b,j,r}')
j = b[j+r-2]
plaintext.append(ciphertext[0 + j*a + 1 + t * i:j*a + 1 + t + t * i])
else:
t = math.ceil(len(ciphertext)/num)
for i in range(num):
plaintext.append(ciphertext[0 + t * i: t + t * i])
print(f'栅栏为{num}时,明文为: ')
for i in range(math.ceil(len(ciphertext)/num)):
for j in range(len(plaintext)):
try:
# print(plaintext)
print(plaintext[j][i],end='')
except:
pass
print()
if __name__ == '__main__':
enter = int(input('加密输入0\n解密输入1\n无密钥暴力破解输入2\n'))
# enter = 2
#加密
if enter == 0:
plaintext = input('请输入明文: ').lower()
num = int(input('总共将明文分为为num栏,请输入num: '))
print(f'明文共{len(plaintext)}位')
print('-----------------------------')
# plaintext = 'stay hungry stay foolishstay hungry stay foolish'
# num = 4
if len(plaintext) % num != 0:
plaintext = plaintext + ' '
encryption(plaintext,num)
#解密
if enter == 1:
ciphertext = input('请输入密文: ')
num = int(input('请输入栅栏数: '))
# ciphertext = 'qhaiuinazgiiys uhsu'
# num = 7
print(f'栅栏数为{num},密文长度为{len(ciphertext)}')
decryption(ciphertext,num)
#不知道栅栏数时进行解密,初始设定为256,可以自己调
if enter == 2:
# ciphertext = 'qhaiuinazgiiys uhsu'
num = 2
ciphertext = input('请输入密文: ')
decryption(ciphertext, num)
while num < 257:
if num <= len(ciphertext):
print(f'栅栏数为{num},密文长度为{len(ciphertext)}')
decryption(ciphertext,num)
num = num + 1
else:
print(f'\n目前{len(ciphertext)}位,栅栏在{num}时被停止')
break
# print('--------破解完毕--------')
- 可以把while < 257 改成其他栅栏数方便进行更多的遍历
- 解密的时候不会自动去空格,所以输入的时候不要多输或少输,会影响结果