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