CTF(CaptureTheFlag)密码学基础

信息安全不简单鸭 2024-08-31 21:46:31

密码学其实很烧脑,有点偏理论数学,成为合法黑客是一件不容易的事情。2%的是天赋型选手,类似李白,98%是牛马型选手,比如高适。纵观20年的黑客发展之路,合法黑客早已成为企业高管,但是欲练此功,必会代码(c、java、python、php多多益善),练练不忘,必有回响(OverTheWire、CTF365、CTFtime、vulnhub),国际认证,势必捷径(GPEN认证、CEH认证、OSCP认证,CISSP认证),加油吧~

ailx10

网络安全优秀回答者

网络安全硕士

去咨询

1、凯撒密码

相传有一位大帝叫 Caesar ,他发明了一种神秘的编码,你能解开这段密文吗?密文内容:mshn{jhlzhy_pz_mbuufek}。flag格式为flag{字符串}。

凯撒密码是一种替换密码,通过将字母按照一个固定的偏移量进行替换来加密消息。根据给出的密文,我们需要尝试所有可能的偏移量来解密消息。使用python编码:

# -*- coding: utf-8 -*-def decrypt_caesar(ciphertext, shift): decrypted = '' for char in ciphertext: if char.isalpha(): shifted = ord(char) - shift if char.islower(): if shifted < ord('a'): shifted += 26 elif char.isupper(): if shifted < ord('A'): shifted += 26 decrypted += chr(shifted) else: decrypted += char return decrypted# 密文ciphertext = "mshn{jhlzhy_pz_mbuufek}"# 尝试解密并输出所有可能的偏移量结果for i in range(26): decrypted_text = decrypt_caesar(ciphertext, i) print(f"Shift {i}: {decrypted_text}")

2、Rot13加密

加密一次,再来一次,然后回到起点。1Ebbg8Vf7Abg3Nyybjrq

rot13是一种简单的替换密码,实际上是凯撒密码的一种特殊形式,它的解密过程就是再次应用rot13加密。使用python编码:

# -*- coding: utf-8 -*-def decrypt_rot13(ciphertext): decrypted = '' for char in ciphertext: if char.isalpha(): # Check if the character is a letter shifted = ord(char) - 13 if char.islower(): if shifted < ord('a'): shifted += 26 elif char.isupper(): if shifted < ord('A'): shifted += 26 decrypted += chr(shifted) else: decrypted += char return decrypted# 密文ciphertext = "1Ebbg8Vf7Abg3Nyybjrq"# 解密rot13并输出结果decrypted_text = decrypt_rot13(ciphertext)print(f"Decrypted text: {decrypted_text}")# Decrypted text: 1Root8Is7Not3Allowed

3、栏栅密码

Caesar 被困住了2333333,13 根栅栏,怎么办? h{igr},aarclietflhf-_peecirroc,eo_fhlels_caifnge

栏栅密码是一种置换密码,它通过在一定规则下将明文的字母写成不同行形成一种特定模式来加密信息。解密这种密码需要知道加密时用的行数。使用python编码:

# -*- coding: utf-8 -*-def decrypt_rail_fence(ciphertext, rails): fence = [['' for _ in range(len(ciphertext))] for _ in range(rails)] direction = -1 # 控制铁栏的方向,向上或向下 row, column = 0, 0 for char in ciphertext: fence[row][column] = '*' if row == 0 or row == rails - 1: direction *= -1 # 到达铁栏上端或下端时改变方向 row += direction column += 1 # 根据铁栏的模式重新排列密文 idx = 0 for i in range(rails): for j in range(len(ciphertext)): if fence[i][j] == '*' and idx < len(ciphertext): fence[i][j] = ciphertext[idx] idx += 1 # 读取解密后的明文 direction = -1 row, column = 0, 0 plaintext = '' for _ in range(len(ciphertext)): plaintext += fence[row][column] if row == 0 or row == rails - 1: direction *= -1 row += direction column += 1 return plaintextciphertext = "h{igr},aarclietflhf-_peecirroc,eo_fhlels_caifnge"for num_rails in range(4,30): # 密文和假设的行数 # 解密栏栅密码并输出结果 decrypted_text = decrypt_rail_fence(ciphertext, num_rails) print(f"Decrypted text: {decrypted_text}")

4、维吉尼亚密码

今天来和 Caesar 学习一点新知识吧: Google下?听说与FLAG这个字符串有关:kwam{atgksprklzojozb}

维吉尼亚密码是一种多表密码,使用一个关键字或短语来加密消息。在解密维吉尼亚密码时,我们需要知道使用的密钥。解密维吉尼亚密码的过程涉及使用密钥对密文进行逆运算,将其还原为原始消息。使用python编码:

# -*- coding: utf-8 -*-def vigenere_decrypt(ciphertext, keyword): decrypted = '' keyword = keyword.upper() key_length = len(keyword) non_alpha_count = 0 for i, char in enumerate(ciphertext): if char.isalpha(): shift = ord(keyword[(i - non_alpha_count) % key_length]) - ord('A') if char.islower(): decrypted += chr((ord(char) - ord('a') - shift) % 26 + ord('a')) elif char.isupper(): decrypted += chr((ord(char) - ord('A') - shift) % 26 + ord('A')) else: decrypted += char non_alpha_count += 1 return decrypted# 密文和密钥ciphertext = 'kwam{atgksprklzojozb}'keyword = 'FLAG'# 解密decrypted_text = vigenere_decrypt(ciphertext, keyword)print("Decrypted Text:", decrypted_text)# Decrypted Text: flag{vigeneregoodjob}

发布于 2024-01-06 14:32・IP 属地美国

0 阅读:3