Skip to content

SharGIT-prog/Encoding-Decoding

Repository files navigation

Encoding-Decoding

Project in C to encode and decode strings in C in order to protect the data.

File Encryption (Substitution): This implements a simple substitution cipher for encrypting and decrypting text files. It uses a key file that defines custom character mappings, which determine how characters are replaced during encryption and how they are restored during decryption.

The encryptFile function begins by opening and parsing the key file to load the original characters and their corresponding substitutions. It also checks for errors and handles invalid files. Then, it opens the input and output files safely, again handling any errors that might occur. The input file is read line by line. For each character, the function writes newlines directly, looks up the character in the loaded keys, and writes the corresponding substitution to the output file. If a character is not found in the mapping, a ? is written instead. After processing, all files are closed, the content of the output file is displayed, and any allocated memory is freed.

The decryptFile function also starts by opening and parsing the key file, handling errors and invalid cases. Next, it opens the input and output files safely. The entire content of the input file is read into a buffer, and a timeout timer is started. The function processes the buffer character by character, while checking for timeouts. Newlines are written directly. For encrypted patterns, it tries to find matches in the loaded keys; if a match is found, it writes the original character and moves forward in the buffer. If no match is found, it skips the character and continues. Once done, the output file is closed. If a timeout occurs, the output file is removed. If the keys are incorrect (i.e., no matches were found), the output file is also removed. For partial decryption, unknown patterns are simply skipped. If decryption is successful, the function reopens the output file, prints its content, and frees memory.

About

Project in C to encode and decode strings in C in order to protect the data.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages