带大写字母的凯撒密码

2024-10-02 22:35:23 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,目前我的凯撒密码程序运行良好,每当我使用小写字母。我希望它工作时,我输入一个单词或短语与大写。这就是我现在的密码。希望你们能帮我完成这件事。在

用户定义函数

def encrypt(消息,距离): “”“将获取邮件并将其旋转一段距离,以便创建加密邮件”“”

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(消息,距离): “”“将解密上述邮件”“”

^{pr2}$

def二进制转换(消息): “”“将把单词转换成二进制代码”“”

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

while循环

运行=真

运行时:

#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(“谢谢你&就像朱利叶斯·凯撒曾经说过的‘威尼斯,维迪,维西’”)

谢谢你


Tags: theformat消息messageinputdefencryptdistance
1条回答
网友
1楼 · 发布于 2024-10-02 22:35:23

我建议你用一种映射的心态来解决这个问题,而不是一种补偿。您可以基于偏移量构建映射,但如果使用字典或其他形式的一对一映射,则字符处理会更容易。在

例如:

  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)

相关问题 更多 >