In math, they symbol ≡ means identical to. ϕ is phi from Euler's theorum.
- Choose two large prime numbers
p
andq
- Compute
n = pq
- Compute phi of n
ϕ(n) = (p-1)(q-1)
- Choose
e
such thatgcd(e, ϕ(n)) = 1 (coprime)
- The most common value for
e
is0x10001
or65537
- The most common value for
- Compute
d ≡ e^-1
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
c ≡ m^e (mod n)
where
m
is plaintextc
is ciphertextd
is the private key componentn
is key modulus
assert len(ciphertext) <= 256
message = pow(int.from_bytes(, "little"), key.e, key.n).to_bytes(256, "little")
m ≡ c^d (mod n)
where
m
is plaintextc
is ciphertexte
is the public key componentn
is key modulus
assert len(message) <= 256
ciphertext = pow(int.from_bytes(message, "little"), d, n).to_bytes(256, "little")
In this challenge you will decrypt a secret encrypted with RSA (Rivest–Shamir–Adleman). You will be provided with both the public key and private key.
#!/usr/bin/env python3
import sys
import base64
from pwn import *
def show(name, value, *, b64=True):
log.info(f"{name}: {value}")
def show_b64(name, value):
show(f"{name} (b64)", base64.b64encode(value).decode())
def show_hex(name, value):
show(name, hex(value))
# Run the challenge using pwntools
run = process(b"/challenge/run")
# Read the RSA public exponent e
run.recvuntil(b"e: ")
estr = run.recvline().strip();
e = int(estr, 16)
show_hex("e", e)
# Read the RSA private exponent d
run.recvuntil(b"d: ")
dstr = run.recvline().strip();
d = int(dstr, 16)
show_hex("d", d)
# Read the RSA modulus
run.recvuntil(b"n: ")
nstr = run.recvline().strip();
n = int(nstr, 16)
show_hex("n", n)
# Read the Base64 encoded secret
run.recvuntil(b"secret ciphertext (b64): ")
secretb64 = run.recvline().strip();
secret = base64.b64decode(secretb64)
show_b64("secret", secret)
# Decrypt the RSA encrypted message
plaintext = pow(int.from_bytes(secret, "little"), d, n).to_bytes(256, "little")
log.info(plaintext)
In this challenge you will decrypt a secret encrypted with RSA (Rivest–Shamir–Adleman). You will be provided with the prime factors of n.
#!/usr/bin/env python3
import sys
import base64
from pwn import *
def show(name, value, *, b64=True):
log.info(f"{name}: {value}")
def show_b64(name, value):
show(f"{name} (b64)", base64.b64encode(value).decode())
def show_hex(name, value):
show(name, hex(value))
# Run the challenge using pwntools
run = process(b"/challenge/run")
# Read the RSA public exponent e
run.recvuntil(b"e: ")
estr = run.recvline().strip();
e = int(estr, 16)
show_hex("e", e)
# Read the first factor of the RSA modulus p
run.recvuntil(b"p: ")
pstr = run.recvline().strip();
p = int(pstr, 16)
show_hex("p", p)
# Read the second factor of the RSA modulus q
run.recvuntil(b"q: ")
qstr = run.recvline().strip();
q = int(qstr, 16)
show_hex("q", q)
# Read the Base64 encoded secret
run.recvuntil(b"secret ciphertext (b64): ")
secretb64 = run.recvline().strip();
secret = base64.b64decode(secretb64)
show_b64("secret", secret)
# Compute d and n
n = p * q
phi = (p-1)*(q-1)
d = pow(e, -1, phi)
# Decrypt the RSA encrypted message
plaintext = pow(int.from_bytes(secret, "little"), d, n).to_bytes(256, "little")
log.info(plaintext)
In this challenge you will complete an RSA challenge-response. You will be provided with both the public key and private key.
#!/usr/bin/env python3
import sys
from pwn import *
def show(name, value, *, b64=True):
log.info(f"{name}: {value}")
def show_hex(name, value):
show(name, hex(value))
# Run the challenge using pwntools
run = process(b"/challenge/run")
# Read the RSA public exponent e
run.recvuntil(b"e: ")
estr = run.recvline().strip();
e = int(estr, 16)
show_hex("e", e)
# Read the RSA private exponent d
run.recvuntil(b"d: ")
dstr = run.recvline().strip();
d = int(dstr, 16)
show_hex("d", d)
# Read the RSA modulus
run.recvuntil(b"n: ")
nstr = run.recvline().strip();
n = int(nstr, 16)
show_hex("n", n)
# Read the challenge
run.recvuntil(b"challenge: ")
challengestr = run.recvline().strip();
challenge = int(challengestr, 16)
show_hex("challenge", challenge)
# Calculate and send the response
response = pow(challenge, d, n)
run.recvuntil(b"response: ")
run.sendline(hex(response))
# Pwn the flag
log.info(run.recvall())
In this challenge you will complete an RSA challenge-response. You will provide the public key.
#!/usr/bin/env python3
import sys
import base64
from Crypto.PublicKey import RSA
from pwn import *
def show(name, value, *, b64=True):
log.info(f"{name}: {value}")
def show_b64(name, value):
show(f"{name} (b64)", base64.b64encode(value).decode())
def show_hex(name, value):
show(name, hex(value))
# Run the challenge using pwntools
run = process(b"/challenge/run")
# Generate and send the key
key = RSA.generate(1024)
run.recvuntil(b"e: ")
run.sendline(hex(key.e).encode('ascii'))
run.recvuntil(b"n: ")
run.sendline(hex(key.n).encode('ascii'))
# Read the challenge
run.recvuntil(b"challenge: ")
challengestr = run.recvline().strip();
challenge = int(challengestr, 16)
show_hex("challenge", challenge)
# Calculate and send the response
response = pow(challenge, key.d, key.n)
run.recvuntil(b"response: ")
run.sendline(hex(response).encode('ascii'))
# Pwn the flag
run.recvuntil(b"(b64): ")
cipherb64 = run.recvline().strip();
show("cipherb64", cipherb64)
cipher = base64.b64decode(cipherb64)
flag = pow(int.from_bytes(cipher, "little"), key.d, key.n).to_bytes(256, "little")
log.info(flag)