Skip to content

Instantly share code, notes, and snippets.

@J16N
Created July 27, 2021 13:31
Show Gist options
  • Save J16N/e95d53c5a565382ae117d1dcb8c885d3 to your computer and use it in GitHub Desktop.
Save J16N/e95d53c5a565382ae117d1dcb8c885d3 to your computer and use it in GitHub Desktop.
Encrypt your sensitive files.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Required Prototypes
char *get_file(char *);
int encrypt(char *);
char *renamef(char *, char *);
int main(int argc, char *argv[])
{
printf("\n------- ENCRYPT YOUR FUC***G FILE ------\n");
char *name = get_file("\n File: ");
encrypt(name);
return 0;
}
char *get_file(char *label)
{
// Display the placeholder
printf("%s", label);
int i = 0;
char *name = NULL;
for(int ch; ( ch = getchar() ) != '\n'; ++i)
{
name = realloc(name, (i + 2) * sizeof(char));
name[i] = ch;
}
name[i] = '\0';
//return name/location of the file
return name;
}
int encrypt(char *name)
{
FILE *source = fopen(name, "rb");
if (!source) return -1;
// Seek to the end of file offseting from the beginning
fseek(source, 0, SEEK_END);
// Total size of the file
long int len = ftell(source);
// Rewind to the beginning
rewind(source);
printf("\n Encrypting your fuc***g file. Please wait...\n");
// Rename the output file as <filename>_encrypted.<ext>
name = renamef(name, "encrypted");
FILE *target = fopen(name, "wb");
for (int ch; ( ch = fgetc(source) ) != EOF;)
{
// Show how much the encryption is done
long int curr = ftell(source);
printf("\r %.1lf%% completed", (double) curr / len * 100);
fflush(stdout);
fputc(~ch, target);
}
printf("\n Successfully encrypted the file.\n\n");
// Free the file name
free(name);
// Close all the files
fclose(source);
fclose(target);
return 0;
}
char *renamef(char *name, char *append)
{
int suff_size = strlen(append);
int name_size = strlen(name);
// Increase the size of the original string to add our suffix
name = realloc(name, (name_size + suff_size) * sizeof(char));
// Get index of the period from where we're going to append
int index_of = 0;
while(name[index_of] != '.') index_of++;
// Save extension name to a new string
char *ext = NULL;
int j = 0;
for (int i = index_of + 1; name[i] != '\0'; ++i, ++j)
{
ext = realloc(ext, (j + 2) * sizeof(char));
ext[j] = name[i];
}
ext[j] = '\0';
// Replace the period with _. After that,
// add the suffix to the original string.
name[index_of] = '_';
for (int i = index_of + 1, j = 0; append[j] != '\0'; ++i, ++j)
name[i] = append[j];
// After adding the suffix, put the period in
// desired location and copy the extension name
name[suff_size + index_of + 1] = '.';
for (int i = suff_size + index_of + 2, j = 0; ext[j] != '\0'; ++i, ++j)
name[i] = ext[j];
// Free the new string that stored our
// extension name
free(ext);
return name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment