Skip to content

Instantly share code, notes, and snippets.

@4anonz
Created August 4, 2021 00:39
Show Gist options
  • Save 4anonz/fd077004fb104ea8992350cae3bee708 to your computer and use it in GitHub Desktop.
Save 4anonz/fd077004fb104ea8992350cae3bee708 to your computer and use it in GitHub Desktop.
Simple C Program for Encrypting/Decrypting Basic Files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define MAXIN 250
void encrypt(char *, int);
void decrypt(char *, int);
void prompt(const char*, char*);
int main() {
printf("\n++++[1]> Encrypt ++++\n");
printf("++++[2]> Decrypt ++++\n");
printf("++++[0]> exit ++++\n\n");
char choice[MAXIN];
prompt("++[>]Enter choice: ", choice);
if(atoi(choice) != 0 && atoi(choice) != 1 && atoi(choice) != 2) {
printf("Invalid option\n");
exit(1);
}
if(atoi(choice) == 0)
exit(0);
char input_file[MAXIN], chkey[MAXIN];
int key;
//Get the target file path for encryption/decrytion
prompt("++[>]File path: ", input_file);
//Use a key
prompt("++[>]Key: ", chkey);
//convert the key character to an int value;
key = atoi(chkey);
if(!key) {
printf("Encryption/Decrytion key must be an integer value!\n");
exit(1);
}
if(atoi(choice) == 1)
encrypt(input_file, key);
else
decrypt(input_file, key);
return 0;
}
void prompt(const char *prompt, char *buffer) {
printf("%s", prompt);
buffer[0] = 0;
fgets(buffer, MAXIN, stdin);
const int isize = strlen(buffer);
if(isize > 0)
buffer[isize - 1] = 0;
}
void encrypt(char *filename, int key) {
FILE *target, *output;
char output_file[MAXIN];
prompt("++[>]Enter output filename: ", output_file);
target = fopen(filename, "r");
if(target == NULL) {
fprintf(stderr, "Error reading file %s (%s)\n", filename, strerror(errno));
exit(1);
}
output = fopen(output_file, "w");
if(output == NULL) {
fprintf(stderr, "Error creating/opening file %s (%s)\n", output_file, strerror(errno));
exit(1);
}
printf("++Encrypting file....\n");
char ch;
//Read the file char by char as long as it does reach to the end
ch = fgetc(target);
while(ch != EOF) {
char tmp;
tmp = ch + key;
fprintf(output, "%c", tmp);
printf("%c", ch);
ch = fgetc(target);
}
printf("++Encrption complete [✔]");
fclose(target);
fclose(output);
}
void decrypt(char *filename, int key) {
FILE *target, *output;
char output_file[MAXIN];
prompt("++[>]Enter output filename: ", output_file);
target = fopen(filename, "r");
if(target == NULL) {
fprintf(stderr, "Error reading file %s (%s)\n", filename, strerror(errno));
exit(1);
}
output = fopen(output_file, "w");
if(output == NULL) {
fprintf(stderr, "Error creating/opening file %s (%s)\n", output_file, strerror(errno));
exit(1);
}
printf("++Decrypting file....\n");
char ch;
//Read the file char by char as long as it does reach to the end
ch = fgetc(target);
while(ch != EOF) {
char tmp;
memset(&tmp, 0, sizeof(tmp));
tmp = ch - key;
fprintf(output, "%c", tmp);
ch = fgetc(target);
}
printf("++Decrption complete [✔]");
fclose(target);
fclose(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment