利用python实现仿射密码以及穷举爆破

dic = {1: 1, 3: 9, 5: 21, 7: 15, 9: 3, 11: 19, 15: 7, 17: 23, 19: 11, 21: 5, 23: 17, 25: 25}  # 模逆
table_encypt = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5,
                'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12,
                'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19,
                'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
table_decypt = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g',
                7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r',
                18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z'}




# 加密
def encrypt(clear_content, key_a, key_b):
result = ""
for i in clear_content:
result += table_decypt.get((key_a * (table_encypt.get(i)) + key_b) % 26)
return result




# 解密
def decrypt(cipher, key_a, key_b):
result = ""
for i in cipher:
result += table_decypt.get((dic.get(key_a) * table_encypt.get(i) - (dic.get(key_a) * key_b) % 26) % 26)
return result




# 穷举
def blasting(cipher):
lis = []
result = ""
for i in dic.keys():
for j in range(0, 26):
for s in cipher:
result += table_decypt.get((dic.get(i) * table_encypt.get(s) - (dic.get(i) * j) % 26) % 26)
lis.append(result)
result = ""
return lis


def filter_claer(clear):
result = ""
clear = clear.lower()
for i in clear:
if 97 <= ord(i) <= 122:
result += i
return result
if __name__ == '__main__':
prompt = """选择
(e)加密
(d)解密
(c)爆破
(q)退出
请输入你的选择: 
"""
while True:
choice = input(prompt)
if choice == 'e':
clear = filter_claer(input("请输入明文:"))
key = input("请输入加密秘钥:a和b,以空格间隔").split(" ")
print("密文为: %s" % (encrypt(clear, int(key[0]), int(key[1]))))
elif choice == 'd':
cipher= input("请输入密文: ")
key = input("请输入加密秘钥:a和b,以空格间隔").split(" ")
print("明文为: %s" % decrypt(cipher, int(key[0]), int(key[1])))
elif choice == 'q':
break
elif choice == 'c':
cipher = input("请输入密文: ")
plain=blasting(cipher)
print(plain)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值