在我的凯撒密码中实施蛮力功能

时间:2018-05-24 21:11:06

标签: python function brute-force caesar-cipher

有人可以帮助我为我的Caesar Cipher代码创建一个功能,强制所有可能的关键结果吗?截至目前,我的暴力函数打印出正确的加密消息,但它不会只打印该密钥的消息。例如,(HOWDY!Hello。)给定键一个会输出(1 LSAHC!LIPPS。),这是正确的,但当它移动到下一个键时,它给出一个输出(2 LSAHC!LIPPS.KRZGB !KHOOR。)。输出包括密钥1和密钥2的消息,我希望它只包括分配给每个密钥的消息。

我的代码应输出的内容:

Do you wish to encrypt, decrypt, or brute force a message?
Enter your message:
Your translated text is:
1 LSaHc! Lipps.
2 KRZGb! Khoor.
3 JQYFa! Jgnnq.
4 IPXEZ! Ifmmp.
5 HOWDY! Hello.
6 GNVCX! Gdkkn.
7 FMUBW! Fcjjm.
8 ELTAV! Ebiil.
9 DKSzU! Dahhk.
10 CJRyT! CZggj.
11 BIQxS! BYffi.
12 AHPwR! AXeeh.
13 zGOvQ! zWddg.
14 yFNuP! yVccf.
15 xEMtO! xUbbe.
16 wDLsN! wTaad.
17 vCKrM! vSZZc.
18 uBJqL! uRYYb.
19 tAIpK! tQXXa.
20 szHoJ! sPWWZ.
21 ryGnI! rOVVY.
22 qxFmH! qNUUX.
23 pwElG! pMTTW.
24 ovDkF! oLSSV.
25 nuCjE! nKRRU.

我的代码实际输出的内容:

Do you wish to encrypt, decrypt, or brute force a message?
Enter your message:
Your translated text is:
1 LSAHC! LIPPS.
2 LSAHC! LIPPS.KRZGB! KHOOR.
3 LSAHC! LIPPS.KRZGB! KHOOR.JQYFA! JGNNQ.
4 LSAHC! LIPPS.KRZGB! KHOOR.JQYFA! JGNNQ.IPXEZ! IFMMP.
5 LSAHC! LIPPS.KRZGB! KHOOR.JQYFA! JGNNQ.IPXEZ! IFMMP.HOWDY! HELLO.
6 LSAHC! LIPPS.KRZGB! KHOOR.JQYFA! JGNNQ.IPXEZ! IFMMP.HOWDY! HELLO.GNVCX! 
GDKKN.
7 LSAHC! LIPPS.KRZGB! KHOOR.JQYFA! JGNNQ.IPXEZ! IFMMP.HOWDY! HELLO.GNVCX! 
GDKKN.FMUBW! FCJJM.
8 LSAHC! LIPPS.KRZGB! KHOOR.JQYFA! JGNNQ.IPXEZ! IFMMP.HOWDY! HELLO.GNVCX! 
GDKKN.FMUBW! FCJJM.ELTAV! EBIIL.
9 LSAHC! LIPPS.KRZGB! KHOOR.JQYFA! JGNNQ.IPXEZ! IFMMP.HOWDY! HELLO.GNVCX! 
GDKKN.FMUBW! FCJJM.ELTAV! EBIIL.DKSZU! DAHHK.

等...

我的完整代码:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
message = ""
encryptedmessage = " "
decryptedmessage = " "
keynumber = 0
count = 1
number = 0

def encrypt():
  global message
  global encryptedmessage
  global keynumber
  print()
  print()
  message = str(input("Enter your message:"))
  print()
  print()
  keynumber = int(input("Enter the key number (1-26)"))
  print()
  print()
  for i in message:
    position = alphabet.find(i)
    if position == -1:
      encryptedmessage += i
    else:
      newPosition = (position + keynumber) % 26
      encryptedmessage += alphabet[newPosition]
  print("Your translated text is:")
  print(encryptedmessage)


def decrypt():
  global message
  global encryptedmessage
  global decryptedmessage
  global keynumber
  message = str(input("Enter your message:"))
  print()
  print()
  print()
  print()
  keynumber = int(input("Enter the key number (1-26)"))
  print()
  print()
  print()
  print()
  for i in message:
    position = alphabet.find(i)
    if position == -1:
      decryptedmessage += i
    else:
      newPosition = (position - keynumber) % 26
      decryptedmessage += alphabet[newPosition]
  print("Your translated text is:")
  print(decryptedmessage)


def brute():
  global message 
  global encryptedmessage
  global decryptedmessage
  global keynumber
  global count
  global number
  keynumber = 1
  print()
  print()
  message = str(input("Enter your message:"))
  print()
  print()
  print("Your translated text is:")
  while keynumber < 26:
    for i in message:
      position = alphabet.find(i)
      if position == -1:
        decryptedmessage += i
      else:
        newPosition = (position - keynumber) % 26
        decryptedmessage += alphabet[newPosition]
    print(str(keynumber) + decryptedmessage)
    keynumber = keynumber + 1

action = input("Do you wish to encrypt, decrypt, or brute force a message?")

if action == "encrypt":
  encrypt()
if action == "decrypt":
  decrypt()
if action == "brute":
  brute()

0 个答案:

没有答案