RSA is easy 2 [crypto]

# RSA is easy 2

Provide the flag in this format: HackTM{words_you_found}.

## Recon

Since we don't get `p`,`q` or `n` we simply gotta do freq analysis on the chars. First I map them to single-length symbols so it's somewhat doable and then comes the good ole freq analysis. ...

## Code

```import random
#from my_math import next_prime

#from flag import flag

def egcd(a, b):
x, y, u, v = 0, 1, 1, 0
while a != 0:
q, r = b//a, b % a
m, n = x-u*q, y-v*q
b, a, x, y, u, v = a, r, u, v, m, n
gcd = b
return gcd, x, y

def gen_keys(p, q):
e = 65537
n = p * q
phi = (p - 1) * (q - 1)
gcd, d, b = egcd(e, phi)
# Keys:((pub),  (priv))
return ((e, n), (d, n))

def enc(key, p):
e, n = key
cipher = [pow(ord(char), e, n) for char in p]
return cipher

def dec(pk, c):
key, n = pk
plain = [chr(pow(char, key, n)) for char in c]
return ''.join(plain)

#p = next_prime(random.SystemRandom().getrandbits(512))
#q = next_prime(random.SystemRandom().getrandbits(512))

#flag_key=gen_keys(p, q)

#print("Public key:")
#print(flag_key)

#flag_c=(enc(flag_key, flag))

#print("Encrypted flag:")
#print(flag_c)
enc_msg = -1 #PASTE LIST FROM 'c' FILE HERE
print("enc msg length")
print(len(enc_msg))

def unique(it):
s = set()
for el in it:
if el not in s:
yield el

print("Unique entries: {}".format(len(list(unique(enc_msg)))))

mapping = {}
cur = "A"
for x in enc_msg:
if x not in mapping:
mapping[x] = cur
cur = chr(ord(cur)+1)

mapped = []
for x in enc_msg:
mapped.append(mapping[x])
print(''.join(mapped))

freqs = {}
for x in mapped:
if x not in freqs:
freqs[x] = 0
freqs[x] += 1

freqs_sorted = {k: v for k, v in sorted(freqs.items(), key=lambda item: item)}
print(freqs_sorted)
dec = ''.join(mapped)
dec = dec.replace("E"," ")
dec = dec.replace("C","e")
dec = dec.replace("M","t")
dec = dec.replace("F","i")
dec = dec.replace("G","a")
dec = dec.replace("D","n")
dec = dec.replace("B","h")
dec = dec.replace("A","w")
dec = dec.replace("H","s")
dec = dec.replace("S","d")
dec = dec.replace("L","g")
dec = dec.replace("W","m")
dec = dec.replace("J","o")
dec = dec.replace("N","r")
dec = dec.replace("]","k")
dec = dec.replace("I","c")
dec = dec.replace("K","l")
dec = dec.replace("U","b")
dec = dec.replace("Y","u")
dec = dec.replace("T","v")
dec = dec.replace("O","y")
dec = dec.replace("V","p")
dec = dec.replace("Z","x")
dec = dec.replace("X",".")
dec = dec.replace("[","f")
dec = dec.replace("\\","q")
dec = dec.replace("R",",")
dec = dec.replace("^","'")
dec = dec.replace("_","z")
print(dec)
```

Output:

```enc msg length
1111
Unique entries: 31