> For the complete documentation index, see [llms.txt](https://sarpers-organization.gitbook.io/ctftricks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sarpers-organization.gitbook.io/ctftricks/_chapter-intro-3/obfuscation/rot13-decoding/cryptography-obfuscation-vigenere.md).

# Vigenere

## Vigenere Cryptanalysis (Autosolve)

To break a Vigenere cipher without a known key, utilize an online Vigenere solver equipped with an 'autosolve' or 'break without key' function. These tools typically perform frequency analysis and pattern detection to automatically determine the key length and the key itself, then decrypt the ciphertext. A common pattern detection method is the Kasiski examination, which is used to help determine the length of the key. It involves searching the ciphertext for repeating sequences of letters. The distances between the occurrences of these repeating sequences are calculated. The key length is likely to be a divisor of these distances, often the greatest common divisor (GCD) of several such distances. The distance between the repetitions gives a clue to the length of the key. The key length is likely a divisor of the distances.

Here is a simple Python function to find repeating sequences and their distances:

```python
def find_repeating_sequences(ciphertext, min_len=3):
    repeats = {}
    for i in range(len(ciphertext)):
        for j in range(i + min_len, len(ciphertext)):
            k = 0
            while j + k < len(ciphertext) and ciphertext[i + k] == ciphertext[j + k]:
                k += 1
            if k >= min_len:
                sequence = ciphertext[i : i + k]
                if sequence not in repeats:
                    repeats[sequence] = []
                repeats[sequence].append(j - i)
    return repeats

# Example usage:
# cipher = "..."
# distances = find_repeating_sequences(cipher)
# print(distances)
# Further analysis (GCD) is needed on the distances
```

Once the distances between repeating sequences are found, calculating the greatest common divisor (GCD) of these distances helps identify the most probable key length. A function to calculate the GCD of two numbers can be implemented as follows:

```python
def gcd(a,b):
    while b:
        a, b = b, a % b
    return a
```

By applying this GCD function to the distances found, one can narrow down the possible lengths of the Vigenère key.

Once the key length is determined, the Vigenère cipher essentially breaks down into several simpler Caesar ciphers. Each letter in the key corresponds to a shift in one of these Caesar ciphers. These individual Caesar ciphers can then be solved using frequency analysis of the language.

For instance, given the ciphertext:

```
wpbwbxr wpkzg pltwnhro, txrks_xfqsxrd_bvv_fy_rvmexa_ajk
```

Inputting this into an autosolve tool will attempt to recover the key and corresponding plaintext based on statistical properties of the language used. In a scenario described, such a tool successfully identified the key 'albert' and decrypted the message.
