floorsa - BSides AHM 2021

tags: BSides AHM 2021

Challenge Overview

The flag is encrypted with a simple RSA:
Given two primes p,q and plaintext m, the cipher is c=m65537modpq.

Along with the public key and ciphertext, we're also given the following value s:

s=โˆ‘i=1qโˆ’1โŒŠipqโŒ‹

Analysis

First, expand the sum

s=โˆ‘i=1qโˆ’1โŒŠipqโŒ‹=โŒŠpqโŒ‹+โŒŠ2pqโŒ‹+โŒŠ3pqโŒ‹+โ‹ฏ+โŒŠ(qโˆ’1)pqโŒ‹

Let's calculate s+s and group up the 1st and (q-1)-th terms, 2nd and (q-2)-th terms, and so on:

2s=(โŒŠpqโŒ‹+โŒŠ(qโˆ’1)pqโŒ‹)+(โŒŠ2pqโŒ‹+โŒŠ(qโˆ’2)pqโŒ‹)+โ‹ฏ=(p+โŒŠpqโŒ‹+โŒŠโˆ’pqโŒ‹)+(p+โŒŠ2pqโŒ‹+โŒŠโˆ’2pqโŒ‹)+โ‹ฏ

For all iโˆˆ{1,2,โ‹ฏ,qโˆ’1}, โŒŠip/qโŒ‹+โŒŠโˆ’ip/qโŒ‹=โˆ’1 because gcd(p,i)=1.
Therefore,

2s=(pโˆ’1)+(pโˆ’1)+โ‹ฏ=(pโˆ’1)(qโˆ’1)=ฯ•(n)

Now you can find the private key d by calculating

d=eโˆ’1modฯ•(n)=eโˆ’1mod2s

Script

from Crypto.Util.number import inverse exec(open("../distfiles/output.txt").read()) d = inverse(65537, 2*s) m = pow(c, d, n) print(int.to_bytes(m, 2048//8, 'big'))