Skip to content

Commit cd4a148

Browse files
committed
Implemented test for OFB & small fixes to OFB...
1 parent edfbd96 commit cd4a148

File tree

11 files changed

+74
-39
lines changed

11 files changed

+74
-39
lines changed

.github/workflows/core.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ jobs:
6565
uses: 'marvinpinto/action-automatic-releases@latest'
6666
with:
6767
repo_token: ${{ secrets.RELEASE_TOKEN }}
68-
automatic_release_tag: v1.1.6
68+
automatic_release_tag: v1.2.0
6969
prerelease: false
70-
title: v1.1.6
70+
title: v1.2.0
7171
files: |
7272
dist/*
7373

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Implemented running modes
2121
- [x] **CBC** - Cipher Block Chaining mode (For more information see ...)
2222
- [x] **PCBC** - Propagating Cipher Block Chaining mode (For more information see ...)
2323
- [ ] **CFB** - Cipher Feedback mode (For more information see ...)
24-
- [ ] **OFB** - Output FeedBack mode (For more information see ...)
24+
- [x] **OFB** - Output FeedBack mode (For more information see ...)
2525
- [ ] **CTR** - Counter mode (For more information see ...)
2626
- [ ] **GCM** - Galois/Counter mode (For more information see ...)
2727

src/PyAES/AES.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,13 @@ def ofb_enc(key, file_path, iv, terminal_width=80):
592592
file_size = getsize(file_path)
593593
progress = 0
594594
progress = progress_bar(progress, file_size, terminal_width)
595-
iv = [int(iv[i:i+2], 16) for i in range(0, len(iv), 2)]
595+
mix = [int(iv[i:i+2], 16) for i in range(0, len(iv), 2)]
596+
iv = mix
596597

597598
with open(f"{file_path}.enc", 'wb') as output, open(file_path, 'rb') as data:
598599
for i in range(int(file_size/16)):
599600
raw = [i for i in data.read(16)]
600-
mix = encryption_rounds(iv, key)
601+
mix = encryption_rounds(mix, key)
601602
result = xor(raw, mix)
602603
output.write(bytes(result))
603604
progress = progress_bar(progress, file_size, terminal_width)
@@ -618,9 +619,9 @@ def ofb_enc(key, file_path, iv, terminal_width=80):
618619
output.write(bytes(result + identifier))
619620
progress = progress_bar(progress, file_size, terminal_width)
620621
else:
621-
mix = bytes(encryption_rounds(mix, key))
622+
mix = encryption_rounds(mix, key)
622623
identifier = xor([0 for i in range(16)], mix)
623-
output.write(identifier)
624+
output.write(bytes(identifier))
624625
progress = progress_bar(progress, file_size, terminal_width)
625626
progress = progress_bar(progress, file_size, terminal_width)
626627
remove(file_path)
@@ -665,4 +666,4 @@ def ofb_dec(key, file_path, iv, terminal_width=80):
665666
output.write(result)
666667
progress = progress_bar(progress, file_size, terminal_width)
667668
progress = progress_bar(progress, file_size, terminal_width)
668-
remove(file_path)
669+
remove(file_path)

src/PyAES/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
__copyright__ = 'Copyright 2022, Gabriel Lindeblad'
2020
__credits__ = [""]
2121
__license__ = 'MIT'
22-
__version__ = '1.1.6'
22+
__version__ = '1.2.0'
2323
__maintainer__ = 'Gabriel Lindeblad'
2424
__email__ = 'Gabriel.lindeblad@icloud.com'
2525
__status__ = 'Development'

src/PyAES/decrypt.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def decrypt(key, file_path, running_mode, iv=None, terminal_size=80):
2323
AES.cbc_dec(key, file_path, iv, terminal_size)
2424
elif running_mode == "PCBC" and iv is not None:
2525
AES.pcbc_dec(key, file_path, iv, terminal_size)
26+
elif running_mode == "OFB" and iv is not None:
27+
AES.ofb_dec(key, file_path, iv, terminal_size)
2628
else:
2729
raise Exception("Running mode not supported")
2830

src/PyAES/encrypt.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def encrypt(key, file_path, running_mode, iv=None, terminal_size=80):
2121
AES.cbc_enc(key, file_path, iv, terminal_size)
2222
elif running_mode == "PCBC" and iv is not None:
2323
AES.pcbc_enc(key, file_path, iv, terminal_size)
24+
elif running_mode == "OFB" and iv is not None:
25+
AES.ofb_enc(key, file_path, iv, terminal_size)
2426
else:
2527
raise Exception("Running mode not supported")
2628

tests/test_decryption.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def test_aes_decryption_ECB(data, key, file_name, expected):
2727

2828
assert result == expected
2929

30+
3031
@pytest.mark.parametrize("data,key,file_name,iv,expected", [
3132
# 128 bit
3233
(b'\xe4\xa7\x0e\xbd\x84\xfa\xf5\xd8`\xb8\xa1\x10\x0b~\xadh\x89Feso\xc5~_|\xe9\x1bG\xd9*\\\x81', "2b7e151628aed2a6abf7158809cf4f3c", "tmp1.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890'),
@@ -53,6 +54,7 @@ def test_aes_decryption_CBC(data, key, file_name, iv, expected):
5354

5455
assert result == expected
5556

57+
5658
@pytest.mark.parametrize("data,key,file_name,iv,expected", [
5759
# 128 bit
5860
(b'\xe4\xa7\x0e\xbd\x84\xfa\xf5\xd8`\xb8\xa1\x10\x0b~\xadhJ\xa98\x9f\xceZ\xd4\x9f"\xde\x00\xf6w\xa9\x1b\x05', "2b7e151628aed2a6abf7158809cf4f3c", "tmp1.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890'),
@@ -78,3 +80,30 @@ def test_aes_decryption_PCBC(data, key, file_name, iv, expected):
7880
os.remove(file_name)
7981

8082
assert result == expected
83+
84+
85+
@pytest.mark.parametrize("data,key,file_name,iv,expected", [
86+
# 128 bit
87+
(b'a\xccT\xf8\xac[\x05\x8e\xe397\xe9\x9b\xaf\xec`\xd9\xa4\xda\xda\x08\x92#\x9fk\x8b=v\x80\xe1Vr', "2b7e151628aed2a6abf7158809cf4f3c", "tmp1.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890'),
88+
(b'a\xccT\xf8\xac[\x05\x8e\xe39\x06\xdb\xa8\x9b\xd9V\xd9\xa4\xda\xda\x08\x92#\x9fk\x8b=v\x80\xe1Vt', "2b7e151628aed2a6abf7158809cf4f3c", "tmp2.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890123456'),
89+
(b'a\xccT\xf8\xac[\x05\x8e\xe39\x06\xdb\xa8\x9b\xd9V\xee\x9c\xe3\xea\x08\x92#\x9fk\x8b=v\x80\xe1Vt\xa7\x88\x19X?\x03\x08\xe7\xa6\xbf6\xb18j\xbf/', "2b7e151628aed2a6abf7158809cf4f3c", "tmp7.txt", "000102030405060708090a0b0c0d0e0f", b'12345678901234567890'),
90+
(b"a\xccT\xf8\xac[\x05\x8e\xe39\x06\xdb\xa8\x9b\xd9V\xee\x9c\xe3\xea9\xa0\x10\xab^\xbd\nN\xb9\xd1gF\x94\xbc,n\x08;1\xd7\xa6\xbf6\xb18j\xbf#\xc6\xd3Am)\x16\\o\xcb\x8eQ\xa2'\xba\x99F", "2b7e151628aed2a6abf7158809cf4f3c", "tmp8.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890123456789012345678901234567890'),
91+
# 192 bit
92+
(b"\x97;\x80\xb9\xc6\x87$\x05\xe4\xcf'\x18\xba\tV^R\xef\x01\xdaR`/\xe0\x97_x\xac\x84\xbf\x8aV", "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "tmp3.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890'),
93+
(b'\x97;\x80\xb9\xc6\x87$\x05\xe4\xcf\x16*\x89=chR\xef\x01\xdaR`/\xe0\x97_x\xac\x84\xbf\x8aP', "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "tmp4.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890123456'),
94+
# 256 bit
95+
(b'\x86\x8d\ti\xc1\x0f\xbe\xe5\xae\xc0\xfa\x97\xeb\xce/J\xe1\xc6V0^\xd1\xa7\xa6V8\x05to\xe0>\xda', "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "tmp5.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890'),
96+
(b'\x86\x8d\ti\xc1\x0f\xbe\xe5\xae\xc0\xcb\xa5\xd8\xfa\x1a|\xe1\xc6V0^\xd1\xa7\xa6V8\x05to\xe0>\xdc', "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "tmp6.txt", "000102030405060708090a0b0c0d0e0f", b'1234567890123456')
97+
])
98+
def test_aes_decryption_OFB(data, key, file_name, iv, expected):
99+
with open(f"{file_name}.enc", "wb") as file:
100+
file.write(data)
101+
102+
decrypt(key, f"{file_name}.enc", "OFB", iv)
103+
104+
with open(file_name, "rb") as file:
105+
result = file.read()
106+
107+
os.remove(file_name)
108+
109+
assert result == expected

tests/test_encryption.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_aes_encrypt_ECB(data, key, file_name, expected):
2626

2727
assert result == expected
2828

29+
2930
@pytest.mark.parametrize("data,key,file_name,iv,expected", [
3031
# 128 bit
3132
(b'1234567890', "2b7e151628aed2a6abf7158809cf4f3c", "tmp.txt", "000102030405060708090a0b0c0d0e0f", b'\xe4\xa7\x0e\xbd\x84\xfa\xf5\xd8`\xb8\xa1\x10\x0b~\xadh\x89Feso\xc5~_|\xe9\x1bG\xd9*\\\x81'),
@@ -50,6 +51,7 @@ def test_aes_encrypt_CBC(data, key, file_name, iv, expected):
5051

5152
assert result == expected
5253

54+
5355
@pytest.mark.parametrize("data,key,file_name,iv,expected", [
5456
# 128 bit
5557
(b'1234567890', "2b7e151628aed2a6abf7158809cf4f3c", "tmp.txt", "000102030405060708090a0b0c0d0e0f", b'\xe4\xa7\x0e\xbd\x84\xfa\xf5\xd8`\xb8\xa1\x10\x0b~\xadhJ\xa98\x9f\xceZ\xd4\x9f"\xde\x00\xf6w\xa9\x1b\x05'),
@@ -72,4 +74,29 @@ def test_aes_encrypt_PCBC(data, key, file_name, iv, expected):
7274

7375
os.remove(f"{file_name}.enc")
7476

75-
assert result == expected
77+
assert result == expected
78+
79+
80+
@pytest.mark.parametrize("data,key,file_name,iv,expected", [
81+
# 128 bit
82+
(b'1234567890', "2b7e151628aed2a6abf7158809cf4f3c", "tmp.txt", "000102030405060708090a0b0c0d0e0f", b'a\xccT\xf8\xac[\x05\x8e\xe397\xe9\x9b\xaf\xec`\xd9\xa4\xda\xda\x08\x92#\x9fk\x8b=v\x80\xe1Vr'),
83+
(b'1234567890123456', "2b7e151628aed2a6abf7158809cf4f3c", "tmp1.txt", "000102030405060708090a0b0c0d0e0f", b'a\xccT\xf8\xac[\x05\x8e\xe39\x06\xdb\xa8\x9b\xd9V\xd9\xa4\xda\xda\x08\x92#\x9fk\x8b=v\x80\xe1Vt'),
84+
# 192 bit
85+
(b'1234567890', "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "tmp2.txt", "000102030405060708090a0b0c0d0e0f", b"\x97;\x80\xb9\xc6\x87$\x05\xe4\xcf'\x18\xba\tV^R\xef\x01\xdaR`/\xe0\x97_x\xac\x84\xbf\x8aV"),
86+
(b'1234567890123456', "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", "tmp3.txt", "000102030405060708090a0b0c0d0e0f", b'\x97;\x80\xb9\xc6\x87$\x05\xe4\xcf\x16*\x89=chR\xef\x01\xdaR`/\xe0\x97_x\xac\x84\xbf\x8aP'),
87+
# 256 bit
88+
(b'1234567890', "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "tmp4.txt", "000102030405060708090a0b0c0d0e0f", b'\x86\x8d\ti\xc1\x0f\xbe\xe5\xae\xc0\xfa\x97\xeb\xce/J\xe1\xc6V0^\xd1\xa7\xa6V8\x05to\xe0>\xda'),
89+
(b'1234567890123456', "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "tmp5.txt", "000102030405060708090a0b0c0d0e0f", b'\x86\x8d\ti\xc1\x0f\xbe\xe5\xae\xc0\xcb\xa5\xd8\xfa\x1a|\xe1\xc6V0^\xd1\xa7\xa6V8\x05to\xe0>\xdc')
90+
])
91+
def test_aes_encrypt_OFB(data, key, file_name, iv, expected):
92+
with open(file_name, "wb") as file:
93+
file.write(data)
94+
95+
encrypt(key, file_name, "OFB", iv)
96+
97+
with open(f"{file_name}.enc", "rb") as file:
98+
result = file.read()
99+
100+
os.remove(f"{file_name}.enc")
101+
102+
assert result == expected

tmp/test_2.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
1-
from matplotlib import pyplot as plt
2-
3-
# Set the figure size
4-
plt.rcParams["figure.figsize"] = [5.00, 5.00]
5-
plt.rcParams["figure.autolayout"] = True
6-
7-
8-
# Random data points
9-
#with open("/Users/gabriellindeblad/Documents/GitHub/AES-Python/tmp/test_files/data.txt.enc", "rb") as f:
10-
# data = f.read().splitlines()
11-
# print(data)
12-
# for index, pice in enumerate(data):
13-
# data[index] = [i for i in pice]
14-
151
with open("/Users/gabriellindeblad/Documents/GitHub/AES-Python/tmp/test_files/data.txt.enc", "rb") as f:
162
data = f.read()
17-
data = [i for i in data]
18-
data = [data[x:x+192] for x in range(0, len(data), 192)]
19-
#data = data[:-1]
20-
print(data)
213

224
print(data)
5+
print(len(data))
236

24-
25-
plt.title('adjust axis scale')
26-
27-
# Plot the data using imshow with gray colormap & aspect set to auto
28-
plt.imshow(data, interpolation=None, cmap="gray", aspect="auto")
29-
30-
# Display the plot
31-
plt.show()

tmp/test_3.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from PyAES.AES import ofb_dec, ofb_enc
22

33
def run(val):
4-
key = "2b7e151628aed2a6abf7158809cf4f3c"
4+
key = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"
55
iv = "000102030405060708090a0b0c0d0e0f"
66
file_path = r"/Users/gabriellindeblad/Documents/GitHub/AES-Python/tmp/test_files/data.txt"
77

@@ -12,4 +12,3 @@ def run(val):
1212

1313
if __name__ == "__main__":
1414
run(2)
15-
print("Done!")

0 commit comments

Comments
 (0)