首页 > 解决方案 > 自定义加密/解密程序中的错误

问题描述

我正在 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)

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

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

标签: encryptionpython-3.5

解决方案


问题出在加密程序中:

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)

推荐阅读