使用功能来加密/解密字符串

发布于 2025-01-23 15:19:28 字数 1400 浏览 1 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2025-01-30 15:19:28

看来您想要的是一个凯撒密码,在Python中相对简单。

def encrypt(text, key):
    """Encrypts text using a ceaser cypher"""
    encrypted = ""
    for char in text:
        if char.isalpha():
            encrypted += chr((ord(char) + key - 97) % 26 + 97)
        else:
            encrypted += char
    return encrypted

关于此代码的唯一真正奇怪的部分是Unicode角色疯狂。如果您不知道Unicode/ascii是一种将计算机内存中映射到计算机内存的字符的方式,那么计算机内存在根本上只是1和0。这是所有相关字符的图表

https://www.asc.ohio-state.edu/demarneffe.1/LING5050/material/ASCII-Table.png

It seems like what you want is a caesar cipher which is relatively simple to do in python.

def encrypt(text, key):
    """Encrypts text using a ceaser cypher"""
    encrypted = ""
    for char in text:
        if char.isalpha():
            encrypted += chr((ord(char) + key - 97) % 26 + 97)
        else:
            encrypted += char
    return encrypted

The only really weird part about this code is the unicode character madness. If you don't know unicode/ascii is a way to map numbers in a computers memory to characters which computer memory is fundamentally just 1's and 0's. here's a chart for all the relevant character

https://www.asc.ohio-state.edu/demarneffe.1/LING5050/material/ASCII-Table.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文