# Krypton Level 6 → Level 7

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FXmzG2szzSQoiY2wzNS3S%2FPasted%20image%2020250226233557.png?alt=media&#x26;token=c8a62831-118d-472e-8434-63c11dadab40" alt=""><figcaption></figcaption></figure>

The challenge involves a **stream cipher** that uses a repeating key and a weak "random" number.

```bash
cd /krypton/krypton6
```

```bash
ls -la
```

```bash
file *
```

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FkOJLxQkBvFaRFhrWbR7A%2FPasted%20image%2020250226233559.png?alt=media&#x26;token=8cef4d89-3fea-44dc-991e-165615c1b40b" alt=""><figcaption></figcaption></figure>

There are the provided hints:

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FgCPArNF44XQCF7ugyRlC%2FPasted%20image%2020250226233834.png?alt=media&#x26;token=a38dbbbb-e2ff-4090-bb00-5b2d8ee111e0" alt=""><figcaption></figcaption></figure>

The README contains the challenge description as always.

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FoqtWCb1VwbJgqOG8o2kK%2FPasted%20image%2020250226234214.png?alt=media&#x26;token=876e6f06-3b46-4354-a0c8-dde95274d52f" alt=""><figcaption></figcaption></figure>

And the encrypt binary which will encrypt any string we gave it using the keyfile.dat in the same directory.

Lets test it out:

```bash
python3 -c "print('A' * 137)" > /tmp/Caesar3/plaintext.txt
```

```bash
./encrypt6 /tmp/Caesar3/plaintext.txt /tmp/Caesar3/cipher.txt
```

```bash
cat /tmp/Caesar3/cipher.txt
```

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2Fx7p9elClyBkHZLxoX5QM%2FPasted%20image%2020250226235657.png?alt=media&#x26;token=6a937f84-04ce-4370-959d-a8d19e5beaf9" alt=""><figcaption></figcaption></figure>

As shown every 30 character, the cipher text will repeat it self:

```txt
EICTDGYIYZKTHNSIRFXYCPFUEOCKRN
EICTDGYIYZKTHNSIRFXYCPFUEOCKRN
EICTDGYIYZKTHNSIRFXYCPFUEOCKRN
EICTDGYIYZKTHNSIRFXYCPFUEOCKRN
EICTDGYIYZKTHNSIR
```

Also with other letter:

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2Fqc7X40YfQxgfMksVDR7Y%2FPasted%20image%2020250227000428.png?alt=media&#x26;token=63c0a8d9-9d0b-48d6-99a8-56c8444c4760" alt=""><figcaption></figcaption></figure>

The same process, where for example the first A and B letters are shifted 4 bytes, where the second A and B are shifted 8 bytes, and the third ones are shifted 2 bytes, and so on.

Knowing that we can easily recover the correct password, using the same method.

We can get how many character a character has been shifted starting from the first one, and we will use the same length of the encrypted flag:

```python
Cipher_Text = "PNUKLYLWRQKGKBE"  
Encoded_As = "EICTDGYIYZKTHNSIRFXYCPFUEOCKRN"  
List = []  
  
for i in range(len(Cipher_Text)):  
    As = "A" * len(Cipher_Text)  
    List.append(ord(Encoded_As[i]) - ord(As[i]))  
  
print(List)
```

```bash
vim script.py
```

```bash
cat script.py
```

```bash
python3 script.py
```

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FrelV2BHlpLxUZEVCWLpO%2FPasted%20image%2020250227001851.png?alt=media&#x26;token=081e0ac7-6ecf-4dba-8761-56aa5b70fbb4" alt=""><figcaption></figcaption></figure>

This list contains where each A is shifted depending on its place in the string up to the length of the cipher text.

We will use this list to subtract the cipher text for each character with its index place:

```python
Cipher_Text = "PNUKLYLWRQKGKBE"  
Encoded_As = "EICTDGYIYZKTHNSIRFXYCPFUEOCKRN"  
List = []  
  
for i in range(len(Cipher_Text)):  
    As = "A" * len(Cipher_Text)  
    List.append(ord(Encoded_As[i]) - ord(As[i]))  
  
Decrypted_Text = ""  
  
for j in range(len(Cipher_Text)):  
    Decrypted_Text += chr(ord(Cipher_Text[j]) - List[j])  
  
print(Decrypted_Text)
```

```bash
vim script.py

:wq
```

```bash
cat script.py
```

```bash
python3 script.py
```

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2Fh1gXbtIIi2NDdXTHqqdL%2FPasted%20image%2020250227004229.png?alt=media&#x26;token=f1e9733b-25b9-465a-b3a8-ed12a8610acd" alt=""><figcaption></figcaption></figure>

As shown some letters are not capital letters just like the original cipher text, so we need to make sure that if a characters goes under the capital letter 'A' to add 26 to wrap around the alphabet:

```python
Cipher_Text = "PNUKLYLWRQKGKBE"  
Encoded_As = "EICTDGYIYZKTHNSIRFXYCPFUEOCKRN"  
List = []  
  
for i in range(len(Cipher_Text)):  
    As = "A" * len(Cipher_Text)  
    List.append(ord(Encoded_As[i]) - ord(As[i]))  
  
Decrypted_Text = ""  
  
for j in range(len(Cipher_Text)):  
    Subtract = ord(Cipher_Text[j]) - List[j]  
    if Subtract < ord('A'):  
        Subtract += 26  
    Decrypted_Text += chr(Subtract)  
  
print(Decrypted_Text)
```

```bash
vim script.py

:wq
```

```bash
cat script.py
```

```bash
python3 script.py
```

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2For51Ug7mUUbJLdY3WEos%2FPasted%20image%2020250227004453.png?alt=media&#x26;token=08b988ec-4864-4e9a-bba3-6ddb67bacdee" alt=""><figcaption></figcaption></figure>

Here is the correct password.

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FzPCFPHsZuLZ07dksDVGT%2Fimage.png?alt=media&#x26;token=0d64d648-f356-4514-8cef-35df02e7aa74" alt=""><figcaption></figcaption></figure>
