Python解密

发布于 2024-11-02 22:33:39 字数 903 浏览 0 评论 0原文

我目前正在做一项家庭作业,我们必须构建一个函数来制作一个三步加密/解密程序。我们必须构建的密码之一是转置/栅栏,它将变量(n)作为您想要加密消息的“轨道”数量。我已经构建了加密,但是我' m不知解密方法。

这是 Python 的入门级课程,因此除了基础知识(例如下面包含的加密代码)之外,我们不知道太多。

如果你不确定我所说的换位加密/围栏是什么意思,这里有一个例子......

Message = abcdefg
n = 3

它最终会被加密成 3 组(如 n 所示),这些组将是 "adg be cf " 然后加密将它们重新组合成一个字符串 "adgbecf"。我的麻烦是将它们分解回原来的三个字符串“adg be cf”,然后将它们转回原始值。

加密:

def trans_encrypt(message, n):
    cipher = ""
    for i in range(n):
        for j in range(i, len(message), n):
            cipher = cipher + message[j]
    return cipher

当前解密(不起作用):

def trans_decrypt(cipher, n):
    length = len(cipher) // n
    message = ''
    for i in range(length): 
        for j in range(n):
            letter = (i + j * length) 
            message = message + cipher[letter] 
    return message

I am currently working on a homework assignment, and we have to build a function where we make a three step encryption/decryption program. One of the ciphers we have to build is a transposition/rail fence that takes a variable (n) as a number of "rails" that you'd like to have the message encrypted in. I've built the encryption, but I'm at a loss of the decryption method.

This is for an intro level class to python, so we don't know too much beyond the basics like the encryption code that is included below.

If you're not sure what I mean by transposition encrypt/rail fence is here's an example...

Message = abcdefg
n = 3

It'd end up being encrypted into 3 groups (as noted by n) and those groups would be "adg be cf" and from there the encryption recombines them into one string "adgbecf". My trouble is breaking them back down into the original three strings of "adg be cf" and then transposing them back to the original values.

Encryption:

def trans_encrypt(message, n):
    cipher = ""
    for i in range(n):
        for j in range(i, len(message), n):
            cipher = cipher + message[j]
    return cipher

Current decryption (doesn't work):

def trans_decrypt(cipher, n):
    length = len(cipher) // n
    message = ''
    for i in range(length): 
        for j in range(n):
            letter = (i + j * length) 
            message = message + cipher[letter] 
    return message

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

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

发布评论

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

评论(2

比忠 2024-11-09 22:33:39

如果您的加密方法是正确的(有人评论说这不是铁路密码),则以下情况是正确的:

您可以像这样进行加密:

>>> def trans_encrypt(message,n):
...     return "".join([message[i::n] for i in range(n)])
... 
>>> trans_encrypt(a,3)
'adgbecf'

我会给您一个有关解密的提示:

>>> b = 'adgbe cf '
>>> trans_encrypt(b,3)
'abcdefg  '

我在“中插入了空格” encrpyted”字符串,并仅对“加密”字符串使用加密方法来解密它。我得到了正确的结果,最后有一些额外的预期空间。

我会让你弄清楚如何计算在哪里添加空格,因为这是家庭作业。

您的解密方法可以只修改消息(插入空格),调用您的加密方法,并删除尾随的空格。

If your encryption method is correct, (someone commented that this isn't a rail cypher), the following is true:

You can do your encrypt like so:

>>> def trans_encrypt(message,n):
...     return "".join([message[i::n] for i in range(n)])
... 
>>> trans_encrypt(a,3)
'adgbecf'

I'll give you a hint about your decryption:

>>> b = 'adgbe cf '
>>> trans_encrypt(b,3)
'abcdefg  '

I inserted spaces into the "encrpyted" string and just used the encrypt method on the "encrypted" string to decrypt it. I got the right result with some extra expected spaces at the end.

I'll let you figure out how to calculate where to add the spaces since this is homework.

Your decrypt method can just modify the message (inserting spaces), call your encrypt method, and strip the trailing white space.

稳稳的幸福 2024-11-09 22:33:39

问题是密码不一定能被 n 整除,因此加密函数创建的“bins”的长度并不相同。第一个 len(cipher)%n 个 bin 有一个额外的字母。

当您在解密函数中迭代 j 时,如果 i 小于 len(cipher)%n,则范围应为 n+1。

The problem is that the cipher is not necessarily evenly divisible by n, so your 'bins' created by the encrypt function are not all of the same length. The first len(cipher)%n bins have one extra letter.

When you iterate over j in the decrypt function, if i is less than len(cipher)%n the range should be n+1.

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