python实现凯撒加解密(i/o流)

凯撒加解密(基于文件层面的操作)

凯撒加密

def caesar_encryption():
    """
    在密码学中,恺撒密码(英语:Caesar cipher),或称恺撒加密、恺撒变换、变换加密,是一种最简单且最广为人知的加密技术。
    它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。
    例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。
    """
    flag = 0
    while (flag == 0):
        try:
            print("please enter file path,For example D:/text.txt")
            filepath = input("path:")
            file1 = open(filepath, 'r+', encoding='UTF-8')
            a = file1.read()
            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 = []
            for i in a:
                if i in a2:
                    x = (a2.index(i) + 3) % 26
                    putlist.append(a2[x])
                elif i in a3:
                    x = (a3.index(i) + 3) % 26
                    putlist.append(a3[x])
                else:
                    putlist.append(i)
            out = ''.join(putlist)
            file1.close()
            print("Please enter the ciphertext storage path,For example D:/text.txt")
            filepath = input("path:")
            file2 = open(filepath, 'w+', encoding='UTF-8')
            file2.write(out)
            print("加密成功,密文路径为:%s"%filepath)
            flag = 1

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

凯撒解密

def caesar_decrypt():
  flag = 0
  while (flag == 0):
      try:
          print("please enter ciphertext storage path path,For example D:/text.txt")
          filepath = input("path:")
          file1 = open(filepath, 'r+', encoding='UTF-8')
          key = int(input("key(Must be a number):"))
          a = file1.read()
          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 = []
          for i in a:
              if i in a2:
                  x = (a2.index(i) + (26-key)) % 26
                  putlist.append(a2[x])
              elif i in a3:
                  x = (a3.index(i) + (26-key)) % 26
                  putlist.append(a3[x])
              else:
                  putlist.append(i)
          out = ''.join(putlist)
          file1.close()
          print("Please enter the Decrypt file,For example D:/text.txt")
          filepath = input("path:")
          file2 = open(filepath, 'w+', encoding='UTF-8')
          file2.write(out)
          print("解密成功,密文路径为:%s"%filepath)
          flag = 1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值