【问题标题】:How do I display an encrypted message from a text file using ASCII in Python?如何在 Python 中使用 ASCII 显示来自文本文件的加密消息?
【发布时间】:2015-06-28 09:55:13
【问题描述】:

我试图通过将文本文件中的字符串中的字符转换为其等效的 ASCII 值来显示加密消息。完成后,我想添加一个偏移因子,比如 10,然后转换回来并显示消息。此外,如果有人可以忽略空格,我也希望它不会被加密。最后,如果有人可以从 ASCII 值中减去 94,如果它高于 126,那么它会是一个奖励。

到目前为止,我已经“生成”了整数,但我得到的只是方括号

user_input = input("Enter a text file name. Remeber to add 'txt'")
file = open(user_input,"r+") 
print(file.readline())

int_text = ([ord(c) for c in file.readline()])
print(int_text)

感谢您的帮助。

【问题讨论】:

  • 它真的打开了文件吗?你应该使用raw_input 而不是input
  • 这看起来像 Python 3,所以不,它们不应该。
  • 我投票结束这个问题,因为这既不是代码编写也不是教程服务

标签: python python-3.x encryption ascii


【解决方案1】:

shifting 采用文本和可选的偏移因子并返回转换后的字符串:

def shifting(text, n=10):
    ints = (ord(c) for c in text)
    def helper():
        for c in ints:
            if c==32:
                yield " "
            elif c+n > 126:
                yield chr(c+n-94)
            else:
                yield chr(c+n)
    return "".join(helper())

enc_text = shifting(file.readline(),10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    相关资源
    最近更新 更多