---
# System prepended metadata

title: (solved) Crypto/pseudo-key

---

# (solved) Crypto/pseudo-key
> Keys are not always as they seem...
Provided files: `pseudo-key-output.txt` and `pseudo-key.py`

`pseudo-key-output.txt`
```
Ciphertext: z_jjaoo_rljlhr_gauf_twv_shaqzb_ljtyut
Pseudo-key: iigesssaemk
```

`pseudo-key.py`

```python=
#!/usr/bin/env python3

from string import ascii_lowercase

chr_to_num = {c: i for i, c in enumerate(ascii_lowercase)}
num_to_chr = {i: c for i, c in enumerate(ascii_lowercase)}

def encrypt(ptxt, key):
    ptxt = ptxt.lower()
    key = ''.join(key[i % len(key)] for i in range(len(ptxt))).lower()
    ctxt = ''
    for i in range(len(ptxt)):
        if ptxt[i] == '_':
            ctxt += '_'
            continue
        x = chr_to_num[ptxt[i]]
        y = chr_to_num[key[i]]
        ctxt += num_to_chr[(x + y) % 26]
    return ctxt

with open('flag.txt') as f, open('key.txt') as k:
    flag = f.read()
    key = k.read()

ptxt = flag[5:-1]

ctxt = encrypt(ptxt,key)
pseudo_key = encrypt(key,key)

print('Ciphertext:',ctxt)
print('Pseudo-key:',pseudo_key)
```

When encrypting the alphabet with itself, it shows that the output keyspace is 13 characters only.

Decrypting the key against itself for both possibilities shows:
```
eedcjjjacgf
rrqpwwwnpts
```

Combining between the two, the password was probably `redpwwwnctf`.

Now decrypting the provided `ctxt` with this key gives the correct flag: `i_guess_pseudo_keys_are_pseudo_secure`.

Used Python code:

```python=
#!/usr/bin/python3
from string import ascii_lowercase
import math

chr_to_num = {c: i for i, c in enumerate(ascii_lowercase)}
num_to_chr = {i: c for i, c in enumerate(ascii_lowercase)}

ctxt='z_jjaoo_rljlhr_gauf_twv_shaqzb_ljtyut'
pseudo_key='iigesssaemk'
keydict='acegikmoqsuwyacegikmoqsuwy'

def encrypt(ptxt, key):
    ptxt = ptxt.lower()
    key = ''.join(key[i % len(key)] for i in range(len(ptxt))).lower()
    ctxt = ''
    for i in range(len(ptxt)):
        if ptxt[i] == '_':
            ctxt += '_'
            continue
        x = chr_to_num[ptxt[i]]
        y = chr_to_num[key[i]]
        ctxt += num_to_chr[(x + y) % 26]
    return ctxt

def decrypt(ctxt, key):
    key = ''.join(key[i % len(key)] for i in range(len(ctxt))).lower()
    ptxt = ''
    for i in range(len(ctxt)):
        if ctxt[i] == '_':
            ptxt += '_'
            continue
        x = chr_to_num[ctxt[i]]
        y = chr_to_num[key[i]]
        ptxt += num_to_chr[(x - y) % 26]
    return ptxt

# Decrypt key
key=""
key2=""
for i in pseudo_key:
    key += num_to_chr[keydict.find(i)]
    key2 += num_to_chr[keydict.find(i)+13]

key = 'redpwwwnctf'
print(decrypt(ctxt,key))
```