-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0737fdb
Showing
4 changed files
with
324 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,64 @@ | ||
# Description: This program encrypts and decrypts a message using the Caesar cipher. | ||
# Author: Lerynnx (GitHub) | ||
# Date: 2024-03-11 | ||
# Version: 1.0 | ||
# Do not remove this Attribution if you use this code. | ||
|
||
def affine_encode(a, b, plain_text): | ||
alphabet = 'abcdefghijklmnñopqrstuvwxyz' | ||
encoded_text = '' | ||
|
||
# Read each character of the plain text | ||
for character in plain_text: | ||
# Check if the character is in the alphabet | ||
if character.lower() in alphabet: | ||
# Get the index of the character | ||
index = alphabet.index(character.lower()) | ||
# Apply the encryption formula | ||
encoded_index = (a * index + b) % 27 | ||
# Get the encoded character | ||
encoded_character = alphabet[encoded_index] | ||
# Add the encoded character to the encoded text | ||
encoded_text += encoded_character | ||
# Show the operation performed to calculate the encoded index | ||
print(f"({a}*{character.lower()}+{b})mod 27 --> ({a}*{index}+{b})mod 27 = {encoded_index} --> {encoded_character}") | ||
|
||
# Display the result | ||
print(encoded_text) | ||
|
||
def affine_decode(a, b, encoded_text): | ||
alphabet = 'abcdefghijklmnñopqrstuvwxyz' | ||
decoded_text = '' | ||
|
||
# Calculate the modular multiplicative inverse of a | ||
a_inverse = pow(a, -1, 27) | ||
|
||
# Read each character of the encoded string | ||
for character in encoded_text: | ||
# Check if the character is in the alphabet | ||
if character.lower() in alphabet: | ||
# Get the index of the character | ||
index = alphabet.index(character.lower()) | ||
# Apply the decryption formula with the modular multiplicative inverse of a | ||
decoded_index = (a_inverse * (index + 27 - b)) % 27 | ||
# Get the decoded character | ||
decoded_character = alphabet[decoded_index] | ||
# Add the decoded character to the decoded text | ||
decoded_text += decoded_character | ||
# Show the operation performed to calculate the decoded index | ||
print(f"({a_inverse}*({character.lower()}+27-{b}))mod 27 --> ({a_inverse}*({index}+27-{b}))mod 27 = {decoded_index} --> {decoded_character}") | ||
|
||
# Display the result | ||
print(decoded_text) | ||
|
||
# Example usage | ||
a = 4 | ||
b = 7 | ||
encoded_text = "WCGWWCKFWPWYOMOMNSWOMAYHSNYHAMF" | ||
affine_decode(a, b, encoded_text.lower()) | ||
|
||
# Example usage | ||
a = 5 | ||
b = 8 | ||
plain_text = "Esteesunmensajecifradoconelcifradorafintengoqueexplicarcorrectamentetodoslosejerciciosparapoderpuntuaralmaximo" | ||
affine_encode(a, b, plain_text.lower()) |
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,67 @@ | ||
# Description: This program encrypts and decrypts a message using the Caesar cipher. | ||
# Author: Lerynnx (GitHub) | ||
# Date: 2024-03-11 | ||
# Version: 1.0 | ||
# Do not remove this Attribution if you use this code. | ||
|
||
def caesar_cipher_encrypt(word, key): | ||
alphabet = 'abcdefghijklmnñopqrstuvwxyz' | ||
encrypted_word = '' | ||
|
||
for letter in word: | ||
# Check if the letter is in the alphabet | ||
if letter.lower() in alphabet: | ||
# Calculate the index of the encrypted letter | ||
index = (alphabet.index(letter.lower()) + key) % len(alphabet) | ||
print(f"{letter} --> {alphabet[index]}") | ||
|
||
# Check if the original letter is uppercase | ||
if letter.isupper(): | ||
# Add the encrypted letter in uppercase to the encrypted word | ||
encrypted_word += alphabet[index].upper() | ||
else: | ||
# Add the encrypted letter in lowercase to the encrypted word | ||
encrypted_word += alphabet[index] | ||
else: | ||
# Add the original letter to the encrypted word without encrypting it | ||
encrypted_word += letter | ||
|
||
return encrypted_word | ||
|
||
def caesar_decrypt(ciphertext, key): | ||
alphabet = 'abcdefghijklmnñopqrstuvwxyz' | ||
plaintext = "" | ||
|
||
for char in ciphertext: | ||
# Check if the character is a letter | ||
if char.isalpha(): | ||
# Calculate the index of the decrypted letter | ||
index = (alphabet.index(char.lower()) - key) % len(alphabet) | ||
print(f"{char} --> {alphabet[index]}") | ||
|
||
# Check if the original letter is uppercase | ||
if char.isupper(): | ||
# Add the decrypted letter in uppercase to the plaintext | ||
plaintext += alphabet[index].upper() | ||
else: | ||
# Add the decrypted letter in lowercase to the plaintext | ||
plaintext += alphabet[index] | ||
else: | ||
# If the character is not a letter, add it directly to the plaintext | ||
plaintext += char | ||
|
||
return plaintext | ||
|
||
# Example usage | ||
key = 19 | ||
word = "SOMOSESPIASNUESTRASFUENTESNOVIENENANOSOTROS" | ||
|
||
encrypted_word = caesar_cipher_encrypt(word, key) | ||
print("Encrypted word:", encrypted_word) | ||
|
||
# Example usage | ||
ciphertext = "LHEHLWLIASLFNWLMKSLXNWFMWLFHÑAWFWFSFHLHMKHL" | ||
key = 19 | ||
|
||
plaintext = caesar_decrypt(ciphertext, key) | ||
print("Decrypted message:", plaintext) |
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,140 @@ | ||
""" | ||
Description: Program that encrypts a text using the Caesar cipher algorithm with a given key. | ||
Author: Lerynnx (GitHub) | ||
Date: 2024-03-11 | ||
Version: 1.1 | ||
Do not remove this Attribution if you use this code. | ||
""" | ||
def encrypt(text, key, start): | ||
text_len = len(text) | ||
key_len = len(key) | ||
dictionary = "abcdefghijklmnñopqrstuvwxyz" | ||
encrypted_dictionary = [''] * 27 | ||
encrypted_text = [''] * text_len | ||
i = 0 | ||
finished = False | ||
|
||
while not finished: | ||
if i == key_len: | ||
finished = True | ||
else: | ||
if start + i > 26: | ||
start = (-1 * i) | ||
encrypted_dictionary[start + i] = key[i] | ||
i += 1 | ||
|
||
# Fill spaces to the right | ||
for j in range(start + i, 27): | ||
k = 0 | ||
while k < 27: | ||
found = False | ||
for l in range(j): | ||
if encrypted_dictionary[l] == dictionary[k]: | ||
found = True | ||
break | ||
if not found: | ||
encrypted_dictionary[j] = dictionary[k] | ||
break | ||
k += 1 | ||
|
||
# Fill spaces to the left | ||
for j in range(start): | ||
k = 0 | ||
while k < 27: | ||
found = False | ||
for l in range(27): | ||
if encrypted_dictionary[l] == dictionary[k]: | ||
found = True | ||
break | ||
if not found: | ||
encrypted_dictionary[j] = dictionary[k] | ||
break | ||
k += 1 | ||
|
||
# for i in range(27): | ||
# print(dictionary[i], "-->", encrypted_dictionary[i]) | ||
|
||
# Encrypt text by finding the index of each letter in the dictionary and replacing it with the corresponding letter in the encrypted dictionary | ||
for i in range(text_len): | ||
if text[i] == ' ': | ||
encrypted_text[i] = ' ' | ||
else: | ||
for j in range(27): | ||
if text[i] == dictionary[j]: | ||
encrypted_text[i] = encrypted_dictionary[j] | ||
print(text[i], "-->", encrypted_dictionary[j]) | ||
break | ||
|
||
return ''.join(encrypted_text) | ||
|
||
def decrypt(encrypted_text, key, start): | ||
encrypted_text_len = len(encrypted_text) | ||
key_len = len(key) | ||
dictionary = "abcdefghijklmnñopqrstuvwxyz" | ||
encrypted_dictionary = [''] * 27 | ||
decrypted_text = [''] * encrypted_text_len | ||
i = 0 | ||
finished = False | ||
|
||
while not finished: | ||
if i == key_len: | ||
finished = True | ||
else: | ||
if start + i > 26: | ||
start = (-1 * i) | ||
encrypted_dictionary[start + i] = key[i] | ||
i += 1 | ||
|
||
# Fill spaces to the right | ||
for j in range(start + i, 27): | ||
k = 0 | ||
while k < 27: | ||
found = False | ||
for l in range(j): | ||
if encrypted_dictionary[l] == dictionary[k]: | ||
found = True | ||
break | ||
if not found: | ||
encrypted_dictionary[j] = dictionary[k] | ||
break | ||
k += 1 | ||
|
||
# Fill spaces to the left | ||
for j in range(start): | ||
k = 0 | ||
while k < 27: | ||
found = False | ||
for l in range(27): | ||
if encrypted_dictionary[l] == dictionary[k]: | ||
found = True | ||
break | ||
if not found: | ||
encrypted_dictionary[j] = dictionary[k] | ||
break | ||
k += 1 | ||
|
||
# for i in range(27): | ||
# print(dictionary[i], "-->", encrypted_dictionary[i]) | ||
|
||
# Decrypt text | ||
for i in range(encrypted_text_len): | ||
if encrypted_text[i] == ' ': | ||
decrypted_text[i] = ' ' | ||
else: | ||
for j in range(27): | ||
if encrypted_text[i] == encrypted_dictionary[j]: | ||
decrypted_text[i] = dictionary[j] | ||
print(encrypted_text[i], "-->", dictionary[j]) | ||
break | ||
|
||
return ''.join(decrypted_text) | ||
|
||
text = "tengoqueaprendercriptografiaclasicaparaaprobarlaasignaturadeseguridadinformaticaycriptografia" | ||
key = "ESTAUNCLV" #Yoy can't repeat letters in the key | ||
start = 2 | ||
|
||
encrypted_text = encrypt(text.lower(), key.lower(), start) | ||
print("Encrypted text:", encrypted_text) | ||
|
||
decrypted_text = decrypt(encrypted_text, key.lower(), start) | ||
print("Decrypted text:", decrypted_text) |
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,53 @@ | ||
# Description: This program encrypts and decrypts a message using the Vigenere cipher. | ||
# Author: Lerynnx (GitHub) | ||
# Date: 2024-03-12 | ||
# Version: 1.0 | ||
# Do not remove this Attribution if you use this code. | ||
def vigenere_encode(message, key): | ||
encoded_message = "" | ||
alphabet = "abcdefghijklmnñopqrstuvwxyz" | ||
for i in range(len(message)): | ||
# Get the index of the character in the alphabet | ||
message_index = alphabet.index(message[i]) | ||
key_index = alphabet.index(key[i % len(key)]) | ||
|
||
# Calculate the shift and get the encoded character | ||
shift = (message_index + key_index) % 27 | ||
encoded_character = alphabet[shift] | ||
# Show the operation performed to calculate the encoded index | ||
print(f"({message[i]}+{key[i % len(key)]})mod 27 --> ({message_index}+{key_index})mod 27 = {shift} --> {encoded_character}") | ||
|
||
# Add the encoded character to the encoded message | ||
encoded_message += encoded_character | ||
|
||
return encoded_message | ||
|
||
def vigenere_decode(encoded_message, key): | ||
decoded_message = "" | ||
alphabet = "abcdefghijklmnñopqrstuvwxyz" | ||
for i in range(len(encoded_message)): | ||
# Get the index of the encoded character in the alphabet | ||
encoded_index = alphabet.index(encoded_message[i]) | ||
key_index = alphabet.index(key[i % len(key)]) | ||
|
||
# Calculate the shift and get the decoded character | ||
shift = (encoded_index - key_index) % 27 | ||
decoded_character = alphabet[shift] | ||
# Show the operation performed to calculate the encoded index | ||
print(f"({encoded_message[i]}-{key[i % len(key)]})mod 27 --> ({encoded_index}-{key_index})mod 27 = {shift} --> {decoded_character}") | ||
|
||
# Add the decoded character to the decoded message | ||
decoded_message += decoded_character | ||
|
||
return decoded_message | ||
|
||
|
||
message = "WHRYQKYVOMUBJHAMHRWWBACXMJ" | ||
key = "soyunbuenestudiante" | ||
|
||
#TODO it's case sensitive, do not use upper case | ||
encoded_message = vigenere_encode(message, key) | ||
print("Encoded message:", encoded_message) | ||
|
||
decoded_message = vigenere_decode(encoded_message.lower(), key) | ||
print("Decoded message:", decoded_message) |