Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created July 9, 2019 20:10
Show Gist options
  • Select an option

  • Save DreamVB/799c41da2d77ea1b3a1b27e75e317f00 to your computer and use it in GitHub Desktop.

Select an option

Save DreamVB/799c41da2d77ea1b3a1b27e75e317f00 to your computer and use it in GitHub Desktop.
Good File Encryption With Password.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
FILE *infile;
FILE *outfile;
char ch = '\0';
char newch = '\0';
int x = 0;
int pwsMask = 0;
int Magic = 1163;
int r = 0;
if(argc != 5){
printf("The syntax of the command is incorrect.\n");
return 0;
}
else{
if(argv[3][1] != 'p'){
printf("You need to include a password.\n");
return 0;
}
//Start reading input file name.
infile = fopen(argv[1],"rb");
if(infile == NULL){
printf("Unable to open input file name.\n\%s\n",argv[1]);
return 0;
}
//See if we can write output file
outfile = fopen(argv[2],"wb");
if(outfile == NULL){
printf("Unable to write output file name.\n\%s\n",argv[2]);
return 0;
}
//Build a sum of all the chars in the password.
while(x < strlen(argv[4])){
pwsMask+= (int)argv[4][x];
x++;
}
//Mask pwsMask with Magic and multiply by the length of the password.
pwsMask = (pwsMask ^ Magic) * strlen(argv[4]);
//Reset x
x = 0;
//Start reading and encoding the file data.
while(!feof(infile)){
//Read input char
ch = fgetc(infile);
//If not at end of the input file start encrypting.
if(!feof(infile)){
//Get random number from pwsMask and MOD by 255
r = rand() * pwsMask % 0xff;
//XOR ch with r
newch = (ch ^ r);
//Write to the output file name.
fputc(newch,outfile);
//INC x
x++;
}
}
//Close both file handles
fclose(outfile);
fclose(infile);
}
return 0;
}
@DreamVB
Copy link
Copy Markdown
Author

DreamVB commented Jul 9, 2019

To use this code use

To Encrypt File
bcrypt test.txt test.crypt -p idkfa123

To Decrypt File
bcrypt test.crypt dump.txt -p idkfa123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment