-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptionclasses.py
More file actions
107 lines (91 loc) · 3.91 KB
/
encryptionclasses.py
File metadata and controls
107 lines (91 loc) · 3.91 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class CaesarCypher:
# Class to handle Caesar cipher encryption and decryption.
def __init__(self, shift: int, input: str):
# Initialize with a shift value and input text.
self.shift = shift
self.input = input
def Encrypt(self) -> str:
# Encrypt the input text using Caesar cipher.
encrypted_data = ''
for char in self.input:
if char.isalpha():
if char.isupper():
encrypted_data += chr((ord(char) + self.shift - 65) % 26 + 65)
else:
encrypted_data += chr((ord(char) + self.shift - 97) % 26 + 97)
else:
encrypted_data += char
return encrypted_data
def Decrypt(self) -> str:
# Decrypt the input text using Caesar cipher.
decrypted_data = ''
for char in self.input:
if char.isalpha():
if char.isupper():
decrypted_data += chr((ord(char) - self.shift - 65) % 26 + 65)
else:
decrypted_data += chr((ord(char) - self.shift - 97) % 26 + 97)
else:
decrypted_data += char
return decrypted_data
class VernamCypher:
# Class to handle Vernam cipher encryption and decryption.
def __init__(self, key: str, input: str):
# Initialize with a key and input text.
if not key:
raise ValueError("Key cannot be empty for Vernam Cipher")
if len(key) != len(input):
raise ValueError("Key length must match input length for Vernam Cipher")
self.key = key
self.input = input
logging.debug(f"VernamCypher initialized with key: {key} and input: {input}")
def Encrypt(self) -> str:
# Encrypt the input text using Vernam cipher.
encrypted_data = ''.join(chr(ord(self.input[i]) ^ ord(self.key[i])) for i in range(len(self.input)))
return encrypted_data
def Decrypt(self) -> str:
# Decrypt the input text using Vernam cipher.
decrypted_data = ''.join(chr(ord(self.input[i]) ^ ord(self.key[i])) for i in range(len(self.input)))
return decrypted_data
class VigenereCipher:
# Class to handle Vigenere cipher encryption and decryption.
def __init__(self, key: str, input_text: str):
# Initialize with a key and input text.
self.key = key
self.input = input_text
def encrypt(self) -> str:
# Encrypt the input text using Vigenere cipher.
encrypted_data = ''
key_length = len(self.key)
key_index = 0
for char in self.input:
if char.isalpha():
shift = ord(self.key[key_index % key_length].lower()) - ord('a')
if char.islower():
encrypted_char = chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
else:
encrypted_char = chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
encrypted_data += encrypted_char
key_index += 1
else:
encrypted_data += char
return encrypted_data
def decrypt(self) -> str:
# Decrypt the input text using Vigenere cipher.
decrypted_data = ''
key_length = len(self.key)
key_index = 0
for char in self.input:
if char.isalpha():
shift = ord(self.key[key_index % key_length].lower()) - ord('a')
if char.islower():
decrypted_char = chr((ord(char) - ord('a') - shift + 26) % 26 + ord('a'))
else:
decrypted_char = chr((ord(char) - ord('A') - shift + 26) % 26 + ord('A'))
decrypted_data += decrypted_char
key_index += 1
else:
decrypted_data += char
return decrypted_data