Python:解密caesar ciph

2024-09-27 21:29:34 发布

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

作业:

编写一个脚本,输入一行加密文本和一个距离值,然后使用Caesar密码输出明文。在

我的问题:

正在打印hello ^3 world ^2

我不太清楚为什么。如果您能帮助我修复或理解代码中的错误,我将不胜感激。(这不是为等级分配的,即没有为我自己的利益而设定的到期日)

它希望我能够使用的输入及其各自的:

Lipps${svph%4

Jhss'tl'Pzothls57

Zu&hk2&ux&tuz&zu&hk2

khoor#zruog$3

我的代码:

code = input("enter coded text: ") 
distance = int(input("enter value: ")) 
plainText = "" 
for ch in code: 
  ordvalue = ord(ch) 
  ciphervalue = ordvalue - distance 
  if ciphervalue < ord('a'): 
    ciphervalue = ord('z') - \
      (distance - (ord('a')-ordvalue - 1))
  plainText += chr(ciphervalue) 
print(plainText)

Tags: 代码文本脚本距离input作业codech
1条回答
网友
1楼 · 发布于 2024-09-27 21:29:34

首先,您应该更容易地将所有输入与输出分离,因为:

def cipher(code, distance):
    plainText = ""
    for ch in code:
        ordvalue = ord(ch)
        ciphervalue = ordvalue - distance
        if ciphervalue < ord('a'):
            ciphervalue = ord('z') - (distance - (ord('a')-ordvalue - 1))
        plainText += chr(ciphervalue)
    return plainText

code = input("enter coded text: ")
distance = int(input("enter value: "))
print(cipher(code, distance))

现在,你有:

^{pr2}$

我猜你是在期待“你好,世界!”。如果你去掉这两条线你就会得到:

if ciphervalue < ord('a'):
    ciphervalue = ord('z') - (distance - (ord('a')-ordvalue - 1))

看:

def cipher2(code, distance):
    plainText = ""
    for ch in code:
        ordvalue = ord(ch)
        ciphervalue = ordvalue - distance
        plainText += chr(ciphervalue)
    return plainText

>>> cipher2("Lipps${svph%", 4)
'Hello world!'
>>> cipher2("Jhss'tl'Pzothls5", 7)
'Call me Ishmael.'
>>> cipher2("Zu&hk2&ux&tuz&zu&hk", 6) # was 2, but I guess it's 6
'To be, or not to be'
>>> cipher2("khoor#zruog$", 3)
'hello world!'

这是问题的有趣部分。但是我认为你有一个正确的直觉:当产生的ordvalue值不在预期范围内(即负的或大的)时会发生什么?在

>>> cipher2("hello", 103)
Traceback (most recent call last):
...
ValueError: chr() arg not in range(0x110000)

函数ord生成一个unicode码位,介于0和1114111之间,但我认为对于练习,您可以将范围限制在0-127(ASCII字符):

def cipher3(code, distance):
    assert abs(distance) < 128
    plainText = ""
    for ch in code:
        ordvalue = ord(ch)
        ciphervalue = ordvalue - distance
        if ciphervalue < 0:
            ciphervalue += 128
        elif ciphervalue >= 128: # don't forget distance can be negative
            ciphervalue -= 128
        plainText += chr(ciphervalue)
    return plainText

>>> cipher3("hello", 103)
'\\x01~\\x05\\x05\\x08'
>>> cipher3('\x01~\x05\x05\x08', -103)
'hello'

注意:

        if ciphervalue < 0:
            ciphervalue += 128
        elif ciphervalue >= 128: # don't forget distance can be negative
            ciphervalue -= 128

相当于:

        ciphervalue = ciphervalue % 128

如果只希望有可打印字符,可以使用string模块:

import string
# string.printable is '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

def cipher4(code, distance):
    assert abs(distance) < len(string.printable)
    plainText = ""
    for ch in code:
        ordvalue = string.printable.index(ch) # this is clearly suboptimal
        ciphervalue = (ordvalue - distance) % len(string.printable)
        plainText += string.printable[ciphervalue]
    return plainText

>>> cipher4("hello", 80)
'ByFFI'
>>> cipher4('ByFFI', -80)
'hello'

相关问题 更多 >

    热门问题