@@ -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 )
0 commit comments