-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcipher.py
126 lines (107 loc) · 4.39 KB
/
cipher.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class CaesarCipher:
def __init__(self, key=3):
self.key = key
def encrypt(self, plaintext_bytes: bytes) -> bytes:
plaintext = plaintext_bytes.decode()
encrypted = ""
for char in plaintext:
if char.isalpha():
ascii_offset = 65 if char.isupper() else 97
encrypted += chr((ord(char) + self.key - ascii_offset) % 26 + ascii_offset)
else:
encrypted += char
return encrypted.encode()
def decrypt(self, ciphertext_bytes: bytes) -> bytes:
ciphertext = ciphertext_bytes.decode()
decrypted = ""
for char in ciphertext:
if char.isalpha():
ascii_offset = 65 if char.isupper() else 97
decrypted += chr((ord(char) - self.key - ascii_offset) % 26 + ascii_offset)
else:
decrypted += char
return decrypted.encode()
class VignereCipher:
def __init__(self, key="KOSEKIBIBOONINE"):
self.key = key
def extend_key(self, text: str) -> str:
extended_key = ""
key_pointer = 0
for char in text:
if char.isalpha():
extended_key += self.key[key_pointer]
key_pointer = (key_pointer + 1) % len(self.key)
else:
extended_key += char
return extended_key
def encrypt(self, plaintext_bytes: bytes) -> bytes:
plaintext = plaintext_bytes.decode()
extended_key = self.extend_key(plaintext)
encrypted = ""
index = 0
while index < len(plaintext):
char = plaintext[index]
if char.isalpha():
ascii_offset = 65 if char.isupper() else 97
encrypted += chr((ord(char) + ord(extended_key[index]) - ascii_offset) % 26 + ascii_offset)
else:
encrypted += char
index += 1
return encrypted.encode()
def decrypt(self, ciphertext_bytes: bytes) -> bytes:
ciphertext = ciphertext_bytes.decode()
extended_key = self.extend_key(ciphertext)
decrypted = ""
index = 0
while index < len(ciphertext):
char = ciphertext[index]
if char.isalpha():
ascii_offset = 65 if char.isupper() else 97
decrypted += chr((ord(char) - ord(extended_key[index]) - ascii_offset) % 26 + ascii_offset)
else:
decrypted += char
index += 1
return decrypted.encode()
class SubstitutionCipher:
def __init__(self):
self.subsdict = {
'a': 'g', 'b': 'e', 'c': 'x', 'd': 'n', 'e': 'z',
'f': 'y', 'g': 'h', 'h': 't', 'i': 'w', 'j': 'q',
'k': 'o', 'l': 'r', 'm': 'j', 'n': 'f', 'o': 'd',
'p': 'u', 'q': 'c', 'r': 'a', 's': 'p', 't': 'k',
'u': 's', 'v': 'l', 'w': 'v', 'x': 'i', 'y': 'b', 'z': 'm',
'A': 'G', 'B': 'E', 'C': 'X', 'D': 'N', 'E': 'Z',
'F': 'Y', 'G': 'H', 'H': 'T', 'I': 'W', 'J': 'Q',
'K': 'O', 'L': 'R', 'M': 'J', 'N': 'F', 'O': 'D',
'P': 'U', 'Q': 'C', 'R': 'A', 'S': 'P', 'T': 'K',
'U': 'S', 'V': 'L', 'W': 'V', 'X': 'I', 'Y': 'B', 'Z': 'M'
}
def encrypt(self, plaintext_bytes: bytes) -> bytes:
plaintext = plaintext_bytes.decode()
encryptedtext = ""
for char in plaintext:
encryptedtext += self.subsdict.get(char, char)
# print(self.subsdict.get(char, char))
# print(encryptedtext)
return encryptedtext.encode()
def decrypt(self, ciphertext_bytes: bytes) -> bytes:
ciphertext = ciphertext_bytes.decode()
decryptedtext = ""
for char in ciphertext:
if char in self.subsdict.values():
for key, value in self.subsdict.items():
if value == char:
# print(char)
decryptedtext += key
break
else:
decryptedtext += char
return decryptedtext.encode()
if __name__ == "__main__":
plaintext = input("Masukkan plain text: ")
cipher = SubstitutionCipher()
print("Plain Text: ", plaintext)
ciphertext = cipher.encrypt(plaintext.encode())
print("Encrypted Text: ", ciphertext)
decryptedtext = cipher.decrypt(ciphertext)
print("Decrypted Text: ", decryptedtext)