维吉尼亚密码(又译维热纳尔密码)是使用一系列凯撒密码组成密码字母表的加密算法,属于多表密码的一种简单形式。相关描述可以看百度百科,个人认为比较全面详尽。
此处给出我的代码(大概会存在一些变量命名的不规范)
需注意明文与密钥应全部使用大写字母(因为给出的字母表中全是大写字母)
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def encode():
default = input('input your contents here:')
key = input('input your key here:')
cipher = ''
for i in range(len(default)):
cipher += alphabet[(alphabet.find(default[i]) + alphabet.find(key[i % len(key)])) % 26]
print('the encrypted text is:', cipher)
def decode():
key = input('input the key here:')
cipher = input('input the encrypted text:')
real = ''
for i in range(len(cipher)):
real += alphabet[(alphabet.find(cipher[i]) - alphabet.find(key[i % len(key)])) % 26]
print('the decoded text is:', real)
while True:
choice = input('1.encode; 2.decode; 0.quit\n')
if choice == '1':
encode()
elif choice == '2':
decode()
else:
print('quit successfully!')
break
- 按1输入对应的要加密的文本与密钥(key)可以进行加密
- 按2输入对应要解密的文本与密钥可以进行解密
- 按0结束循环
有问题诚恳请求大家指出改正。