This time I created several challenges for TSJ CTF 2022, and I published my challenges and writeups in maple3142/My-CTF-Challenges. Due to GitHub's poor support of mathjax rendering in markdown, I decided to create this one for readers of Crypto writeups.

## Futago

• Category: Crypto, CSC (Cursed Shaman Challenges, guessy challenges in TSJ CTF)
• Score: 56/100 (CSC)
• Solves: 14/428

### Description

Who need source for RSA challenges?

### Overview

See the README of this challenge.

### Solution

#### Stage 1

Guess that two public keys shares a prime, so you can factor it with gcd.

#### Stage 2

Found out that , but . Using common modulus attack with found that , so you need to take the cube root over integers to decrypt the message.

#### Stage 3

You got two seemingly normal 2048 bits RSA public key , but it is easy to see is about 1024 bits only.

If you write them like this:

Then substracting them:

Suppose are balanced, this means needs to be really small to keep about 1024 bits only. So we have and .

Then you can multiply them together:

It is easy to see and too. Running fermat factorization on will output these two pairs, and you can factor them with gcd.

## RNG++

• Category: Crypto
• Score: 213/500
• Solves: 21/428

This is actually a broken version of RNG+++.

### Description

I encrypted the flag and messages by xoring them with a random number generator.

### Overview

You got a simple LCG with , where are prime and is a power of . The flag are encrypted by xor it with initial state . Some random messages are generated by randmsg are encrypted by the same way.

### Solution

The first thing to notice is randmsg generated messages are all decimal digits, and they are 0x30 to 0x39 in ASCII. It is obvious that their binary representation are all 0011????, so we can get serveral non continuous bits of states .

The most simple unintended solution is to use z3. You just let the state be bits unsigned bitvector, and write LCG and specify the constraints, and it will magically found the state, so you can get the flag too.

Another more mathematical unintended solution by @Kuruwa: Since is a power of , reducing it to works too. And just do some bruteforce searching over the digits to see if it holds.

The intended solution will be in the writeup of RNG+++.

## babyRSA

• Category: Crypto
• Score: 276/500
• Solves: 13/428

### Overview

We have , where is 1024 bit prime and is 512 bit prime. Define an elliptic curve over , flag (with some random padding) are encoded as x coordinate of a point on curve. The ciphertext is another point , where .

### Solution

By substituting the coorinates of back to the original curve, we have , where are known. If we change this equation to , we get .

Define another polynomial , it is easy to see . Since is small compared to , we use Coppersmith's method to solve for the root and factor .

For more details: Extensions of Coppersmith algorithm

Once you got , the remaining part should be easy. Change the curve to and and compute the order separately, and invert the scalar multiplication to get the flag.

Another way to factor it by @Kuruwa:

Since , the vector will be in lattice spanned by the following basis .

Since is just 512 bits, is probably a short vector in it. Run LLL or Lagrange-Gauss algorithm could produce the vector .

## Top Secret

• Category: Crypro
• Score: 325/500
• Solves: 9/428

### Description

In year 2087, the TSJ corporation invented a new patented stream cipher to protect their communication. One day, another member stole an important file from the CEO's computer, but it turns out to be encrypted. Fortunately, the script used to encrypt the file were stolen too. You, as a member of Nantou Resistance, accept the challenge to decrypt it.

### Overview

This is a weird stream cipher that has an internal state s, and it will update its state using the forward/fast_forward function:

The k is a known constant, and the n is the key of the cipher. The initialize state of the cipher is fixed too. The objective is to decrypt a PNG encrypted by it.

### Solution

First, it is necessary to understand the meaning of s = (s >> 1) ^ ((s & 1) * k). If you ever tried to read the implementation of AES, you may find that it is pretty similar to the xtime operation. (An example in C)

xtime operation means multiplying an element by , and each element is represented by a polynomial of , modulo . In AES, the lowest bit represents , and the highest bit represents .

In this challenge, the order of bits are swapped, so the bit shifting direction are different. And the constant k obvious represents a 128 degree polynomial...?

Actually, key = randbelow(2 ** 128) is meant to confuse people. If you try to construct the polynomial by f'{k:0128b}1' you will see it is not a prime polynomial, because its leftmost bit is 0. The correct degree is 127, so you can construct the field in Sage and implements fast_forward like this:

