Caesar密码Python-其他功能

Caesar密码Python-其他功能,python,ascii,shift,Python,Ascii,Shift,所以目前,我的代码是这样的(感谢我在另一篇文章中的帮助) 问题是,如果我键入多个单词,例如:hello world,移位为1 输出为:ifmmp!xpsme 感叹号表示空格(根据移位而变化)。我正在考虑使用if语句来检测空格: phrase = raw_input("Enter text to Cipher: ") shift = int(raw_input("Please enter shift: ")) result = ("Encrypted text is: ") for charac

所以目前,我的代码是这样的(感谢我在另一篇文章中的帮助)

问题是,如果我键入多个单词,例如:hello world,移位为1

输出为:
ifmmp!xpsme

感叹号表示空格(根据移位而变化)。我正在考虑使用if语句来检测空格:

phrase = raw_input("Enter text to Cipher: ")
shift = int(raw_input("Please enter shift: "))
result = ("Encrypted text is: ")

for character in phrase: 
        #Loops through phrase and shows ascii numbers, example: hello is: 104,101,108,108,111
    x = ord(character)

    if x == ord(' '):
        print "\nfound a space space"

        #adds 1 to each character so 'hello' becomes: ifmmp 105,102,109,109,112
    result += chr(x + shift)


print "\n",result,"\n"
但我不知道如何将空格添加到结果变量中。另外,我在这篇文章中看到:

JeffB使用while循环来处理ASCII表32是空格,127是DEL。他为什么用96?我不明白

while x < 32:
    x += 96

while x > 127:
    x -= 96
当x<32时:
x+=96
当x>127时:
x-=96

对不起,这个问题太长了。非常感谢!您的帮助对我来说是无价的。

您可以跳过以下空白:

for character in phrase:
    x = ord(character)

    if character == ' ':
        result += ' '
    else:
        result += chr(x + shift)
您的移位不会将输出限制为仅ASCII。如果要确保这一点,应使用模运算符:

chr(32 + (x + shift) % (127 - 32))

您可以跳过空格:

for character in phrase:
    x = ord(character)

    if character == ' ':
        result += ' '
    else:
        result += chr(x + shift)
您的移位不会将输出限制为仅ASCII。如果要确保这一点,应使用模运算符:

chr(32 + (x + shift) % (127 - 32))

您可以添加这样的空间:

if character.isspace():
   result += ' '
或在空白处拆分字符串:

示例:

>>> "hello world".split()
['hello', 'world']
new_strs = []
result = ("Encrypted text is:")

for word in phrase.split(): 
    new_word = []
    for character in word:
       x = ord(character) + shift
       new_word.append(chr(x if 97 <= x <= 122 else 96 + x % 122))

    new_strs.append("".join(new_word))

print result, " ".join(new_strs)
$ python so.py
Enter text to Cipher: hello xyz
Please enter shift: 1
Encrypted text is: ifmmp yza
代码:

>>> "hello world".split()
['hello', 'world']
new_strs = []
result = ("Encrypted text is:")

for word in phrase.split(): 
    new_word = []
    for character in word:
       x = ord(character) + shift
       new_word.append(chr(x if 97 <= x <= 122 else 96 + x % 122))

    new_strs.append("".join(new_word))

print result, " ".join(new_strs)
$ python so.py
Enter text to Cipher: hello xyz
Please enter shift: 1
Encrypted text is: ifmmp yza

您可以添加这样的空间:

if character.isspace():
   result += ' '
或在空白处拆分字符串:

示例:

>>> "hello world".split()
['hello', 'world']
new_strs = []
result = ("Encrypted text is:")

for word in phrase.split(): 
    new_word = []
    for character in word:
       x = ord(character) + shift
       new_word.append(chr(x if 97 <= x <= 122 else 96 + x % 122))

    new_strs.append("".join(new_word))

print result, " ".join(new_strs)
$ python so.py
Enter text to Cipher: hello xyz
Please enter shift: 1
Encrypted text is: ifmmp yza
代码:

>>> "hello world".split()
['hello', 'world']
new_strs = []
result = ("Encrypted text is:")

for word in phrase.split(): 
    new_word = []
    for character in word:
       x = ord(character) + shift
       new_word.append(chr(x if 97 <= x <= 122 else 96 + x % 122))

    new_strs.append("".join(new_word))

print result, " ".join(new_strs)
$ python so.py
Enter text to Cipher: hello xyz
Please enter shift: 1
Encrypted text is: ifmmp yza

只需使用maketrans和translate函数,它们基本上可以为您加密或解密消息。它们可以为问题提供非常简短而有效的解决方案

message = input('enter message').lower()
offset = int(input('enter offset (enter a negative number to decrypt)'))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
enc_alphabet = (alphabet[alphabet.index(alphabet[offset]):len(alphabet)])+ alphabet[0:offset]
data = str.maketrans(alphabet,enc_alphabet)
final_message = str.translate(message, data)
print(final_message)

这样,您就不必担心添加空格或任何东西,这是一个完全可以工作的caesar密码加密程序

只需使用maketrans和translate函数,基本上可以为您加密或解密消息。它们为问题提供了一个非常短而有效的解决方案

message = input('enter message').lower()
offset = int(input('enter offset (enter a negative number to decrypt)'))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
enc_alphabet = (alphabet[alphabet.index(alphabet[offset]):len(alphabet)])+ alphabet[0:offset]
data = str.maketrans(alphabet,enc_alphabet)
final_message = str.translate(message, data)
print(final_message)

这样,您就不必担心添加空格或其他任何东西,这是一个完全工作的caesar密码加密程序

空间不是Cesar密码(又称移位密码)需要处理的唯一问题。历史上,字符设置为所有大写(或小写),所有空格和所有标点符号都被删除

显示了一个处理所有标点符号删除和密钥生成(可选)的Cesar密码实现的良好示例。链接实现选择使用由正则表达式实现的允许字符的白名单

# Message is a the string to be encrypted / decrypted
sub(r'[^A-Z]', '', message.upper())

空间不是Cesar密码(又称移位密码)需要处理的唯一问题。历史上,字符设置为所有大写(或小写),所有空格和所有标点符号都被删除

显示了一个处理所有标点符号删除和密钥生成(可选)的Cesar密码实现的良好示例。链接实现选择使用由正则表达式实现的允许字符的白名单

# Message is a the string to be encrypted / decrypted
sub(r'[^A-Z]', '', message.upper())

谢谢你,搅拌机。我只是出于自己的学习目的注释掉了else语句,发现没有打印结果。因此,您能否确认以下内容:if语句仅适用于“”空间。character变量的其余部分通过else语句?如果是这样,那就是天才。谢谢你,汉克斯搅拌机。我只是出于自己的学习目的注释掉了else语句,发现没有打印结果。因此,您能否确认以下内容:if语句仅适用于“”空间。character变量的其余部分通过else语句?如果是这样,那就是天才。谢谢Hanks Ashwini,这是一种很好的方法,对我来说有点复杂,但我会学习它并提高我的python技能:)谢谢Ashwini,这是一种很好的方法,对我来说有点复杂,但我会学习它并提高我的python技能:)