|
10 | 10 | * 2019-11-07 Initial programming
|
11 | 11 |
|
12 | 12 | """
|
13 |
| - |
| 13 | +import re |
| 14 | +from collections import Counter |
14 | 15 |
|
15 | 16 | alphabet = "abcdefghijklmnopqrstuvwxyz "
|
16 | 17 |
|
@@ -49,16 +50,48 @@ def decrypt(cipher, key):
|
49 | 50 |
|
50 | 51 | return decrypted
|
51 | 52 |
|
| 53 | +# Nettoyage et préparation du texte |
| 54 | +def clean_text(text): |
| 55 | + return re.sub(r'[^a-z]', '', text.lower()) |
| 56 | + |
| 57 | +# Index de Coïncidence |
| 58 | +def index_of_coincidence(text): |
| 59 | + N = len(text) |
| 60 | + freqs = Counter(text) |
| 61 | + return sum(f * (f - 1) for f in freqs.values()) / (N * (N - 1)) if N > 1 else 0 |
| 62 | + |
| 63 | +# Estimation de la longueur de la clé |
| 64 | +def estimate_key_length(text, max_len=20): |
| 65 | + ic_scores = [] |
| 66 | + for key_len in range(1, max_len + 1): |
| 67 | + groups = [text[i::key_len] for i in range(key_len)] |
| 68 | + avg_ic = sum(index_of_coincidence(group) for group in groups) / key_len |
| 69 | + ic_scores.append((key_len, avg_ic)) |
| 70 | + likely_lengths = sorted(ic_scores, key=lambda x: -x[1]) |
| 71 | + return likely_lengths[0][0] |
| 72 | + |
| 73 | +# Déduire la clé par fréquence |
| 74 | +def deduce_key(text, key_len): |
| 75 | + alphabet = "abcdefghijklmnopqrstuvwxyz" |
| 76 | + letter_to_index = {ch: idx for idx, ch in enumerate(alphabet)} |
| 77 | + index_to_letter = {idx: ch for idx, ch in enumerate(alphabet)} |
| 78 | + key = '' |
| 79 | + |
| 80 | + for i in range(key_len): |
| 81 | + group = text[i::key_len] |
| 82 | + freq = Counter(group) |
| 83 | + most_common = freq.most_common(1)[0][0] |
| 84 | + shift = (letter_to_index[most_common] - letter_to_index['e']) % 26 |
| 85 | + key += index_to_letter[shift] |
| 86 | + return key |
52 | 87 |
|
53 | 88 | def main():
|
54 |
| - message = "i loove peanuts" |
55 |
| - key = "banana" |
| 89 | + message = "gagoclcqgzhzkwnqadgvvqbxtmnqgugczdcgjmumweuqsgirrceqffpxtugipbtqrvwfhqimntgfguwvnujvgaqggccugzogglwzognivugwgugzhtqurxsxriugbvritazipmvmwkgkjmbkgmkxgkcxcusrvbqggfpvgzhiplcuhhwmeqggqcrewitmigzmgzuhcznmuqhgquoqzfkvvmwrutgepiwqvedvgvcusevcpqgspwtuhvtiwciiuipeiegkjarepanmwiowtfsxktuqasnikfeygtgehvpmddswhcuesevlwzbskzkzqfpvwqdekauudrttgedswauusigaxazepbgerlepcdpspinaiifqrmfhgaimnhwqrqgekmpfglttgemiwfnqgdgkjqghganmaggauaiwnmwdgtjirqoyzlgfcznmoqhentkcivpgoqhxcqgzhhwmfqgtqqpfgiqciqoxtmuabeglkehmpowmwktqgzzevikxzvuwwhfekboabkcqvmwruqsgiegtcduiepgywegmrxoxgmvapckywqcyniugwvfmfullkdgdgrwzcuheoiueslpmpgwxrzqrcefmfqgjqzoqgjrmeffenmuemriqvmwipbnqgcwmwdgtgzfgsjnikegekmpfsevzghcmtcpqffplggfhgpczqygcpnfeuvqgslzcpqhivmxuccgvvqpetjqgwcnmgocqomraiiwvedwqgxcdtfkagzgifmvmqycvvximuikqbkfmunzseafqvfwqnxshgarmbjgbfqgetmvqgstcuciiompfocncoqghwvtqtcgbfqqvkavmzgwqufcyvzgfcddikfoypwkdzvuzkhspcqpqgkcxcusrviidoefaeaituaqgfuuqnzmexikfdcwasgspgpcxskgugzhhgarawktqpqgpgotauegugzhhgogzsvvlgroxkowqgfwanmdiuipfsltlgxomtmv " |
| 90 | + key = deduce_key(message, 5) |
56 | 91 | encrypted_message = encrypt(message, key)
|
57 | 92 | decrypted_message = decrypt(encrypted_message, key)
|
58 | 93 |
|
59 |
| - print("Original message: " + message) |
60 |
| - print("Encrypted message: " + encrypted_message) |
61 |
| - print("Decrypted message: " + decrypted_message) |
62 |
| - |
| 94 | + |
| 95 | + print("key: " + key) |
63 | 96 |
|
64 | 97 | main()
|
0 commit comments