defVirginia_encryption():#加密
flag =0while(flag ==0):try:print("please enter encryption file path")
filepath =input("path:")
key =input("key(Must be a number):")
file1 =open(filepath,'r+', encoding='UTF-8')
a = file1.read()print("原文内容为下")print(a)
alphabet ="a b c d e f g h i j k l m n o p q r s t u v w x y z"
a1 = alphabet.upper()
a2 = alphabet.split(' ')
a3 = a1.split(' ')
putlist =[]
count=1for i in a:if i in a2:
b = key[count %int(len(key))]
b1 = a2.index(b)
x =(a2.index(i)+ b1)%26
putlist.append(a2[x])
count = count +1elif i in a3:
b = key[count %len(key)]
b1 = a3.index(b)
x =(a3.index(i)+ b1)%26
putlist.append(a3[x])
count = count +1else:
putlist.append(i)
count = count +1
out =''.join(putlist)
file1.close()print("Please enter the ciphertext storage path,For example ")
filepath =input("path:")
file2 =open(filepath,'w+', encoding='UTF-8')
file2.write(out)print("维吉尼亚加密成功,密文路径为:%s"%filepath)
flag =1except Exception:print("文件打开失败,请检查文件路径是否正确")
Virginia_encryption()
解密
defVirginia_decrypt():
flag =0while(flag ==0):try:print("请输入加密文件路径")
filepath =input("path:")
file1 =open(filepath,'r+', encoding='UTF-8')
key =input("key(Must be a number):")
a = file1.read()print("原文内容为下")print(a)
alphabet ="a b c d e f g h i j k l m n o p q r s t u v w x y z"
a1 = alphabet.upper()
a2 = alphabet.split(' ')
a3 = a1.split(' ')
putlist =[]
count=1for i in a:if i in a2:
b = key[count %int(len(key))]
b1 = a2.index(b)
x =(a2.index(i)+(26-b1))%26
putlist.append(a2[x])
count = count +1elif i in a3:
b = key[count %len(key)]
b1 = a3.index(b)
x =(a3.index(i)+(26-b1))%26
putlist.append(a3[x])
count = count +1else:
putlist.append(i)
count = count +1
out =''.join(putlist)
file1.close()print("请输入解密后文件存放路径")
filepath =input("path:")
file2 =open(filepath,'w+', encoding='UTF-8')
file2.write(out)print("维吉尼亚解密成功,密文路径为:%s"%filepath)
flag =1except Exception:print("文件打开失败,请检查文件路径是否正确")
Virginia_decrypt()