python3 仿射密碼破解腳本
在做CTF的時候會用到,收藏備用
程式使用操作很簡單,直接運行,輸入c選擇破解模式,輸入密文即可開始破解,if result[0:3].lower() in ['key', 'ctf', 'fla']:程式會自動把含有"fla key ctf"的字串篩選,輸出
# -*- coding: utf-8 -*-
#使用前注意修改判別flag條件,第35行*********
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}#模逆
#加密
def encrypt(clear_content, key_a, key_b):
result = ""
for i in clear_content:
if (ord(i) >= 65 and ord(i) <= 90):
result += chr(((key_a*(ord(i)-65)+key_b)%26)+65)
elif (ord(i) >= 97 and ord(i) <= 122):
result += chr(((key_a*(ord(i)-97)+key_b)%26)+97)
else:
result += i
return result
#窮舉
def blasting(cipher):
lis = []
flag = []
result = ""
a = 1
for i in dic.keys():
for j in range(0, 26):
for s in cipher:
if (ord(s) >= 65 and ord(s) <= 90):
result += chr(((dic.get(i) * (ord(s)-65) - (dic.get(i) * j) % 26) % 26)+65)
elif (ord(s) >= 97 and ord(s) <= 122):
result += chr(((dic.get(i) * (ord(s)-97) - (dic.get(i) * j) % 26) % 26)+97)
else:
result += s
print('明文' + str(a) + ' : ' + result)
a += 1
if result[0:3].lower() in ['key', 'ctf', 'fla']:
flag.append(result)
result = ""
return flag
if __name__=='__main__':
prompt = """選擇:(e)加密 (c)破解
請輸入您的選擇:"""
choice = input(prompt)
if choice == 'e':
ming = input("請輸入明文:")
key = input("請輸入加密密鑰:a和b,以空格間隔").split(" ")
print("密文為:%s" % (encrypt(ming, int(key[0]), int(key[1]))))
elif choice == 'c':
cipher = input("請輸入密文:")
plain = blasting(cipher)
print("程式判定flag為:")
print(plain)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/238501.html
標籤:其他
上一篇:資訊安全實踐Lab1-自建CA證書搭建https服務器
下一篇:簡單的for回圈,你不會就落后了
