sumPrimes revenge

Cryptography · CCS Club · Tulgaaaa

Problem

This challenge involves a combination of RSA encryption and matrix operations. Here's a breakdown:

  1. Prime Number Generation:

    • 8 prime numbers (512-bit) are generated and stored in a list called primes.

    • A 2D matrix (key) of size 8x8 is created with smaller 64-bit prime numbers.

  2. Encryption Process:

    • For each i in the range 8, the algorithm computes an encrypted value enc[i] by performing matrix multiplication between the key matrix and the primes list, then applying modulo mod.

  3. RSA Setup:

    • A modulus n is computed as the product of all primes in primes.

    • The public exponent e is set to 65537.

    • The flag (flag.txt) is read, converted into a long integer m, and encrypted using RSA (c = m^e % n).

  4. Outputs:

    • The out.txt file contains the matrix key, modulus mod, encryption values enc, and the ciphertext c.

chall.py

from Crypto.Util.number import getPrime,bytes_to_long

primes = [getPrime(512) for _ in range(8)]
key = [[getPrime(64) for _ in range(8)] for i in range(8)]
mod = getPrime(513)

enc = [0] * 8

for i in range(8):
    for j in range(8):
        enc[i] += key[i][j] * primes[j]
    enc[i] %= mod

n = 1
for i in primes:
    n *= i
e = 65537

flag = open('flag.txt','rb').read()
m = bytes_to_long(flag)

c = pow(m,e,n)

with open('out.txt','w') as f:
    f.write('key = '+ str(key) +'\n')
    f.write('mod = '+ str(mod) +'\n')
    f.write('enc = '+ str(enc) +'\n')
    f.write('c = '+str(c)+'\n')

out.txt

Solution

To recover the primes and decrypt the message:

  1. Matrix Inversion:

  • The matrix key is used in the encryption process. We use matrix inversion to recover the primes used in the encryption.

  • The determinant of the key matrix is checked for invertibility modulo mod. If it's invertible, the inverse matrix is computed.

Equation

primes=key1enc  (mod  m)primes=key^{-1} ⋅enc\; (mod\;m)
  1. Prime Recovery:

  • The inverse of the key matrix is multiplied by the enc values (modulo mod) to recover the original primes.

  1. RSA Decryption:

  • Using the recovered primes, n and phi(n) are calculated.

  • The private key d is computed using e and phi(n), and then the ciphertext c is decrypted using d to retrieve the flag.

solution.py

Last updated