Last active
August 6, 2023 16:52
-
-
Save felix-kyun/bc41ef144c30def262935d857cdaad37 to your computer and use it in GitHub Desktop.
lcrypt.lua ported to c, simple no nonsense super fast file encryption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* INFO: | |
| * made by Felix | |
| * original source lcrypt.lua -> | |
| * `https://gist.github.com/Felix-Kyun/5c9cb2eb5a40289825cb2705bd1f1654` | |
| * @arg -> 0:@mode{e, d} 1:@file_name[i] 2:@file_name[o] 3:@pass | |
| * example:", | |
| * `$ lcrypt e file.txt encrypted_file.txt felix@crypt`", | |
| * `$ lcrypt d encrypted_file.txt file.txt felix@crypt`", | |
| * @mode <- only 'e' or 'd' meaning encrypt and decrypt respectivly", | |
| * @pass has to be supplied with no space"}; | |
| */ | |
| // #include "misc.h" | |
| #include <stddef.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #define MAX_ARG_LEN 5 | |
| void show_help(char *err_msg); // just shows a help message and exits | |
| int ascii_push(int i, int j); // used in encrypt() for offsetting bytes | |
| void encrypt(FILE *input_file, FILE *output_file, char *pass, char mode); | |
| FILE *input_file, *output_file; | |
| int main(int argc, char *argv[]) { | |
| printf("argc: %d", argc); | |
| if (argc != MAX_ARG_LEN) | |
| show_help("Takes Exactly 4 Arguments"); | |
| input_file = fopen(argv[2], "r"); | |
| output_file = fopen(argv[3], "w"); | |
| rewind(input_file); | |
| if (input_file && output_file) { | |
| encrypt(input_file, output_file, argv[4], (char)argv[1][0]); | |
| } else | |
| show_help("Unable To Open Files"); | |
| fclose(input_file); | |
| fclose(output_file); | |
| } | |
| void show_help(char *err_msg) { | |
| char *help_txt[] = { | |
| "LCRYPT", | |
| "Made By Felix", | |
| "@arg -> 0:@mode{e, d} 1:@file_name[i] 2:@file_name[o] 3:@pass", | |
| "example:", | |
| "`$ lcrypt e file.txt encrypted_file.txt felix@crypt`", | |
| "`$ lcrypt d encrypted_file.txt file.txt felix@crypt`", | |
| "@mode <- only 'e' or 'd' meaning encrypt and decrypt respectivly", | |
| "@pass has to be supplied with no space"}; | |
| size_t len = sizeof(help_txt) / sizeof(help_txt[0]); | |
| for (int i = 0; i < len; i++) { | |
| puts(help_txt[i]); | |
| } | |
| puts(err_msg); | |
| exit(69); | |
| if (input_file) | |
| fclose(input_file); | |
| if (output_file) | |
| fclose(output_file); | |
| } | |
| int ascii_push(int i, int j) { | |
| int k = i + j; | |
| if (k >= 0 && k <= 255) | |
| return k; | |
| else if (k < 0) | |
| return 256 + k; | |
| else | |
| return k - 256; | |
| } | |
| void encrypt(FILE *input_file, FILE *output_file, char *pass, char mode) { | |
| char fch, ch = fgetc(input_file); | |
| size_t pass_len = strlen(pass); | |
| unsigned long int pos = 0; | |
| int modifier, tmp = 0; | |
| if (mode == 'e') | |
| modifier = 1; | |
| else if (mode == 'd') | |
| modifier = -1; | |
| else | |
| show_help("invalid call to encrypt func in lcrpy.c"); | |
| while (!feof(input_file)) { | |
| tmp = pos % pass_len; | |
| fch = ascii_push(ch, modifier * pass[tmp]); | |
| putc(fch, output_file); | |
| ch = fgetc(input_file); | |
| modifier *= -1; | |
| pos++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment