-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
45 lines (34 loc) · 1.01 KB
/
helpers.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
"""
Common utilities for text encryption
"""
def text_to_nums(text, block_length = 64):
# turn into numbers
plain_bytes = [ord(c) for c in text]
# put the bytes into blocks
plain_nums = []
working_num = 0
length = 0
for byte in plain_bytes:
working_num <<= 8
working_num += byte
length+=1
if length == block_length:
plain_nums.append(working_num)
working_num = 0
length = 0
if length != 0:
plain_nums.append(working_num)
return plain_nums
def num_to_chars(block):
"""
take block (bits), and convert to chars
"""
binary_block = format(block, 'b')
# it might not have a multiple of 8 bits, so zero pad on right
if len(binary_block)%8 != 0:
binary_block = '0'*(8-len(binary_block)%8) + binary_block
chars = []
for i in range(len(binary_block)/8):
byte = binary_block[8*i:8*(i+1)]
chars.append(chr(int(byte, 2)))
return ''.join(chars)