要解密PHP中的凯撒密码,可以使用Python编写以下代码:
def caesar_decrypt(ciphertext, shift):
    plaintext = ""
    for char in ciphertext:
        if char.isalpha():
            ascii_offset = ord('a') if char.islower() else ord('A')
            decrypted_char = chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset)
            plaintext += decrypted_char
        else:
            plaintext += char
    return plaintext
ciphertext = "cdefg"
shift = 2
plaintext = caesar_decrypt(ciphertext, shift)
print(plaintext)
在上述代码中,caesar_decrypt函数接受两个参数:ciphertext表示要解密的密文,shift表示凯撒密码的位移量。函数会遍历密文中的每个字符,如果是字母,则根据位移量进行解密,否则直接添加到明文中。最后,返回解密后的明文。
在示例中,密文为"cdefg",位移量为2。解密后的明文为"abcd"。你可以根据实际情况修改密文和位移量进行解密。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站