Hack Your Heart

Back

Introduction to CryptoHack#

Finding Flags#

crypto{y0ur_f1rst_fl4g}

Great Snakes#

crypto{z3n_0f_pyth0n}

ASCII#

1python
2Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
3Type "help", "copyright", "credits" or "license" for more information.
4>>> xl=[99, 114, 121, 112, 116, 111, 123, 65, 83, 67, 73, 73, 95, 112, 114, 49, 110, 116, 52, 98, 108, 51, 125]
5>>> print("".join([chr(i) for i in xl]))
6crypto{ASCII_pr1nt4bl3}
7>>> print("".join(map(chr,xl)))
8crypto{ASCII_pr1nt4bl3}
cmd

Hex#

1>>> print(bytes.fromhex("63727970746f7b596f755f77696c6c5f62655f776f726b696e675f776974685f6865785f737472696e67735f615f6c6f747d"))
2b'crypto{You_will_be_working_with_hex_strings_a_lot}'
bash

Base64#

1Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
2Type "help", "copyright", "credits" or "license" for more information.
3>>> import base64
4>>> bt=bytes.fromhex("72bca9b68fc16ac7beeb8f849dca1d8a783e8acf9679bf9269f7bf")
5>>> print(bt)
6b'r\xbc\xa9\xb6\x8f\xc1j\xc7\xbe\xeb\x8f\x84\x9d\xca\x1d\x8ax>\x8a\xcf\x96y\xbf\x92i\xf7\xbf'
7>>> print(base64.b64encode(bt))
8b'crypto/Base+64+Encoding+is+Web+Safe/'
bash

Bytes and Big Integers#

1>>> from Crypto.Util.number import *
2>>> print(long_to_bytes(11515195063862318899931685488813747395775516287289682636499965282714637259206269))
3b'crypto{3nc0d1n6_4ll_7h3_w4y_d0wn}'
bash

XOR Starter#

 1python
 2Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
 3Type "help", "copyright", "credits" or "license" for more information.
 4>>> st = "label"
 5>>> print("".join(chr(ord(i)^13) for i in st))
 6aloha
 7>>> def xor(a,b):
 8...     return a^b
 9...
10>>> print("".join(chr(xor(ord(i),13)) for i in st))
11aloha
bash

XOR Properties#

1k1_ord = [o for o in bytes.fromhex("a6c8b6733c9b22de7bc0253266a3867df55acde8635e19c73313")]
2k23_ord = [o for o in bytes.fromhex("c1545756687e7573db23aa1c3452a098b71a7fbf0fddddde5fc1")]
3flag_123_ord = [o for o in bytes.fromhex("04ee9855208a2cd59091d04767ae47963170d1660df7f56f5faf")]
4
5k123_ord=[a^b for a,b in zip(k1_ord,k23_ord)]
6flag_ord=[a^b for a,b in zip(flag_123_ord,k123_ord)]
7
8print(bytes(flag_ord))
python

Favourite byte#

1st=bytes.fromhex('73626960647f6b206821204f21254f7d694f7624662065622127234f726927756d')
2for num in range(256):
3    string="".join((chr(num ^ i) for i in st))
4    if string.startswith("crypto"):
5        print(string)
python

You either know, XOR you don’t#

flag格式形如crypto{FLAG}

与密钥异或后得到0e0b213f26041e480b26217f27342e175d0e070a3c5b103e2526217f27342e175d0e077e263451150104

那么这个字符串的前七位字节和最后一个字节与crypto{、}异或就能得到密钥的前七位。最后一位可能不准,因为密钥短的话就循环异或,不一定最后一位就和最后一个字节异或

先计算出来:

1enc_flag = '0e0b213f26041e480b26217f27342e175d0e070a3c5b103e2526217f27342e175d0e077e263451150104'
2partial_flag = 'crypto{'
3
4encflag_ord=[o for o in bytes.fromhex(enc_flag)]
5par_ord=[o for o in partial_flag.encode()]
6key_start="".join([chr(a^b) for (a,b) in zip(encflag_ord,par_ord)])
7key_end=chr(encflag_ord[-1] ^ ord('}'))
8print(key_start)
9print(key_end)
bash

输出myXORke和y,拼在一起有意义:myXORkey

