Skip to content

Commit 895313b

Browse files
authored
Merge pull request #50 from Glindeb/dev
Dev
2 parents 8c3bc1f + cd4a148 commit 895313b

File tree

16 files changed

+187
-45
lines changed

16 files changed

+187
-45
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
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
\mychapter{Introduktion}
22

3-
\section{Inledning}
4-
abstract
3+
Kryptering, något vi möter varje dag i dagens samhälle men...
4+
5+
När de vi idag kallar internet uppkom och öppnades för allmänheten så var tankarna på hur
6+
våran data och information
7+
8+
Hela tiden runt om oss skickas information digitalt från enhet till enhet. Information från
9+
höger till väster bara flödar ...
10+
11+
Information, en grundsten i dagens samhälle ...
12+
513
\section{Syfte}
6-
abstract
7-
\section{Frågeställning}
8-
abstract
14+
Syftet med denna undersökning är att utveckla förståelsen för kryptering och krypterings
15+
algoritmer
16+
17+
18+
\section{Frågeställningar}
19+
Hur påverkas tiden de tar att kryptera en mängd data mellan de olika nyckel längderna 128-bit,
20+
192-bit och 256-bit nyckel?
21+
22+
Hur påverkas
23+
24+
Vad är skillnaden i tiden de tar för att kryptera något mellan att köra algoritmen i ECB, CBC
25+
[och något mer], Samt vilken betydelse ur ett användnings perspektiv får de?

Documentation/Rapport/Main.pdf

1.19 KB
Binary file not shown.

Documentation/Rapport/Main.synctex.gz

1.94 KB
Binary file not shown.

Documentation/Rapport/Main.tex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@
114114
\vspace{3.5cm}
115115
{\huge\bfseries The AES encryption algorithm\par}
116116
\vspace{0.1cm}
117-
~{\large En undersökning av The Advanced Encryption Standard (AES) \par
118-
~ur ett struktur och användnings perspektiv}
117+
~{\large En undersökning av The Advanced Encryption Standard (AES)}
119118

120119
% Bottom of the page
121120
\vfill

Documentation/Rapport/Ref.bib

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ @misc{wikipedia_2022
55
publisher = {Wikimedia Foundation},
66
year = {2022},
77
month = {Jun}
8-
}
8+
}
9+

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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,85 @@ def pcbc_dec(key, file_path, iv, terminal_width=80):
585585
progress = progress_bar(progress, file_size, terminal_width)
586586
progress = progress_bar(progress, file_size, terminal_width)
587587
remove(file_path)
588+
589+
590+
# OFB encryption function (Tested but no automated test implemented and no test with different file sizes)
591+
def ofb_enc(key, file_path, iv, terminal_width=80):
592+
file_size = getsize(file_path)
593+
progress = 0
594+
progress = progress_bar(progress, file_size, terminal_width)
595+
mix = [int(iv[i:i+2], 16) for i in range(0, len(iv), 2)]
596+
iv = mix
597+
598+
with open(f"{file_path}.enc", 'wb') as output, open(file_path, 'rb') as data:
599+
for i in range(int(file_size/16)):
600+
raw = [i for i in data.read(16)]
601+
mix = encryption_rounds(mix, key)
602+
result = xor(raw, mix)
603+
output.write(bytes(result))
604+
progress = progress_bar(progress, file_size, terminal_width)
605+
606+
if file_size % 16 != 0:
607+
raw = [i for i in data.read()]
608+
raw, length = add_padding(raw)
609+
610+
if file_size < 16:
611+
mix = encryption_rounds(iv, key)
612+
else:
613+
mix = encryption_rounds(mix, key)
614+
result = xor(mix, raw)
615+
616+
mix = encryption_rounds(mix, key)
617+
identifier = xor(([0 for i in range(15)] + [length]), mix)
618+
619+
output.write(bytes(result + identifier))
620+
progress = progress_bar(progress, file_size, terminal_width)
621+
else:
622+
mix = encryption_rounds(mix, key)
623+
identifier = xor([0 for i in range(16)], mix)
624+
output.write(bytes(identifier))
625+
progress = progress_bar(progress, file_size, terminal_width)
626+
progress = progress_bar(progress, file_size, terminal_width)
627+
remove(file_path)
628+
629+
630+
# OFB decryption function (Tested but no automated test implemented and no test with different file sizes)
631+
def ofb_dec(key, file_path, iv, terminal_width=80):
632+
iv = [int(iv[i:i+2], 16) for i in range(0, len(iv), 2)]
633+
file_size = getsize(file_path)
634+
progress = 0
635+
progress = progress_bar(progress, file_size, terminal_width)
636+
file_name = file_path[:-4]
637+
638+
with open(f"{file_name}", 'wb') as output, open(file_path, 'rb') as data:
639+
if int(file_size/16) - 3 >= 0:
640+
raw = [i for i in data.read(16)]
641+
mix = encryption_rounds(iv, key)
642+
result = xor(raw, mix)
643+
output.write(bytes(result))
644+
progress = progress_bar(progress, file_size, terminal_width)
645+
646+
for i in range(int(file_size/16) - 3):
647+
raw = [i for i in data.read(16)]
648+
mix = encryption_rounds(mix, key)
649+
result = xor(raw, mix)
650+
output.write(bytes(result))
651+
progress = progress_bar(progress, file_size, terminal_width)
652+
else:
653+
mix = iv
654+
655+
data_pice = [i for i in data.read(16)]
656+
identifier = [i for i in data.read()]
657+
658+
mix = encryption_rounds(mix, key)
659+
data_pice = xor(data_pice, mix)
660+
661+
mix = encryption_rounds(mix, key)
662+
identifier = xor(identifier, mix)
663+
664+
result = bytes(remove_padding(data_pice, identifier))
665+
666+
output.write(result)
667+
progress = progress_bar(progress, file_size, terminal_width)
668+
progress = progress_bar(progress, file_size, terminal_width)
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__ = '[email protected]'
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: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
with open(r".\tmp\test_files\data.txt.enc", "rb") as p:
2-
t = p.read()
3-
print(t)
1+
from PyAES.AES import ofb_dec, ofb_enc
42

5-
#with open(r"C:\Users\Gabriel\Documents\GitHub\AES-Python\tmp\test_files\data.txt.enc", "wb") as p:
6-
# p.write(b'2 ?\xebm\xf5o\xc2\x8b\x90\x80\x84 D\xc4\x95\x89\x18\n\xeb\xac\xde\xa7P>Ei\xbc|\x9c\xfa\xf2')
3+
def run(val):
4+
key = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"
5+
iv = "000102030405060708090a0b0c0d0e0f"
6+
file_path = r"/Users/gabriellindeblad/Documents/GitHub/AES-Python/tmp/test_files/data.txt"
7+
8+
if val == 1:
9+
ofb_enc(key, file_path, iv)
10+
elif val == 2:
11+
ofb_dec(key, f"{file_path}.enc", iv)
12+
13+
if __name__ == "__main__":
14+
run(2)

tmp/test_files/data.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1234567890
1+
1234567890123456

0 commit comments

Comments
 (0)