自定义 encryption/decryption 程序中的错误

Bug in a custom encryption/decryption program

我正在 python 3.5 中创建加密软件。它应该沿着一个键,使用 key[0] 移动 raw[0],然后使用 key[1] 移动 raw[1] 等,当 raw[i] 大于 key[i] 时回到 key[0] %len(密钥)]。

# Converts the key into a numerical list. 
def convert(alph, key):
  for i in range(0, len(key)):
    rem = alph.index(key[i])
    numkey.append(rem)
    print(numkey)
  return numkey

#shifts the text dependant on the key
def encrypt (numkey, raw, alph):
  encr = ""
  emi = ()
  emi = list(emi)
  for i in range (0, len(raw)):
    rem = raw[i]
    rem = alph.index(rem)
    suba = i%len(numkey)
    ram = numkey[suba]
    shift = (rem + ram) % 28  #ensures that shift is an index of alph

    shift = alph[shift]
    emi.append(shift)
  for i in range(0, len(emi)):
    encr = encr + str(emi[i])
  print (encr)

letters = [
    ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 
    'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 's', 'u', 'v', 'w',
    'x', 'y', 'z', '.', ',', '!', '?']

raw_key = input("Please enter the key:\n")
raw_text = input("Please enter the text you would like to encrypt (no numbers or capitals):")
numkey = convert(letters, raw_key)
encrypt(numkey, raw_text, letters)

我的问题是解密程序(如下)。

# Converts the key into a numerical list. 
def convert(alph, key):
  numkey = ()
  numkey = list(numkey)  # parse numkey as list
  for i in range(0, len(key)):
    rem = alph.index(key[i])
    numkey.append(rem)
  return numkey

# shifts the text dependant on the key
def encrypt (numkey,raw,alph):
  encr = ""
  emi = ()
  emi = list(emi)
  for i in range (0, len(raw)):
    rem = raw[i]
    rem = alph.index(rem)
    suba = i%len(numkey)
    ram = numkey[suba]
    shift = (rem - ram)

    if shift < 0:
        shift = shift + 28
    else:
        pass        
    shift = alph[shift]
    emi.append(shift)
  for i in range(0, len(emi)):
    encr = encr + str(emi[i])
  print (encr)

letters = [
    ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 
    'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 's', 'u', 'v', 'w',
    'x', 'y', 'z', '.', ',' ,'!' ,'?']

raw_key = input("Please enter the key:\n")
raw_text = input("Please enter the text you would like to decrypt:\n")
numkey = convert(letters, raw_key)
encrypt(numkey, raw_text, letters)

由于某种原因,加密字符“,”,“?”后&“!”,如果我通过解密传递它们,它们总是 returns 分别为“ ”、"a" 和 "b"。这不是字符列表中任何其他元素的问题。

如果有人能发现问题,我将不胜感激。

问题出在加密程序中:

shift = (rem + ram) % 28

letters 的长度是 31 而不是 28。这是您过早地循环回到数组开头的地方。

问题反映在解密程序中:

shift = shift + 28

还有其他问题。举几个例子:

  • 在加密程序中numkey没有在convert()中初始化
  • 不用range(),直接用for char in key:
  • 不需要 lst = () 后跟 lst = list(lst) 模式,只需首先使用列表,lst = []
  • 不检查无效字符
  • 函数在解密程序中仍命名为encrypt()

这是清理两者的快速第一步。

加密:

import sys

LETTERS = (
    ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
    'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 's', 'u', 'v', 'w',
    'x', 'y', 'z', '.', ',', '!', '?')

# Converts the key into a numerical list.
def convert(alph, key):
  numkey = []
  for char in key:
    if char not in alph:
      sys.exit("Invalid character")
    numkey.append(alph.index(char))
  print(numkey)
  return numkey

# Shifts the text dependant on the key.
def encrypt (numkey, raw, alph):
  encr = ""
  for i, char in enumerate(raw):
    if char not in alph:
      sys.exit("Invalid character")
    rem = alph.index(char)
    ram = numkey[i % len(numkey)]
    # Ensure that shift is an index of alph
    shift = (rem + ram) % len(alph)
    encr = encr + alph[shift]
  print(encr)

raw_key = input("Please enter the key: ")
raw_text = input("Please enter the text you would like to encrypt (no numbers or capitals):\n")

numkey = convert(LETTERS, raw_key)
encrypt(numkey, raw_text, LETTERS)

解密:

import sys

LETTERS = (
    ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
    'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 's', 'u', 'v', 'w',
    'x', 'y', 'z', '.', ',' ,'!' ,'?')

# Converts the key into a numerical list.
def convert(alph, key):
  numkey = []
  for char in key:
    if char not in alph:
      sys.exit("Invalid character")
    numkey.append(alph.index(char))
  return numkey

# Shifts the text dependant on the key.
def decrypt(numkey, raw, alph):
  decr = ""
  for i, char in enumerate(raw):
    if char not in alph:
      sys.exit("Invalid character")
    rem = alph.index(char)
    ram = numkey[i % len(numkey)]
    shift = rem - ram
    if shift < 0:
        shift = shift + len(alph)
    decr = decr + alph[shift]
  print(decr)

raw_key = input("Please enter the key: ")
raw_text = input("Please enter the text you would like to decrypt:\n")

numkey = convert(LETTERS, raw_key)
decrypt(numkey, raw_text, LETTERS)