CTF 密码学
Posted 木剑配酒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CTF 密码学相关的知识,希望对你有一定的参考价值。
0x00 前言
为了练习python,强迫自己能用Python的题都用python解题还有各种密码
0x01 奇怪的字符串
实验吧题目:信息保密的需求和实际操作自古有之,与之相应的信息加密与解密也是历史悠久,现有一段经过古典密码理论(不止一种)加密的密文,内容如下:
89 51 82 109 89 50 86 122 97 71 107 61请找出这段密文隐藏的消息明文
1.数字都没有超过127,ASCII码,解出来base64,然后再转得到flag
1 import base64 2 s="89 51 82 109 89 50 86 122 97 71 107 61" 3 s=s.split() 4 d = "" 5 for i in s: 6 d+=(chr(int(i))) 7 print(d) 8 print(base64.b64decode(d))
0x02 base??
实验吧题目:YMFZZTY0D3RMD3RMMTIZ 这一串到底是什么!!!!为什么这么像base32却不是!!! 明文的md5值为16478a151bdd41335dcd69b270f6b985
1.给了一段全字母全大写的字符串,提示说像base32,后来试了一下错误的,后面有md5值,网上搜了一下直接解出来了。起始作者的思路
2 爆破列出所有的字符串组合的MD5值和给出的md5比较,如果相等,返回原字符串,得出flag,上代码
import base64 import hashlib import itertools pwd ="YMFZZTY0D3RMD3RMMTIZ" tables =[] #检查md5值是否相同 def check(src): m1 =hashlib.md5() m1.update(src) ans =m1.hexdigest() if ans =="16478a151bdd41335dcd69b270f6b985": return 1 else: return 0 #生成字符串的列表 def generateTable(string): lists=[] for i in range(len(string)): if str.isupper(string[i]): lists.append([str.lower(string[i]),string[i]]) else: lists.append([string[i]]) print(lists) #generateTable(pwd) lists=itertools.product([‘y‘, ‘Y‘], [‘m‘, ‘M‘], [‘f‘, ‘F‘], [‘z‘, ‘Z‘], [‘z‘, ‘Z‘], [‘t‘, ‘T‘], [‘y‘, ‘Y‘], [‘0‘], [‘d‘, ‘D‘], [‘3‘], [‘r‘, ‘R‘], [‘m‘, ‘M‘], [‘d‘, ‘D‘], [‘3‘], [‘r‘, ‘R‘], [‘m‘, ‘M‘], [‘m‘, ‘M‘], [‘t‘, ‘T‘], [‘i‘, ‘I‘], [‘z‘, ‘Z‘]) for i in lists: temp=‘‘.join(i) if check(base64.b64decode(temp)) ==1: print(bytes.decode(base64.b64decode(temp)))
0x03 Morese 加解密
#Author TriompheL #2017年11月17日 #功能:对于Morse密码的加密解密 DeCODE = {‘.-‘: ‘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‘, ‘-----‘: ‘0‘, ‘.----‘: ‘1‘, ‘..---‘: ‘2‘, ‘...--‘: ‘3‘, ‘....-‘: ‘4‘, ‘.....‘: ‘5‘, ‘-....‘: ‘6‘, ‘--...‘: ‘7‘, ‘---..‘: ‘8‘, ‘----.‘: ‘9‘ } EnCODE = {‘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‘: ‘--..‘, ‘0‘: ‘-----‘, ‘1‘: ‘.----‘, ‘2‘: ‘..---‘, ‘3‘: ‘...--‘, ‘4‘: ‘....-‘, ‘5‘: ‘.....‘, ‘6‘: ‘-....‘, ‘7‘: ‘--...‘, ‘8‘: ‘---..‘, ‘9‘: ‘----.‘ } def encrypt(): msg = raw_input(‘MESSAGE: ‘) ans ="" for char in msg: if char == ‘ ‘: ans+=‘ ‘ else: ans+=EnCODE[char.upper()] ans+=‘ ‘ #print(EnCODE[char.upper()] + ‘ ‘) print(ans) def decrypt(): msg =input(‘MESSAGE:‘) mores ="" ans ="" for char in msg: if char==‘.‘ or char==‘-‘: mores+=char if char ==‘ ‘ and mores !=‘ ‘: ans+=DeCODE[mores] mores="" if mores !="": ans+= DeCODE[mores] mores="" print(ans.lower()) def run(): para = input(‘1 is encrypt 2 is decrypt : ‘) if para == ‘1‘: encrypt() elif para == ‘2‘: decrypt() else: print(" your input is error") run()
0x04 费纳姆密码
#encoding:UTF-8 # 费纳姆密码 paswd = "000000000000000000000000000000000" "000000000000000000101110000110001000000101000000001" EnCODE ={‘A‘:‘1000001‘ ,‘N‘:‘1001110‘, ‘B‘: ‘1000010‘, ‘O‘ :‘1001111‘, ‘C‘: ‘1000011‘, ‘P‘ :‘1010000‘, ‘D‘: ‘1000100‘, ‘Q‘ :‘1010001‘, ‘E‘: ‘1000101‘, ‘R‘ :‘1010010‘, ‘F‘: ‘1000110‘, ‘S‘ :‘1010011‘, ‘G‘: ‘1000111‘, ‘T‘ :‘1010100‘, ‘H‘: ‘1001000‘, ‘U‘ :‘1010101‘, ‘I‘: ‘1001001‘, ‘V‘ :‘1010110‘, ‘J‘: ‘1001010‘, ‘W‘ :‘1010111‘, ‘K‘: ‘1001011‘ ,‘X‘ :‘1011000‘, ‘L‘: ‘1001100‘ ,‘Y‘ :‘1011001‘, ‘M‘: ‘1001101‘ ,‘Z‘ :‘1011010‘ } DeCODE={ ‘1000001‘:‘A‘, ‘1001110‘:‘N‘, ‘1000010‘:‘B‘, ‘1001111‘:‘O‘, ‘1000011‘:‘C‘, ‘1010000‘:‘P‘, ‘1000100‘:‘D‘, ‘1010001‘:‘Q‘, ‘1000101‘:‘E‘, ‘1010010‘:‘R‘, ‘1000110‘:‘F‘, ‘1010011‘:‘S‘, ‘1000111‘:‘G‘, ‘1010100‘:‘T‘, ‘1001000‘:‘H‘, ‘1010101‘:‘U‘, ‘1001001‘:‘I‘, ‘1010110‘:‘V‘, ‘1001010‘:‘J‘, ‘1010111‘:‘W‘, ‘1001011‘:‘K‘, ‘1011000‘:‘X‘, ‘1001100‘:‘L‘, ‘1011001‘:‘Y‘, ‘1001101‘:‘M‘, ‘1011010‘:‘Z‘ } #加密 def encrypt(): msg =input("Input Message:") ans ="" for char in msg: if char ==‘ ‘: ans+= ‘ ‘ else: ans+=EnCODE[char.upper()]+‘ ‘ print(ans) #解密 def decrypt(): msg =input(‘MESSAGE:‘) german ="" ans ="" for char in msg: if char==‘0‘ or char==‘1‘: german+=char if len(german)==7: ans+=DeCODE[german] german="" print(ans) def run(): para = input(‘1 is encrypt 2 is decrypt : ‘) if para == ‘1‘: encrypt() elif para == ‘2‘: decrypt() else: print(" your input is error") run()
以上是关于CTF 密码学的主要内容,如果未能解决你的问题,请参考以下文章