一、实验目的
编制基本的文本加、解密程序
二、实验内容
凯撒加解密、维吉尼亚表加解密、DES加解密
三、实验要求
使用任意高级语言(Python)做出给文本文件加、解
密的软件.
四、实验步骤:
参考有关内容,编制程序,界面要求如下
参考:凯撒加解密
def KaiSaEncrypt(ch, k):
if (not isinstance(ch, str)) or len(ch)!=1:
print('The first parameter must be a character')
return
if (not isinstance(k, int)) or (not 1<=k<=25):
print('The second parameter must be an integer between 1 and 25')
return
if 'a'<=ch<=chr(ord('z')-k): #把英文字母变换为后面第k个字母
return chr(ord(ch)+k)
elif chr(ord('z')-k)
elif 'A'<=ch<=chr(ord('Z')-k):
return chr(ord(ch)+k)
elif chr(ord('Z')-k)
else:
return ch
def encrypt(plain, k):
return ''.join([KaiSaEncrypt(ch, k) for ch in plain])
def KaiSaDecrypt(ch, k):
if (not isinstance(ch, str)) or len(ch)!=1:
print('The first parameter must be a character')
return
if (not isinstance(k, int)) or (not 1<=k<=25):
print('The second parameter must be an integer between 1 and 25')
return
if chr(ord('a')+k)<=ch<='z': #然后把每个字母变换为前面第k个字母
return chr(ord(ch)-k)
elif 'a'<=ch
elif chr(ord('A')+k)<=ch<='Z':
return chr(ord(ch)-k)
elif 'A'<=ch
else:
return ch
def decrypt(plain, k):
return ''.join([KaiSaDecrypt(ch, k) for ch in plain])
plainText = 'Explicit is better than implicit.'
cipherText = encrypt(plainText, 5)
print(plainText)
print(cipherText)
print(decrypt(cipherText,5))
程序运行结果为:
Explicit is better than implicit.
Jcuqnhny nx gjyyjw ymfs nruqnhny.
Explicit is better than implicit.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)