-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
28 lines (26 loc) · 1.43 KB
/
cipher.py
File metadata and controls
28 lines (26 loc) · 1.43 KB
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
def vigenere_cipher_encrypt(plain_text, key):
'''Encrypts text using the Vigenere cipher given a key, while preserving spaces and punctuation.'''
result_encrypted = ""
key_length = len(key)
key_index = 0 # To handle non-alphabetic characters correctly
for char in plain_text:
if char.isalpha(): # Only encrypt alphabetic characters
shift_amount = (ord(key[key_index % key_length]) - ord('a')) % 26
result_encrypted += chr((ord(char) - ord('a') + shift_amount) % 26 + ord('a'))
key_index += 1 # Increment only when a letter is encrypted
else:
result_encrypted += char # Keep spaces and punctuation as is
return result_encrypted
def vigenere_cipher_decrypt(encrypted_text, key):
'''Decrypts text that was encrypted using the Vigenere cipher, while preserving spaces and punctuation.'''
result_decrypted = ""
key_length = len(key)
key_index = 0 # To handle non-alphabetic characters correctly
for char in encrypted_text:
if char.isalpha(): # Only decrypt alphabetic characters
shift_amount = (ord(key[key_index % key_length]) - ord('a')) % 26
result_decrypted += chr((ord(char) - ord('a') - shift_amount + 26) % 26 + ord('a'))
key_index += 1 # Increment only when a letter is decrypted
else:
result_decrypted += char # Keep spaces and punctuation as is
return result_decrypted