-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (58 loc) · 2.34 KB
/
main.py
File metadata and controls
62 lines (58 loc) · 2.34 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
from aes import AES
from halka import HALKA
from utils import split_blocks, pad, str2bits, bits2str
from bitstring import BitArray
if __name__ == "__main__":
aes = True
if aes:
# 16 byte key (128 bits)
master_key = b"blockchain__2023"
aes = AES(master_key)
plain_text = b"Introduction to blockchain is the best cource ever!" * 25
plain_text_blocks = split_blocks(
plain_text, block_size=16, require_padding=False
)
for block in plain_text_blocks:
if len(block) != 16:
block = pad(block, length=16)
print(len(block), 'bytes')
# each block is 16 bytes
block_enc = aes.encrypt_block(block)
block_dec = aes.decrypt_block(block_enc)
assert block == block_dec
print("original block:", block)
print("encrypted block:", block_enc)
print("decrypted block:", block_dec)
print()
print()
print(aes.operations_one_round
)
print(aes.operations_all_rounds)
else:
# 80 bits key (or 10 bytes)
#[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
master_key = '0xffffffffffffffffffff'
key_array = BitArray(hex=master_key)
master_key_bits = list(key_array.bin)
master_key_bits = list(map(int, master_key_bits)) # str list -> int list
halka = HALKA(master_key_bits)
plain_text = "Introduction to blockchain is the best cource ever!" # in bytes
plain_text_bits = str2bits(plain_text) # from str to bits
# halka takes 64 bit (8bytes) block size
plain_text_blocks = split_blocks(
plain_text_bits, block_size=64, require_padding=False
)
for block in plain_text_blocks:
if len(block) != 64:
block = pad(block, length=64, bit=True)
# each block is 8 bytes (64 bits)
block_enc = halka.encrypt_block(block)
block_dec = halka.decrypt_block(block_enc)
print("original block:", block)
print("encrypted block:", block_enc)
print("decrypted block:", block_dec)
assert block == block_dec
print()
print()
print(halka.operations_one_round)
print(halka.operations_all_rounds)