【问题标题】:Python Auto Encryption ProgramPython自动加密程序
【发布时间】:2017-04-18 18:02:03
【问题描述】:

大家好,我是 python 的初学者,正在尝试制作一个使用我定义的加密密钥的加密代码,例如我制作了这段代码,但它不起作用:

def encrypt():
    A = "f"
    B = "d"
    C = "z"
    T = "x"
    first = input()
    print(first)

我希望程序的功能就像用户在输入中输入 CAT 时输出应该是 zax 但是当我输入时甚至 A在我当前的程序中它打印 A 原样。

任何帮助将不胜感激!

【问题讨论】:

  • 使用dict:enc = {'A': 'f', 'B': 'd', ...},然后使用enc[first]
  • 你实际上并没有对输入做任何事情。如果你想改变它,你需要对first 执行一些操作。
  • @RocketHazmat 好的,让我看看它是否有效!
  • 请参阅stackoverflow.com/a/21038977/3901060,了解使用str.translate 方法执行此操作的示例。
  • CAT 不应该变成 zfx 吗?而不是 zax?

标签: python encryption


【解决方案1】:

您可以使用string.translate 方法。示例代码是:

def encrypt():
    encryption_table = {
        ord('A'):ord('f'),
        ord('B'):ord('d'),
        ord('C'):ord('z'),
        ord('T'):ord('x'),                        
    }
    first = input()
    print(first.translate(encryption_table))

对于此代码,输入 CAT 将返回 zfx。

【讨论】:

    【解决方案2】:

    首先你应该制作一个字典来映射你的字母替换,例如:

    my_dict = { 'A':'f', 'B':'d', 'C':'z', 'T':'x', ... }  
    

    那么你需要一个encrypt 函数来返回这个字典的值:

    def encrypt(data):
        return ''.join( my_dict[d] if d in my_dict else d for d in data )
    

    还有一个 decrypt 功能相反:

    def decrypt(data):
        my_dict_rev = dict((v,k) for k,v in my_dict.items())
        return ''.join( my_dict_rev[d] if d in my_dict_rev else d for d in data )
    

    现在让我们测试一下:

    my_data = 'TEST DATA'
    enc_data = encrypt(my_data)
    dec_data = decrypt(enc_data)
    

    输出:

    print(my_data)
    print(enc_data)
    print(dec_data)
    
    TEST DATA
    xESx Dfxf
    TEST DATA
    

    【讨论】:

      【解决方案3】:

      如果你想使用,这里有一个简单的加密和解密脚本。密码部分可以省略,也可以保留。加载栏也是如此。加载栏位于第 1-4 行,密码位于最后 9 行。如果您确实取出密码,请确保调用开始。您还需要通过 pip 安装 tqdm。

      import time
      from tqdm import *
      for i in tqdm(range(1000)):
          time.sleep(0.001)
      def encrypt(some_string):
          alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
          cipher =   'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA'
          encryption = ''
          for char in some_string:
              if(alphabet.find(char) == -1):
                  encryption = encryption + char
              else:
                  position = alphabet.index(char)
                  encryption = encryption + cipher[position]
          return encryption
      def decrypt(some_string):
          cipher =   'bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZa'
          alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
          decryption = ''
          for char in some_string:
              if(cipher.find(char) == -1):
                  decryption = decryption + char
              else:
                  position = cipher.index(char)
                  decryption = decryption + alphabet[position]
          return decryption   
      def restart():
          print('Would you like to restart.')
          print('1: Yes.')
          print('2: No.')
          c = float(input('Type a 1 or a 2: '))
          if c == 1:
              print('Ok you can restart.')
              start()
          if c == 2:
              print('Sorry to see you leave.')
              quit()
          else:
              print('Invalid response.')
              restart()
      def start():
          print('1: Encryption')
          print('2: Decryption')
          e = float(input('Type a 1 or a 2: '))
          if e == 1:
              print('Type your message here.')
              e = str(input('Enter message to encrypt: '))
              print(encrypt(e))
              restart()
          if e == 2:
              print('Type your messed up message here.')
              e = str(input('Enter your messed up message: '))
              print(decrypt(e))
              restart()
          else:
              print('Please type a 1 or a 2.')
              start()
      def password():
          a = float(input('Enter the correct password please: '))
          if a == 5194703:
              print('Correct!')
              start()
          else:
              print('Invalid Password, try again!')
              password()
      password()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-28
        • 2018-04-25
        • 2011-02-28
        • 2014-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多