所以认为它是密钥

下面就计算他和异或后的字符串就行

长度不一致,一般是循环使用短序列

这里默认使用pwn库的xor函数

1import pwn
2key="myXORkey"
3enc="0e0b213f26041e480b26217f27342e175d0e070a3c5b103e2526217f27342e175d0e077e263451150104"
4flag=pwn.xor(key.encode(),bytes.fromhex(enc)).decode()
5print(flag)
python

crypto{1f_y0u_Kn0w_En0uGH_y0u_Kn0w_1t_4ll}

Modular Arithmetic#

Greatest Common Divisor#

我自己写的gcd脚本:

 1def gcd(a,b):
 2    x=max(a,b)
 3    y=min(a,b)
 4    while x%y!=0:
 5        a=y
 6        y=(x%y)
 7        x=a
 8    print(y)
 9
10gcd(52920,66528)
python

DeepSeek优化的:

1def gcd(a, b):
2    while b != 0:
3        a, b = b, a % b
4    return a
5
6print(gcd(52920, 66528))  # 输出结果:1512
python

它好聪明

Extended GCD#

原理知道,但是写不出来脚本

GingerCarrot ↗ 的脚本,不是很好理解这个脚本,可能我不明白递归,代码能力退化了

 1
 2def egcd(a, b):
 3    if a == 0:
 4        return b, 0, 1
 5    else:
 6        gcd, x, y = egcd(b % a, a)
 7        return gcd, y - (b // a) * x, x
 8
 9
10print(egcd(26513, 32321))
python

Modular Square Root#

1a = 8479994658316772151941616510097127087554541274812435112009425778595495359700244470400642403747058566807127814165396640215844192327900454116257979487432016769329970767046735091249898678088061634796559556704959846424131820416048436501387617211770124292793308079214153179977624440438616958575058361193975686620046439877308339989295604537867493683872778843921771307305602776398786978353866231661453376056771972069776398999013769588936194859344941268223184197231368887060609212875507518936172060702209557124430477137421847130682601666968691651447236917018634902407704797328509461854842432015009878011354022108661461024768
2p = 30531851861994333252675935111487950694414332763909083514133769861350960895076504687261369815735742549428789138300843082086550059082835141454526618160634109969195486322015775943030060449557090064811940139431735209185996454739163555910726493597222646855506445602953689527405362207926990442391705014604777038685880527537489845359101552442292804398472642356609304810680731556542002301547846635101455995732584071355903010856718680732337369128498655255277003643669031694516851390505923416710601212618443109844041514942401969629158975457079026906304328749039997262960301209158175920051890620947063936347307238412281568760161
3Mod(a,p).sqrt()
python

Chinese Remainder Theorem#

我是手算的

算出了6482,但解是

kM+∑i=1naitiMi ; k∈Z kM + \sum_{i=1}^{n}a_{i}t_{i}M_{i}\ ;\ k\in \mathbb{Z}

一般都是要求最小的解,所以我这边模M即可,得到872

wp有代码版本,推荐几个:

1from Crypto.Util.number import *
2"""
3x ≡ 2 mod 5
4x ≡ 3 mod 11
5x ≡ 5 mod 17
6"""
7print((2*11*17*inverse(11*17,5) + 3*5*17*inverse(5*17,11) + 5*5*11*inverse(5*11,17)) % 935)
python

这个不好理解的话,看下面这个

 1from Crypto.Util.number import inverse
 2
 3M = 5 * 11 * 17
 4M1 = M // 5
 5M2 = M // 11
 6M3 = M // 17
 7
 8N1 = inverse(M1, 5)
 9N2 = inverse(M2, 11)
10N3 = inverse(M3, 17)
11
12ans = pow(2 * M1 * N1 + 3 * M2 * N2 + 5 * M3 * N3, 1, M)
13print(ans)
python
1from sympy.ntheory.modular import crt
2 
3r = [2, 3, 5]
4m = [5, 11, 17]
5 
6print(crt(m, r)[0])
python

Adrien’s Signs#

正经CTF题目了

代码注释如下:

CRYPTOHACK学习
https://h4cker.zip/9c5133/
Author Tajang
Published at June 4, 2025
Copyright CC BY-NC-SA 4.0