Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 练习加密并关闭1个字符_Python_Encryption - Fatal编程技术网

Python 练习加密并关闭1个字符

Python 练习加密并关闭1个字符,python,encryption,Python,Encryption,编写一个脚本,输入一行明文和一个距离值,并使用凯撒密码输出加密文本 该脚本应适用于任何可打印字符 程序输入和输出示例如下所示: Enter a message: Hello world! Enter the distance value: 4 Lipps${svph% 这是我的代码: # Encryptioin org_message = input("Enter a message: ") dist_value = int(input("Enter the distance value: "

编写一个脚本,输入一行明文和一个距离值,并使用凯撒密码输出加密文本

该脚本应适用于任何可打印字符

程序输入和输出示例如下所示:

Enter a message: Hello world!
Enter the distance value: 4
Lipps${svph%
这是我的代码:

# Encryptioin
org_message = input("Enter a message: ")
dist_value = int(input("Enter the distance value: "))
code_message = ""
for ch in org_message:
    order_value = ord(ch)
    cipher_value = order_value + dist_value
    if cipher_value > ord('z'):
        cipher_value = ord('a')+dist_value - \
                      (ord('z') - order_value + 1)
    code_message += chr(cipher_value)
print(code_message)
我的代码可以正常工作,但由于某种原因,对于这个特定条目,我得到了以下输出:

Lipps$asvph%
所以我得到的不是“{”而是“a”

获得帮助后,我将这些行编辑为:

if cipher_value > ord('~'):
        cipher_value = ord(' ')+dist_value - \
                      (ord('~') - order_value + 1)

提示:
ord('{')
大于
ord('z'))
。你的真命天子!这就是我从一本书中得到的编码str8 lol…在这张便条上,我可以用z现在是~和a现在是!得到我想要的…我现在的问题是如何包含NULL和Dec值0到31?通常在尝试编码时,首先将字符转换为加密为零基于字母表中的索引(这是一个比“abc”更广泛的概念),然后执行模块化加法/减法(mod n,其中n是字母表的大小),然后再次进行转换。字母表中的哪些字符取决于您(尽管NULL不是字符,并且您已经包含十进制零)。