-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cesar.py
68 lines (56 loc) · 2.41 KB
/
Cesar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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.
# Nottify the author if you want to use this code before using it.
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)