Cryptography
RSA-TZ Mode [Easy]
Handouts:
from Crypto.Util.number import *
Flag = b'CSCCTF{SXMgSFRVIHNvIGJhZCBhZnRlcmFsbD8}'
Flag = bytes_to_long(Flag)
p = getPrime(512)
q = getPrime(512)
while p >= q:
q = getPrime(512)
n = p*q
e = 0x10001
def encrypt(m):
return pow(m, e, n)
with open('out.txt', 'w') as f:
f.write(f'ct = {encrypt(Flag)}\n')
f.write(f'n = {n}\n')
f.write(f'e = {e}\n')
f.write(f'TZ = {q%p}\n')Solution Explanation
In this challenge, we exploit the fact that the prime factors p and q of the modulus n are close in value given the hint q%p:
Key Observations and Reshaping the Problem
Given Relationship:
Since
q % p = TZ(the remainder whenqis divided byp), we can expressqin terms ofpandTZas:
Where
kis a small integer, and we can assumekis small, since p is very close to q.Rewriting TZ:
Using the relation
TZ = q - p, we can substitute and reshape the equation.
Substituting Into n:
The modulus
nis the product ofpandq, so:
Quadratic Equation:
Expanding and reshaping:
This is a standard quadratic equation of the form:
Here,
a = 1,b = TZ, andc = -n.
Solving for q
qUsing the quadratic formula:
Substituting the values:
Select the positive root, as q must be positive.
Finding p
pOnce q is found, compute p using:
Computing phi and the Private Key d
phi and the Private Key dCompute
phi:The Euler's totient function
phiis given by:
Find
d:The private exponent
dis the modular inverse ofemodulophi:
Decrypting the Ciphertext
Finally, use the private key d to decrypt the ciphertext ct:
The decrypted value flag represents the plaintext or solution to the challenge.In this challenge, we exploit the fact that the prime factors p and q of the modulus n are close in value (i.e., they have the same bit length), and p is slightly less than q. Here's a step-by-step breakdown of the solution:
Solver:
APT [Medium]
Handouts:
Solution Explanation
This challenge leverages RSA encryption and exploits a cryptographic vulnerability known as the common modulus attack. Below is a detailed explanation of the challenge mechanics and how to solve it.
Challenge Details
The challenge introduces the APT game, where:
RSA encryption is used to generate ciphertexts ("shots").
Each player is assigned a unique public exponent (
e).A common modulus
nis used for all encryption operations.
At the start of the game:
The list of public exponents (
Es) and the modulus (n) is displayed.The goal is to recover the plaintext (
FLAG) by collecting ciphertexts and exploiting the vulnerability.
The Cryptographic Vulnerability
Common Modulus Attack
The RSA encryption formula is: Where:
( c ): ciphertext
( m ): plaintext
( e ): public exponent
( n ): common modulus
If two ciphertexts are generated using the same modulus n but different public exponents e1 and e2, and if e1 and e2 are coprime, the plaintext ( m ) can be recovered using the common modulus attack.
Attack Steps
1. Collect Ciphertexts
You need two ciphertexts (( c1, c2 )) encrypted with different public exponents (( e1, e2 )). Ensure:
The ciphertexts are valid (not distorted, indicated by the absence of the
huh?message).( e1 ) and ( e2 ) are coprime.
where is the plaintext, is the common modulus, and are the public exponents with .
Using Bézout's identity, since , there exist integers and such that:
Raising to the power of this equation modulo gives:
Substituting and , we get:
where and satisfy Bézout's identity.
Solver:
You could bruteforce the 6 Es like I did in my solver, or you could map each CTs shot taker to the corresponding entry in the Es array given, feel free to do it however you see fit :))
RZA [Hard]
Handout:
Solution Explanation
This challenge involves exploiting RSA encryption with hints provided through a custom modulus generation mechanism. The ultimate goal is to recover the FLAG using the provided ciphertext and modulus hints.
Challenge Details
The challenge uses:
An RSA public exponent ( e = 65537 ).
A ciphertext ( c ) that encrypts the flag.
Modulus hints provided through a menu option.
At the start of the game:
You are given the ciphertext ( c ).
You can request modulus hints in the form of congruence relations, allowing the recovery of the modulus.
The objective is to reconstruct the modulus ( p ), decrypt the ciphertext, and retrieve the FLAG.
The Cryptographic Vulnerability
The modulus ( p ) is generated with properties that allow reconstruction through the Chinese Remainder Theorem (CRT):
Multiple congruences are provided.
Using CRT, the modulus ( p ) can be recovered.
Attack Steps
1. Collect Congruences
Using the menu option, collect congruence relations (( a \mod m )). Ensure that the moduli ( m ) are pairwise coprime to use CRT.
2. Reconstruct ( p )
Apply CRT to the collected congruences to reconstruct ( p ). The reconstructed value will require a brute-force adjustment due to a missing bit range.
3. Decrypt the Ciphertext
Since the Flag is smaller than P, we don't need Q to decrypt
Proof
We are given:
and need to prove:
Step 1: Express ( c )
From ( ), there exists an integer ( k ) such that:
Step 2: Take modulo ( p )
Taking () on both sides:
Step 3: Simplify
Since ( p ) divides ( ), the term ( ) vanishes, leaving:
With ( p ), compute: The private key ( d ) is calculated as: Finally, decrypt the ciphertext:
Solver:
Digital Encryption League [Insane]
Handout:
Solution Explanation
In this challenge, we exploit a cryptographic vulnerability that arises when the same random value k is reused during the signing of two different messages. This section explains the process and how the attack functions.
Key Concept: The Danger of Reusing k
kWhen signing a message, the user generates a random value k and uses it in the signature calculation. The following values are publicly shared as part of the signature:
Where:
Gis the generator point.zis the hash of the message.dAis the private key.pis the prime order of the curve or group.
If the same k is used for two different signatures, the attacker can recover the private key dA using the following process.
Exploiting the Reuse of k
kObtain Two Signatures with the Same
k:Let the two messages have hashes
z1andz2, and the corresponding signatures are(r, s1)and(r, s2).Since the
kvalue is the same for both signatures, we know:
Calculate
k:Subtract the two equations:
Rearrange to solve for
k:
Recover the Private Key
dA:Using one of the signatures, calculate:
This equation allows the attacker to compute the private key.
Challenge-Specific Requirements
For the attack to work, you must:
Send
inp1, which is added to the static random valuek.Send
inp2, where:
By sending these inputs, the same k value is effectively reused in two signatures, enabling the attack as described above.
Once the attacker has recovered the private key dA, they can find the flag as it is the secret key.
Solver:
Last updated