加密程序:

def encrypt(message, keyword):
    # 生成关键词加密表
    table = []
    for c in keyword:
        if c not in table:
            table.append(c)
    for c in "abcdefghijklmnopqrstuvwxyz":
        if c not in table:
            table.append(c)
    # 加密消息
    encrypted = ""
    for c in message:
        if c.isalpha():
            index = ord(c.lower()) - ord('a')
            encrypted += table[index]
        else:
            encrypted += c
    return encrypted

解码程序:

def decrypt(message, keyword):
    # 生成关键词解密表
    table = []
    for c in keyword:
        if c not in table:
            table.append(c)
    for c in "abcdefghijklmnopqrstuvwxyz":
        if c not in table:
            table.append(c)
    # 解密消息
    decrypted = ""
    for c in message:
        if c.isalpha():
            index = table.index(c.lower())
            decrypted += chr(index + ord('a'))
        else:
            decrypted += c
    return decrypted

使用示例:

message = "Hello, World!"
keyword = "python"
encrypted = encrypt(message, keyword)
print("加密后的消息:", encrypted)
decrypted = decrypt(encrypted, keyword)
print("解密后的消息:", decrypted)

输出结果:

加密后的消息: jgnnq, xqktc!
解密后的消息: hello, world!
``
关键词加密1、选择一个关键词如果这个关键词中有重复的字母则去除第一次出现之外的所有相同字母。2、将关键词写在字母表的下方并用字母表中出了关键词之外的其他字母按标准的顺序填写余下空间。写一个Python加密和解码程序

原文地址: https://www.cveoy.top/t/topic/hjc0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录