|
| 1 | +# Vigenere (crypto 100) |
| 2 | + |
| 3 | +###ENG |
| 4 | +[PL](#pl-version) |
| 5 | + |
| 6 | +In the task we get a ciphertext: |
| 7 | + |
| 8 | +``` |
| 9 | +LMIG}RPEDOEEWKJIQIWKJWMNDTSR}TFVUFWYOCBAJBQ |
| 10 | +``` |
| 11 | + |
| 12 | +And information that this is Vigener Cipher with alphabet: |
| 13 | + |
| 14 | +``` |
| 15 | +ABCDEFGHIJKLMNOPQRSTUVWXYZ{} |
| 16 | +``` |
| 17 | + |
| 18 | +And the md5 of plaintext is `f528a6ab914c1ecf856a1d93103948fe` |
| 19 | + |
| 20 | +We of course know the flag prefix `SECCON{` so we can instantly recover the prefix of the key: |
| 21 | + |
| 22 | +```python |
| 23 | +def get_key_prefix(alphabet, ct, known_pt): |
| 24 | + result = "" |
| 25 | + for i in range(len(known_pt)): |
| 26 | + plain = known_pt[i] |
| 27 | + cipher = ct[i] |
| 28 | + key = alphabet[alphabet.index(cipher) - alphabet.index(plain)] |
| 29 | + result += key |
| 30 | + return result |
| 31 | +``` |
| 32 | + |
| 33 | +which gives us `VIGENER` |
| 34 | + |
| 35 | +Next we can just brute-force the missing 4 bytes of the key: |
| 36 | + |
| 37 | +```python |
| 38 | +def decode(alphabet, ct, key): |
| 39 | + result = "" |
| 40 | + for i in range(len(ct)): |
| 41 | + c = ct[i] |
| 42 | + k = key[i % len(key)] |
| 43 | + if k != "?": |
| 44 | + p = alphabet[alphabet.index(c) - alphabet.index(k)] |
| 45 | + else: |
| 46 | + p = "?" |
| 47 | + result += p |
| 48 | + return result |
| 49 | + |
| 50 | + |
| 51 | +def worker(data): |
| 52 | + c, alphabet, ct, key = data |
| 53 | + key += c |
| 54 | + for suffix in itertools.product(alphabet, repeat=4): |
| 55 | + new_key = key + "".join(suffix) |
| 56 | + pt = decode(alphabet, ct, new_key) |
| 57 | + if hashlib.md5(pt).hexdigest() == "f528a6ab914c1ecf856a1d93103948fe": |
| 58 | + print(pt) |
| 59 | + return pt |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ{}" |
| 64 | + ct = "LMIG}RPEDOEEWKJIQIWKJWMNDTSR}TFVUFWYOCBAJBQ" |
| 65 | + key_prefix = get_key_prefix(alphabet, ct, "SECCON{") |
| 66 | + print('key prefix ', key_prefix) |
| 67 | + print(brute(worker, [(c, alphabet, ct, key_prefix) for c in alphabet])) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + freeze_support() |
| 72 | + main() |
| 73 | +``` |
| 74 | + |
| 75 | +Which gives us almost instantly `SECCON{ABABABCDEDEFGHIJJKLMNOPQRSTTUVWXYYZ}` |
| 76 | + |
| 77 | +###PL version |
| 78 | + |
| 79 | +W zadaniu dostajemy zaszyfrowany tekst: |
| 80 | + |
| 81 | +``` |
| 82 | +LMIG}RPEDOEEWKJIQIWKJWMNDTSR}TFVUFWYOCBAJBQ |
| 83 | +``` |
| 84 | + |
| 85 | +I informacje że to szyfr Vigenera z alfabetem: |
| 86 | + |
| 87 | +``` |
| 88 | +ABCDEFGHIJKLMNOPQRSTUVWXYZ{} |
| 89 | +``` |
| 90 | + |
| 91 | +Mamy też md5 plaintextu: `f528a6ab914c1ecf856a1d93103948fe` |
| 92 | + |
| 93 | +I oczywiście znamy prefix flagi `SECCON{` więc możemy od razu odzyskać prefix klucza: |
| 94 | + |
| 95 | +```python |
| 96 | +def get_key_prefix(alphabet, ct, known_pt): |
| 97 | + result = "" |
| 98 | + for i in range(len(known_pt)): |
| 99 | + plain = known_pt[i] |
| 100 | + cipher = ct[i] |
| 101 | + key = alphabet[alphabet.index(cipher) - alphabet.index(plain)] |
| 102 | + result += key |
| 103 | + return result |
| 104 | +``` |
| 105 | + |
| 106 | +co daje nam `VIGENER` |
| 107 | + |
| 108 | +Następnie możemy brute-forcować brakujące 4 bajty klucza: |
| 109 | + |
| 110 | +```python |
| 111 | +def decode(alphabet, ct, key): |
| 112 | + result = "" |
| 113 | + for i in range(len(ct)): |
| 114 | + c = ct[i] |
| 115 | + k = key[i % len(key)] |
| 116 | + if k != "?": |
| 117 | + p = alphabet[alphabet.index(c) - alphabet.index(k)] |
| 118 | + else: |
| 119 | + p = "?" |
| 120 | + result += p |
| 121 | + return result |
| 122 | + |
| 123 | + |
| 124 | +def worker(data): |
| 125 | + c, alphabet, ct, key = data |
| 126 | + key += c |
| 127 | + for suffix in itertools.product(alphabet, repeat=4): |
| 128 | + new_key = key + "".join(suffix) |
| 129 | + pt = decode(alphabet, ct, new_key) |
| 130 | + if hashlib.md5(pt).hexdigest() == "f528a6ab914c1ecf856a1d93103948fe": |
| 131 | + print(pt) |
| 132 | + return pt |
| 133 | + |
| 134 | + |
| 135 | +def main(): |
| 136 | + alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ{}" |
| 137 | + ct = "LMIG}RPEDOEEWKJIQIWKJWMNDTSR}TFVUFWYOCBAJBQ" |
| 138 | + key_prefix = get_key_prefix(alphabet, ct, "SECCON{") |
| 139 | + print('key prefix ', key_prefix) |
| 140 | + print(brute(worker, [(c, alphabet, ct, key_prefix) for c in alphabet])) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + freeze_support() |
| 145 | + main() |
| 146 | +``` |
| 147 | + |
| 148 | +Co od razu daje nam `SECCON{ABABABCDEDEFGHIJJKLMNOPQRSTTUVWXYYZ}` |
0 commit comments