Python单表置换密码解密程序

Python单表置换密码解密程序

1.本置换密码程序的具体解释如下

在这里插入图片描述

2.具体的注释在代码中,有问题可以在评论中提问,我会及时说明

(由于第一次写Python代码,所以有任何相关意见都可以在评论区留言,我会及时改正)

输入密钥:HACKERINVASION

输入密文:cGDJTQEM HJJBVCHQVGF DHSGM

得到的明文为:Computer application major

在这里插入图片描述

# 大写字母表
AlphaBet = ["A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V",  "W", "X", "Y", "Z"]
# 小写字母表
alphaBet = ["a", "b", "c", "d", "e", "f", "g", "h", "i",
            "j", "k", "l", "m", "n", "o", "p", "q", "r",
            "s", "t", "u", "v",  "w", "x", "y", "z"]
# 作为密码本使用的字母表
AlphaBet1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i",
             "j", "k", "l", "m", "n", "o", "p", "q", "r",
             "s", "t", "u", "v",  "w", "x", "y", "z"]

# 输入密钥和密文
key = list(input("输入密钥:"))
miwen = input("输入密文:")

# 这是作为处理后的密钥使用
keyf = []
mingwen = ""

# 将key中重复的字母删除
def chuli_key():
    x = 0
    for i in key:
        z = 0
        x = 0
        for a in key:
            if i == a:
                x = x + 1
                if x == 2:
                    key.pop(z)
                    x = 0
            z += 1
    print(key)

#将key加入到密码表中
def add_key():
    i = 0
    while i < len(key):
        AlphaBet1[i] = key[i]
        i += 1
    k = len(key)
    j = 0

# 将余下的位置用与KEY不重复的字母顺序填充
    for i in AlphaBet:
        if key.count(i) == 0:
            AlphaBet1[k] = i
            k += 1


# 将key中重复的字母删除
chuli_key()
# 将key加入到密码表中
add_key()
# 判断字母大小写
for i in miwen:
    if i.isspace() == True:
        mingwen = mingwen + ' '
        continue
    else:
        if ord(i) <= ord("Z"):
            mingwen = mingwen + alphaBet[AlphaBet1.index(i)]
        elif ord(i) >= ord("a"):
            mingwen = mingwen + AlphaBet1[alphaBet.index(i)]

print(key)
print(alphaBet)
print(AlphaBet)
print(AlphaBet1)
print(mingwen)

input("按任意键退出")

  • 6
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
置换密码是一种简的加密方法,它通过将明文中的每个字母替换为密文中对应的字母来加密。这里我们提供一个使用置换密码进行加密和解密Python 代码示例。 ```python import string def generate_cipher_key(shift): """ 生成密文 """ upper_case = string.ascii_uppercase lower_case = string.ascii_lowercase shifted_upper_case = upper_case[shift:] + upper_case[:shift] shifted_lower_case = lower_case[shift:] + lower_case[:shift] cipher_key = str.maketrans(upper_case + lower_case, shifted_upper_case + shifted_lower_case) return cipher_key def encrypt(plaintext, shift): """ 加密明文 """ cipher_key = generate_cipher_key(shift) ciphertext = plaintext.translate(cipher_key) return ciphertext def decrypt(ciphertext, shift): """ 解密密文 """ cipher_key = generate_cipher_key(shift) plaintext = ciphertext.translate(cipher_key) return plaintext ``` 这里我们使用了 Python 内置的 `string` 模块来获取大小写字母,并通过 `str.maketrans()` 方法生成置换密码的密文。`generate_cipher_key()` 函数接受一个整数参数 `shift`,示字母需要向后移动的位数,从而生成不同的密文。`encrypt()` 函数接受一个明文字符串和一个 `shift` 参数,返回加密后的密文字符串。`decrypt()` 函数接受一个密文字符串和一个 `shift` 参数,返回解密后的明文字符串。 下面是一个使用示例: ```python plaintext = "Hello, World!" shift = 3 ciphertext = encrypt(plaintext, shift) print(ciphertext) # "Khoor, Zruog!" decrypted_plaintext = decrypt(ciphertext, shift) print(decrypted_plaintext) # "Hello, World!" ``` 在这个例子中,我们将明文中的所有字母向后移动了 3 位,并得到了相应的密文。解密过程中,我们将密文中的所有字母向前移动 3 位,从而得到了原始的明文。需要注意的是,由于置换密码只是一种简的加密方法,它的安全性非常有限,容易受到各种攻击。因此在实际应用中,需要使用更加复杂和安全的加密方法来保护敏感数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大可iii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值