ctfs
  • 👋Hello!
  • 🏴Practice
    • 🌐Cryptohack
      • Introduction
      • General
        • Encoding
        • XOR
        • Mathematics
        • Data Formats
      • Symmetric Ciphers
        • How AES Works
        • Symmetric Starter
        • Block Ciphers 1
        • Stream Ciphers
      • Mathematics
        • Modular Math
        • Lattices
      • RSA
        • Starter
        • Primes Part 1
        • Public Exponent
    • 🌐PortSwigger
      • Path Traversal
      • File Upload
      • SSRF Attacks
    • 🌐TryHackMe
      • Basic Skills
      • Linux
      • Penetration Testing
      • Networking
      • OSINT
  • 🚩Competitions
    • 2025
      • 🇮🇩GKSK#9 Osintathon
        • Mudik Lebaran (100 pts)
        • Foto Patung (100 pts)
        • Kolektor Komik (100 pts)
        • Tolong Aku (100 pts)
        • Kencan Pertama (100 pts)
        • Nama Si Pelaku (100 pts)
        • Cekidot (100 pts)
        • Ledakan! (100 pts)
        • 🎹🎶 (100 pts)
        • Batu Besar (100 pts)
        • Komentar (100 pts)
        • Ini dimana? (100 pts)
        • Koordinat Foto Misterius (100 pts)
        • Bianglalaaa (100 pts)
        • Aku Hacker (100 pts)
        • Anjazzz (100 pts)
        • Dikirim Kakakku (129 pts)
        • Ingfo Loker (154 pts)
        • MISSING 00 (100 pts)
        • MISSING 01 (154 pts)
        • Siapa Aku? (154 pts)
      • 🇮🇩IFEST 13
        • Ququerer (250 pts)
        • Silent Trace (370 pts)
        • Nugas (Solved After Event)
        • Free Flag (280 pts)
        • Brute (Solved After Event)
        • Web V1 (Solved After Event)
        • Bypass (Solved After Event)
        • Orbiter (Solved After Event)
      • 🌐OSINT Combine (Wildlife)
        • Getting Started (100 pts)
        • Proper Poppy (100 pts)
        • Legendary Beasts (200 pts)
        • Shadow Fleet (200 pts)
        • Proper Poppy II (200 pts)
        • Not So Smug Smuggler (200 pts)
        • Icy (200 pts)
        • Forest Pals (200 pts)
        • Safari Time II (200 pts)
        • Sneaky! (200 pts)
        • Hello Friend (300 pts)
        • Busy As A (300 pts)
        • Get Rotated! (300 pts)
        • High Seas (300 pts)
        • Nocturnal (300 pts)
        • Safari Time (400 pts)
        • Peak Weather (400 pts)
        • Singsong (400 pts)
        • Falling Fell (500 pts)
        • Kitty Cats (500 pts)
      • 🇮🇩RECURSION
        • let him cook
        • Basic Math
        • Favourite Number
        • Zarrar Cipher (100 pts)
        • paBlue Team (100 pts)
        • [🩸] I wish I was there on December 21, 2024 (100 pts)
        • Small House (200 pts)
        • [🩸] Mission Difference (456 pts)
    • 2024
      • 🌐Santa Claus CTF
        • Complete Picture
        • Day 1 - Big Bang
        • Day 2 - The Summer Job
        • Day 3 - The Visitors
        • Day 4 - Happy Birthday
        • Day 5 - Say My Name
        • Day 6 - Say "Cheese"
        • Day 7 - Revealing Pixels
        • Day 8 - Connecting The Dots
        • Day 9 - 404 Not Found
        • Day 10 - Breaking News
        • Day 11 - Ayrton Santa
        • Day 12 - Lost and Found
        • Day 13 - Planespotting
        • Day 14 - Santa Surveillance
        • Day 15 - Shaken, Not Stirred
        • Day 16 - Status Update
        • Day 17 - Waste ...of Time
        • Day 18 - Lost in Translation
        • Day 19 - Santa's Clones
        • Day 20 - Losing Tracks
        • Day 21 - Sing my Song
        • Day 22 - Eagle Eye
        • Day 23 - Distances Matters
        • Day 24 - Mastermind
      • 🌐Cyber Jawara International
        • Stone Game (100 pts)
        • prepare the tools (176 pts)
        • Persona (484 pts)
      • 🌐OSMOSIS Precon CTF
        • 1 The art of espionage
        • # 2 The Hack
        • # 3 The rabbit hole
        • # 4 The Association
        • # 6 Where is number 5
        • # 5 Who is it
        • Too many Layers
        • The prize
      • 🇮🇩Intechfest
        • Sanity Check (100 pts)
        • Alin (113 pts)
        • GerakSendiri (106 pts)
        • Details (100 pts)
      • 🇮🇩COMPFEST 16
        • Let's Help John! (100 pts)
        • money gone, wallet also gone (100 pts)
        • head’s up! (493 pts)
        • CaRd (304 pts)
        • Sanity Check (100 pts)
      • 🇮🇩Gemastik
        • Baby AES (451 pts)
        • Baby Structured (100 pts)
      • 🇮🇩Technofair 11
        • Kenangan
        • Xorban
        • Marsha
        • Siap Tempur!!
        • eftipi
        • kurang berarti
        • DUMPling
        • Malicious
      • 🌐DIVER OSINT
        • chiban
      • 🇮🇩GKSK#8 Osintathon
        • Sport Location
        • Meklaren lu warna apa boss ?
        • Postcode
        • Rumah Minang
        • Latihan
        • Anak Misterius
        • Travelling Anywhere
        • The Thief
        • Danger Watch
        • Misteri Ruang Angkasa
        • Fun Walk
        • I am Late
        • My Oshi
        • Wellcome to my Youtube Channel
        • Pesan Tersembunyi Wingdings
        • Salah Fokus
        • Apa itu GKSK?
        • Foto Bersejarah
        • Picture
        • Nostalgia Child
        • oldschool
        • Summer Olympic
      • 🇮🇩Techcomfest
        • pemanasan
        • crackable
        • Kuli-ah forensik
    • 2023
      • 🇮🇩Cyber Jawara
        • daruma
      • 🇮🇩NCW
        • Simple (220 pts)
        • wangsaf (320 pts)
        • Sillyville Saga (220 pts)
        • Freminhelp (Solved after event)
      • 🇮🇩Hology 6
      • 🇮🇩SlashRoot 7
        • Summary (441 pts)
        • eeee (480 pts)
        • Zebra Cross (409 pts)
        • Waka Waka eh eh (185 pts)
        • ANABUL (250 pts)
      • 🇮🇩COMPFEST 15
        • not simply corrupted (316 pts)
        • Artificial secret (356 pts)
      • 🇮🇩Gemastik
        • easy AES
        • k-1
        • Gen Z
      • 🇮🇩TechnoFair 10
        • RSA Bwang
        • Marsah
        • rapsodi
        • Pengen Merch JKT 😢
        • space mono
        • file pemberian fans
        • bantu aku mencari sebuah rahasia
    • 2022
      • 🇮🇩NCW
        • sabeb64 (331 pts)
        • cakemath (451 pts)
        • Downloader (244 pts)
        • 199 passcode (Solved after event)
      • 🇮🇩TEDCTF
      • 🇮🇩Gemastik
      • 🇮🇩OSCCTF
      • 🇮🇩ARA
  • 🪦Old Hello
