Цезарь Шифр ​​с заглавными буквами - PullRequest
0 голосов
/ 03 марта 2019

Так что в настоящее время моя программа шифрования Цезаря работает хорошо, когда я использую строчные буквы.Однако я хочу, чтобы он работал, когда я вводил слово или фразу в верхнем регистре.Это код, который я сейчас имею.Надеюсь, вы все поможете мне закончить это.

пользовательские функции

def encrypt (message, distance): "" "Примет сообщение и повернет его на расстояние, чтобы создатьзашифрованное сообщение "" "

encryption = ""
for ch in message:
    ordvalue = ord(ch)
    cipherValue = ordvalue + distance
    if cipherValue > ord("z"):
        cipherValue = ord("a") + distance - (ord("z") - ordvalue + 1)
    encryption += chr(cipherValue)
return encryption

def decrypt (message, distance):" "" Дешифрует вышеуказанное сообщение "" "

decryption = ""
for cc in message:
    ordvalue = ord(cc)
    decryptValue = ordvalue - distance
    if decryptValue < ord("a"):
        decryptValue = ord("z") - distance - (ord("a") - ordvalue - 1)
    decryption += chr(decryptValue)
return decryption

def binaryConversion (message):" ""Преобразует слово в двоичный код" ""

binary = ""
for cb in message:
    binaryString = " " #Binary number
    binaryNumber = ord(cb)
    while binaryNumber > 0:
        binaryRemainder = binaryNumber % 2
        binaryNumber = binaryNumber // 2
        binaryString = str(binaryRemainder) + binaryString
    binary += binaryString
return binary

во время цикла

run = True

во время выполнения:

#input 

message = input("Enter word to be encrypted: ") #original message
distance = int(input("Enter the distance value: ")) #distance letters will be moved

#variables

fancy = encrypt(message, distance)
boring = decrypt(fancy, distance)
numbers = binaryConversion(message)

#output
print("\n")
print("Your word was: ", format(message, ">20s"))
print("The distance you rotated was: ", format(distance), "\n")
print("The encryption is: ", format(fancy, ">16s"))
print("The decryption is: ", format(boring, ">16s"))
print("The binary code is: ", format(numbers)) #I know an error comes here but it will work in the end

repeat = input("Would you like to encrypt again? Y/N ")
print("\n")
if repeat == "N" or repeat == "n":
    run = False
else:
    run = True

Финал

print («Спасибо, как однажды сказал Юлий Цезарь:« Вени, види, вичи »)

Спасибо

1 Ответ

0 голосов
/ 03 марта 2019

Я бы посоветовал вам подойти к этой проблеме с точки зрения отображения, а не смещения.Вы можете построить отображение на основе смещения, но обработка символов будет проще, если вы используете словарь или какую-либо другую форму отображения один к одному.

Например:

  offset = 5
  source = "abcdefghijklmnopqrstuvwxyz"
  target = source[offset:]+source[:offset]
  source = source + source.upper()
  target = target + target.upper()
  encrypt = str.maketrans(source,target)
  decrypt = str.maketrans(target,source)

  e = "The quick brown Fox jumped over the lazy Dogs".translate(encrypt)
  print(e)
  d = e.translate(decrypt)
  print(d)
...