The next step is to observe that the first 16 bytes of PNG is known, not just 8 bytes, because of the IHDR chunk. Xor the first chunk of ciphertext and the first 16 bytes of PNG gives second state of the cipher. This can be written as:

So this is a discrete log problem in . Unfortunately, is a mersenne prime, so you can't use Pohlig–Hellman to compute .

The intended way is to use Fast Evaluation of Logarithms in Fields of Characteristic Two, which can solve discrete log in this size easily, and it is implemented in pari too.

In Sage, you can just use pari.fflog(e, g) to find x such that g^x == e. In newer version of Sage (9.5 or above), e.log(g) works too.

By the way, @Utaha tells me that it is not necessary to compute the discrete log to solve this challenge. Because the key stream is just like a geometric progression. And can be simply computed by , so it is enough to decrypt the flag actually.

## Cipher Switching Service

• Category: Crypto
• Score: 416/500
• Solves: 4/428

### Description

You can freely swap the encrypted flag between two cryptosystem, but you still don't know what is the flag.

### Overview

The server prints one 1024 bits RSA public key and one 1024 bits ElGamal public key on connection, and gives an RSA encrypted randomly padded flag (padded length is 96 bytes). The length of the flag is 20.

Server supports two operations, allowing you to decrypt a RSA ciphertext and encrypt it using ElGamal, and vice versa.

### Solution

Note: There is a much more simpler unintended solution idea from @Mystiz, scroll to the bottom to see

Using the homomorphic property of RSA, we can get any ciphertext of . ElGamal is encrypted as for some random number and public key .

I use to denote Legendre Symbol here because it is hard to type in Latex. Obviously, . Also, by how is generated, it is easy to see , so we have .

When it decrypts with RSA private, it actually gives , so it is actually.

Suppose , is just , so should hold. If , might not hold. From this, it is possible find such that with binary search, so the flag is approximately .

Since the oracle is not really robust, might need to use some heuristic to make it more stable.

An unintended solution by @Mystiz is to find a such that is just slightly above , this means is . By using homomorphic property of ElGamal you can get the RSA ciphertext of , which is just . And you can just do to find the plaintext.

## Signature

• Category: Crypto
• Score: 469/500
• Solves: 2/428

### Description

Another boring crypto challenge about signatures.

### Overview

This challenge signs 6 signatures using ECDSA on Secp256k1. The nonce is generated by xoring hash and private key . The flag is encrypted with AES-CTR, key are derived from ECDSA private and IV are unknown.

### Solution

This challenge is based on jonasnick/ecdsaPredictableNonce.

Need to use two basic facts:

• For two n bits integer , , where and is their n-th bit.

So can be written as . And it can be substituted back to to get a bunch of equations that have as their roots.

In Secp256k1, is a 256 bit number. So in the jonasnick/ecdsaPredictableNonce, you can collect 256 signatures and solve with linear algebra.

But it forgot an important fact: . This problem can be transform into finding a short vector in lattice, and use LLL to solve it.

Details are in the solution script

After recovering , we still need IV to decrypt the flag with AES-CTR. Notice there is a prefix Congrats! This is your flag: in the plaintext, we can take the first block and xor it with the first block of ciphertext, they decrypt it with the key. This is how AES-CTR works.

## RNG+++

• Category: Crypto
• Score: 469/500
• Solves: 2/428

This upgraded version of RNG++, so you might want to read it before reading this.

### Description

I encrypted the flag and messages by xoring them with a random number generator again. But it should be harder to break this time.

### Overview

Basically same as RNG++, but is a prime this time.

### Solution

Since we only know some bits in each state , it can be written as:

is a known part of each state and are unknowns.This is equivalent to this binary representation:

It is easy to see , and trying to substitute with will find out that it is just some linear combination of . And the fact tells us we need to find something small, so it isn't too hard to think about lattice.

Using coefficient matrix of those polynomials (), we can transform this problem into finding closest vector of a lattice. And using Babai Nearest Plane algorithm on a BKZ reduced basis (LLL reduced basis doesn't work well here) allows you to find the correct .