Skip to content

Commit edfbd96

Browse files
committed
OFB implemented partially
1 parent a1a224d commit edfbd96

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

Documentation/Rapport/Introduction.tex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
\mychapter{Introduktion}
22

3-
\section{Inledning}
43
Kryptering, något vi möter varje dag i dagens samhälle men...
54

65
När de vi idag kallar internet uppkom och öppnades för allmänheten så var tankarna på hur

Documentation/Rapport/Main.pdf

740 Bytes
Binary file not shown.
324 Bytes
Binary file not shown.

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+

src/PyAES/AES.py

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

tmp/test_3.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
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 = "2b7e151628aed2a6abf7158809cf4f3c"
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)
15+
print("Done!")

0 commit comments

Comments
 (0)