-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Good cipher algorithm named vigenere implemented in Python Programming Language.
- Loading branch information
1 parent
a73c3f1
commit bae3af2
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
text = "mrttaqrhknsw ih puggrur" | ||
custom_key = "happycoding" | ||
|
||
|
||
def vigenere(message, key, direction=1): | ||
key_index = 0 | ||
alphabet = "abcdefghijklmnopqrstuvwxyz" | ||
final_message = "" | ||
|
||
for char in message.lower(): | ||
# Append any non-letter character to the message | ||
if not char.isalpha(): | ||
final_message += char | ||
else: | ||
# Find the right key character to encode/decode | ||
key_char = key[key_index % len(key)] | ||
key_index += 1 | ||
|
||
# Define the offset and the encrypted/decrypted letter | ||
offset = alphabet.index(key_char) | ||
index = alphabet.find(char) | ||
new_index = (index + offset * direction) % len(alphabet) | ||
final_message += alphabet[new_index] | ||
|
||
return final_message | ||
|
||
|
||
def encrypt(message, key): | ||
return vigenere(message, key) | ||
|
||
|
||
def decrypt(message, key): | ||
return vigenere(message, key, -1) | ||
|
||
|
||
print(f"\nEncrypted text: {text}") | ||
print(f"Key: {custom_key}") | ||
decryption = decrypt(text, custom_key) | ||
print(f"\nDecrypted text: {decryption}\n") |