简体   繁体   English

我将如何扭转这一局面? Python 3.8

[英]How would I reverse this? Python 3.8

So I've created a very... odd little caesar cipher using python.因此,我使用 python 创建了一个非常……奇怪的小凯撒密码。 Very simple.很简单。 Except, I'm not really all that great at math and am wondering how I'd reverse this?除了,我真的不是很擅长数学,我想知道我该如何扭转这一点?

def encrypt(text,s): 
    result = "" 
    for i in range(len(text)): 
        char = text[i] 
        if (char.isupper()): 
            result += chr((ord(char) + s - 23+213**3) % 26 + 713) 
        else: 
            result += chr((ord(char) + s - 23+213**3) % 26 + 715) 
    return result 

text = input("Message: ")
s = 964

print ("Text: " + text) 
print ("Shift: " + str(s)) 
print ("Cipher: " + encrypt(text,s))

Any form of help would be appreciated.任何形式的帮助将不胜感激。

Edit:编辑:

I'M SO CLOSE: I did that math as to how the shift works:我很接近:我对轮班的工作方式进行了数学计算:

if the letter is a capital: 1. 964 - 23+213^3, which ends up as -9662656 2. Get the remainder of that divided by 26 (modulo operator) -9662656 % 26 = 10 3. 10 + 714 = 724 4. 724-63 I got the 63 just through trial and error...如果字母是大写字母: 1. 964 - 23+213^3, which ends up as -9662656 2. Get the remainder of that divided by 26 (modulo operator) -9662656 % 26 = 10 3. 10 + 714 = 724 4. 724-63我通过反复试验得到了 63...

ONLY PROBLEM, It all works up until the letter M. in which case the last 13 letters shift backward 26 characters?唯一的问题,直到字母 M 为止,这一切都有效。在这种情况下,最后 13 个字母向后移动 26 个字符? How would I solve this?我将如何解决这个问题?

def decrypt(text,s): 
    result = "" 
    for i in range(len(text)): 
        char = text[i] 
        result += chr((ord(char) - s))
    return result 

text = input("Message: ")
s = 724-63

print ("Text: " + text) 
print ("Shift: " + str(s)) 
print ("Cipher: " + decrypt(text,s))
Text: ˖˗˘˙˚˛˜˝˞˟ˠˡˢˉˊˋˌˍˎˏːˑ˒˓˔˕
Shift: 661
Cipher: ABCDEFGHIJKLM456789:;<=>?@

I rewrote your encrypt function to show the individual piece of the calculation.我重写了您的加密 function 以显示计算的单个部分。 Then I wrote the decrypt function to show how to "undo" them.然后我写了解密 function 来展示如何“撤消”它们。

Note that -23+213 3 is equivalent to 24 when working mod 26.请注意,在使用 mod 26 时,-23+213 3等价于 24。

def encrypt(text, s):
    cipher = ""
    for pt_char in text:
        val = ord(pt_char)          # 65 <= val < 91
        t0 = val % 26               # 0 <= t0 < 26, with A=>13, B=>14, ... N=>0 etc.
        t1 = (t0 + s + 24) % 26     # 0 <= t1 < 26
        t2 = t1 + 715               # 715 <= t2 < 741
        cipher += chr(t2)
    return cipher


def decrypt(ct, s):
    plain = ""
    for ct_char in ct:
        t2 = ord(ct_char)           # 715 <= t2 < 741
        t1 = t2 - 715               # 0 <= t1 < 26
        t0 = (t1 - s - 24 + 13) % 26     # 0 <= t0 < 26
        val = t0 + 65               # 65 <= val < 91
        plain += chr(val)
    return plain

Again, this only works for upper case ASCII letters.同样,这仅适用于大写 ASCII 字母。 Pay attention to the comments, they are telling you something.注意评论,他们在告诉你一些事情。

Here are shorter one-liner just to show you something about how generators can produce compact code.这里有一些简短的单行代码,只是为了向您展示有关生成器如何生成紧凑代码的一些信息。

def encrypt_one_liner(text, s):
    return ''.join(chr((ord(x) + s + 24) % 26 + 715) for x in text)


def decrypt_one_liner(text, s):
    return ''.join(chr((ord(x) + 2 - s) % 26 + 65) for x in text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM