Skip to content

Commit bebac8e

Browse files
authored
Merge pull request #22 from murugan-k-0204/main
Bacon Cipher and ROT13 Cipher python implementation
2 parents 979a266 + cacaf3c commit bebac8e

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

decrypto/bacon_cipher.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class BaconCipher:
2+
def __init__(self) -> None:
3+
self.baco_dict = {
4+
'A':'aaaaa', 'B':'aaaab', 'C':'aaaba',
5+
'D':'aaabb', 'E':'aabaa', 'F':'aabab',
6+
'G':'aabba', 'H':'aabbb', 'I':'abaaa',
7+
'J':'abaab', 'K':'ababa', 'L':'ababb',
8+
'M':'abbaa', 'N':'abbab', 'O':'abbba',
9+
'P':'abbbb', 'Q':'baaaa', 'R':'baaab',
10+
'S':'baaba', 'T':'baabb', 'U':'babaa',
11+
'V':'babab', 'W':'babba', 'X':'babbb',
12+
'Y':'bbaaa', 'Z':'bbaab'
13+
}
14+
self.reverse_baco = {
15+
self.baco_dict[key] : key for key in self.baco_dict }
16+
17+
def encrypt(self, message : str) -> str:
18+
cipher = ''
19+
for letter in message.upper():
20+
# checks for space
21+
if(letter != ' '):
22+
# adds the ciphertext corresponding to the
23+
# plaintext from the dictionary
24+
cipher += self.baco_dict[letter]
25+
else:
26+
# adds space
27+
cipher += ' '
28+
return cipher
29+
30+
def decrypt(self,message):
31+
decipher = ''
32+
i = 0
33+
while True :
34+
if(i < len(message)-4):
35+
# extracting a set of ciphertext
36+
# from the message which is of size 5
37+
substr = message[i:i + 5]
38+
# checking for space as the first
39+
# character of the substring
40+
if(substr[0] != ' '):
41+
decipher += self.reverse_baco[substr]
42+
i += 5 # to get the next set of ciphertext
43+
else:
44+
# adds space
45+
decipher += ' '
46+
i += 1 # index next to the space
47+
else:
48+
break
49+
return decipher

decrypto/rot13_cipher.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class ROT13Cipher:
2+
def __init__(self) -> None:
3+
pass
4+
5+
def encrypt(self, message):
6+
cipher = ''
7+
for letter in message.upper():
8+
# checking for space
9+
if(letter != ' '):
10+
num = chr((ord(letter) - ord('A') + 13 ) % 26 + ord('A'))
11+
cipher += num
12+
else:
13+
# adds space
14+
cipher += ' '
15+
return cipher
16+
17+
def decrypt(self, message):
18+
decipher = ''
19+
for letter in message.upper():
20+
# checks for space
21+
if(letter != ' '):
22+
num = chr(((ord(letter) - ord('A') - 13 + 26) % 26) + ord('A'))
23+
decipher += num
24+
else:
25+
# adds space
26+
decipher += ' '
27+
return decipher

0 commit comments

Comments
 (0)