Powered by GitBook
On this page
  • Greatest Common Divisor
  • Extended GCD
  • Modular Arithmetic 1
  • Modular Arithmetic 2
  • Modular Inverting
  1. Practice
  2. Cryptohack
  3. General

Mathematics

Greatest Common Divisor

We can calculate GCD using Euclid's algorithm. This algorithm works on the principle that gcd(a,b) == gcd(a,a%b), provided that a>b.

If gcd(a,b)=1, a and b are coprime.

Here is the code implementation.

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

a = 66528
b = 52920

print(gcd(a, b))

Flag: 1512

Extended GCD

EGCD is an algorithm to find u,v, such that a*u + b*v = gcd(a,b). There are infinitely many us and vs that satisfy the equation, but knowing one of the result from calculating the EGCD would be enough.

The equation above is called Bezout's Identity. In the case where gcd(a,b) = 1, it is guaranteed that there integer solution exists (if gcd(a,b) != 1, the solution won't always be an integer).

def egcd(a, b):
	if b == 0:
		return a, 1, 0

	gcd, x1, y1 = egcd(b, a % b)
	x = y1
	y = x1 - (a // b) * y1

	return gcd, x, y

p = 26513
q = 32321

print(egcd(p, q))

# Output: (1, 10245, -8404)

Flag: -8404

Modular Arithmetic 1

The basic concept of modular arithmetic is similar to how you count in terms of time when looking at a clock.

5 - 7 = 10
2 + 3 = 5
4 + 9 = 1

A clock is a modulo 12. That means that the only number that can exist (even after addition, multiplication, etc) are 0-11. When you want to count beyond 11, the number would loop back to 0.

This is the notation for modulo: a ≡ c mod b or a mod b = c.

It is the same thing as this: a = k*b + c, which is a longer form of division a/b = k + c.

If a is divisible by b, then a mod b = 0.

Below is the solution to the challenge.

x = 11 % 6
y = 8146798528947 % 17

print(min(x, y))

Flag: 4

Modular Arithmetic 2

Suppose we pick a modulus p, where p is prime. The integers modulo p define a field, denoted by Fp. A field is a type of Ring that guarantees that every non-zero element has both additive and multiplicative inverse. For example:

3+(10) ≡ 0 mod 13 --> 10 is additive inverse
3*(9) ≡ 1 mod 13 --> 9 is multiplicative inverse

Fermat's Little Theorem states that for a prime modulus p, and a is not a multiple of p, This statement holds:

a^(p-1) ≡ 1 mod p

For example, 7^(16) mod 17 returns 1.

As for the challenge, we can use the theorem and confirm the result using Python's pow() function.

print(pow(273246787654,65536,65537))

Flag: 1

Modular Inverting

The challenge involves calculating the modular multiplicative inverse of an element. That is, an element d that satisfy a * d ≡ 1 mod p.

There are 2 ways we can approach this, first using Bezout's Identity and second by extending the previously mentioned Fermat's Little Theorem.

1. Using Bezout's Identity

To calculate the inverse, we'll need EGCD. Remember how Bezout's identity states that there exists u & v such that a*u + b*v = gcd(a,b)? Here, we can replace gcd(a,b) with 1 and u with the inverse of a.

         a*d ≡ 1 mod p
         a*d = k*p + 1
   a*d - k*p = 1
a*d + p*(-k) = 1

After this, we'll do egcd(a,p) and what comes out as the value of d is the multiplicative inverse of a.

a = 3
p = 13

def egcd(a, b):
	if b == 0:
		return a, 1, 0

	gcd, x1, y1 = egcd(b, a % b)
	x = y1
	y = x1 - (a // b) * y1

	return gcd, x, y

print(egcd(a, p)[1] % p) # egcd result is -4, we mod it with p to get 9, which is congruent to -4

2. Using Fermat's Little Theorem

We can try multiplying both sides so that we can have the inverse of a on the right side.

		 a^(p-1) ≡ 1 mod p
        a^(p-1) * a^(-1) ≡ a^(-1) mod p
		 a^(p-2) ≡ a^(-1) mod p
		  a^(-1) = a^(p-2) mod p
		       d = a^(p-2) mod p

Now, we can put the formula on Python.

a = 3
p = 13

d = pow(a, p-2, p)
print(d)

Flag: 9

PreviousXORNextData Formats

Last updated 3 months ago

To implement EGCD, we extend the previous Euclidean algorithm. This extended algorithm takes note of the equation in each round, then does back substitution from the base case up until the first round. More complete explanation .

🏴
🌐
here