-
Notifications
You must be signed in to change notification settings - Fork 5
/
binary_cipher.py
36 lines (28 loc) · 973 Bytes
/
binary_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
class BinaryCipher:
def __init__(self):
""" This is a python implementation of Binary Cipher. More about it can
be read here : https://en.wikipedia.org/wiki/Binary-to-text_encoding"""
@staticmethod
def encrypt(msg: [int, str]) -> str:
result = None
if isinstance(msg, int):
binary = bin(msg)
result = int(str(binary)[2:])
elif isinstance(msg, str):
result = ''
for ele in msg:
value = ord(ele)
binary_value = bin(value) + ' '
result += binary_value[2:]
return result
@staticmethod
def decrypt(msg: [int, str]) -> str:
result = None
if isinstance(msg, int):
result = int(str(msg), 2)
elif isinstance(msg, str):
result = ''
for ele in msg.split():
value = int(ele, 2)
result += chr(value)
return result