Skip to content

Update vigenere.py #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions Algorithms/cryptology/vigenere_cipher/vigenere.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* 2019-11-07 Initial programming

"""

import re
from collections import Counter

alphabet = "abcdefghijklmnopqrstuvwxyz "

Expand Down Expand Up @@ -49,16 +50,48 @@ def decrypt(cipher, key):

return decrypted

# Nettoyage et préparation du texte
def clean_text(text):
return re.sub(r'[^a-z]', '', text.lower())

# Index de Coïncidence
def index_of_coincidence(text):
N = len(text)
freqs = Counter(text)
return sum(f * (f - 1) for f in freqs.values()) / (N * (N - 1)) if N > 1 else 0

# Estimation de la longueur de la clé
def estimate_key_length(text, max_len=20):
ic_scores = []
for key_len in range(1, max_len + 1):
groups = [text[i::key_len] for i in range(key_len)]
avg_ic = sum(index_of_coincidence(group) for group in groups) / key_len
ic_scores.append((key_len, avg_ic))
likely_lengths = sorted(ic_scores, key=lambda x: -x[1])
return likely_lengths[0][0]

# Déduire la clé par fréquence
def deduce_key(text, key_len):
alphabet = "abcdefghijklmnopqrstuvwxyz"
letter_to_index = {ch: idx for idx, ch in enumerate(alphabet)}
index_to_letter = {idx: ch for idx, ch in enumerate(alphabet)}
key = ''

for i in range(key_len):
group = text[i::key_len]
freq = Counter(group)
most_common = freq.most_common(1)[0][0]
shift = (letter_to_index[most_common] - letter_to_index['e']) % 26
key += index_to_letter[shift]
return key

def main():
message = "i loove peanuts"
key = "banana"
message = "gagoclcqgzhzkwnqadgvvqbxtmnqgugczdcgjmumweuqsgirrceqffpxtugipbtqrvwfhqimntgfguwvnujvgaqggccugzogglwzognivugwgugzhtqurxsxriugbvritazipmvmwkgkjmbkgmkxgkcxcusrvbqggfpvgzhiplcuhhwmeqggqcrewitmigzmgzuhcznmuqhgquoqzfkvvmwrutgepiwqvedvgvcusevcpqgspwtuhvtiwciiuipeiegkjarepanmwiowtfsxktuqasnikfeygtgehvpmddswhcuesevlwzbskzkzqfpvwqdekauudrttgedswauusigaxazepbgerlepcdpspinaiifqrmfhgaimnhwqrqgekmpfglttgemiwfnqgdgkjqghganmaggauaiwnmwdgtjirqoyzlgfcznmoqhentkcivpgoqhxcqgzhhwmfqgtqqpfgiqciqoxtmuabeglkehmpowmwktqgzzevikxzvuwwhfekboabkcqvmwruqsgiegtcduiepgywegmrxoxgmvapckywqcyniugwvfmfullkdgdgrwzcuheoiueslpmpgwxrzqrcefmfqgjqzoqgjrmeffenmuemriqvmwipbnqgcwmwdgtgzfgsjnikegekmpfsevzghcmtcpqffplggfhgpczqygcpnfeuvqgslzcpqhivmxuccgvvqpetjqgwcnmgocqomraiiwvedwqgxcdtfkagzgifmvmqycvvximuikqbkfmunzseafqvfwqnxshgarmbjgbfqgetmvqgstcuciiompfocncoqghwvtqtcgbfqqvkavmzgwqufcyvzgfcddikfoypwkdzvuzkhspcqpqgkcxcusrviidoefaeaituaqgfuuqnzmexikfdcwasgspgpcxskgugzhhgarawktqpqgpgotauegugzhhgogzsvvlgroxkowqgfwanmdiuipfsltlgxomtmv "
key = deduce_key(message, 5)
encrypted_message = encrypt(message, key)
decrypted_message = decrypt(encrypted_message, key)

print("Original message: " + message)
print("Encrypted message: " + encrypted_message)
print("Decrypted message: " + decrypted_message)


print("key: " + key)

main()