python实现维吉尼亚加解密(I/O)

维吉尼亚加密

加密

def Virginia_encryption():
#加密
    flag = 0
    while (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=1
            for 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 + 1
                elif i in a3:
                    b = key[count % len(key)]
                    b1 = a3.index(b)
                    x = (a3.index(i) + b1) % 26
                    putlist.append(a3[x])
                    count = count + 1
                else:
                    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 = 1

        except Exception:
            print("文件打开失败,请检查文件路径是否正确")
Virginia_encryption()

解密


def Virginia_decrypt():
    flag = 0
    while (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=1
            for 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 + 1
                elif 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 + 1
                else:
                    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 = 1

        except Exception:
            print("文件打开失败,请检查文件路径是否正确")
Virginia_decrypt()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值