python怎么做?

JBnb shgk(用表达式解密密文。加西规则:空格代码未变,前4个字母加1,后4个字母的代码减1
解密规则:空格代码未变,首4个宇母的代码-1,后4个宇母的代码+1)
表达式:


a = 'JBnb shgk'
print(''.join([a[n] if a[n] == ' ' else (chr(ord(a[n]) - 1) if n < 4 else chr(ord(a[n]) + 1)) for n in range(len(a))]))

不知道你说的前四个字母是abcd这样前四个,还是密文开头的前4个,按照前者先给你写一个吧

def jbnb_shgk_encrypt(plaintext):
    ciphertext = ""
    for ch in plaintext:
        if ch == " ":
            ciphertext += ch
        elif ch.islower() and ord(ch) < ord("w"):
            ciphertext += chr(ord(ch) + 1)
        elif ch.islower() and ord(ch) >= ord("w"):
            ciphertext += chr(ord(ch) - 7)
        elif ch.isupper() and ord(ch) < ord("W"):
            ciphertext += chr(ord(ch) + 1)
        elif ch.isupper() and ord(ch) >= ord("W"):
            ciphertext += chr(ord(ch) - 23)
        else:
            ciphertext += ch
    return ciphertext


def jbnb_shgk_decrypt(ciphertext):
    plaintext = ""
    for ch in ciphertext:
        if ch == " ":
            plaintext += ch
        elif ch.islower() and ord(ch) > ord("a"):
            plaintext += chr(ord(ch) - 1)
        elif ch.islower() and ord(ch) <= ord("a"):
            plaintext += chr(ord(ch) + 7)
        elif ch.isupper() and ord(ch) > ord("A"):
            plaintext += chr(ord(ch) - 1)
        elif ch.isupper() and ord(ch) <= ord("A"):
            plaintext += chr(ord(ch) + 23)
        else:
            plaintext += ch
    return plaintext
plaintext = "Hello, World!"
ciphertext = jbnb_shgk_encrypt(plaintext)
print("加密后的密文:", ciphertext)
decrypted_plaintext = jbnb_shgk_decrypt(ciphertext)
print("解密后的明文:", decrypted_plaintext)