I need to encrypt and decrypt text, image and video with a program in C with a matrix.

My program is working but it's really slow, how can I improve its speed ?
specially for the decrypt part.

Each matrix have an identity part and a filling part.

For encryption, I only do a matrix multiplication of each hex of ascii I get from files.

There is my code for decryption:

In source_decode I have the name of the file I need to decrypt.

In matrice_identity I have an array that contain the order of the matrix identity hidden in the matrix.
For example for a matrix like:

10001111
11000111
10100100
10010010

The matrice_identity will contain {4,1,2,3}, because the column 4 (where columns are numbered starting from 0) is 1000, the column 1 is 0100, column 2 is 0010, and column 3 is 0001. Other columns are just here to fill.

1 Answer
1

Perform Error Checking as Soon as Possible
The complexity of the code can be decreased by performing earlier error checking in the function decode_it. If the code was changed so that source_file was tested at the beginning there would be less indentation
and the code would be easier to follow:

Don't Assume the Buffer has the Necesary Capacity
There is a possible critical error in the following code:

char* exetention = "d";
strcat(source_decode, exetention);

The variable source_code may not have enough space allocated to append the extention. This can cause the program to crash, especially when the code is optimized. Allocate a new string with enough space to accept the file name and the extension.

The Single Responsibility Principle states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.

Robert C. Martin expresses the principle as follows:

A class should have only one reason to change.

While this is primarily targeted at classes in object oriented languages it applies to functions and subroutines in procedural languages like C as well.

The decode_it() function can be broken up into smaller functions to make it more readable, and to allow you to profile the program
to see where you are spending the most time.

Please note in the previous I cast the result of pow function to int, my compiler gave me a warning message on that line.

Possible Optimizations
Rather than reading the input file character by character input large chunks of data (file block size perferably) into a large
buffer internal to the program and process the buffer instead. Each time fgetc is called it is making a system call which is
expensive(time consuming). Having an input buffer and processing it may help reduce the complexity of the code as well.

For the same reason, create a string output buffer and write it to the file all at once rather than repeatedly calling fprintf().

Using pointers rather than array indexes may also improve performance.