Skip to content

Instantly share code, notes, and snippets.

@farooqkz
Last active December 13, 2017 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farooqkz/5aa0c96f526b46e1da6da4f220f5bb86 to your computer and use it in GitHub Desktop.
Save farooqkz/5aa0c96f526b46e1da6da4f220f5bb86 to your computer and use it in GitHub Desktop.
Something like Vigenere cipher but for bytes instead of letters
/*
Vigenere File Coder and Decoder
Copyright (C) 2017 Farooq Karimi Zadeh <farooghkarimizadeh@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned char encrypt_chr(unsigned char input, unsigned char key){
return input + key; // it does not matter if it performed an overflow!
}
unsigned char decrypt_chr(unsigned char input, unsigned char key){
return input - key; // the same about this!
}
unsigned char crypt_chr(unsigned char input, unsigned char key, char mode){
return mode == 'd'? decrypt_chr(input, key) : encrypt_chr(input, key);
}
void show_help(char *progname){
fprintf(stderr, "\nVigenere Encoder and Decoder \nCopyright (C) 2017 "
"Farooq Karimi Zadeh <farooghkarimizadeh@gmaul.com>\n"
"This program comes with ABSOLUTELY NO WARRANTY; "
"This is free software, and you\n are welcome to redistribute it"
" under certain conditions. For more information\n see MIT"
" License\n\n"
"Usage: %s [-d|-e] [-i <input_file>] [-o <output_file>] [-k"
" <key>]\n\tBy default it ecnodes the input file and <key> is"
" \"key\". Maximum \n\tlength of <key> is 128 bytes\n\tdefault "
"input and output files are -(which refers to"
" STDIN and STDOUT \n\tfor input and output file.)\n"
"Compiled at %s %s\n",
progname, __DATE__, __TIME__);
exit(0);
}
int main(int argc, char *argv[]){
// these variables should be extracted from command line arguments
char key[128 + 1] = "key";
key[128] = '\0';
char in_file_path[FILENAME_MAX + 1] = "-";
char out_file_path[FILENAME_MAX + 1] = "-";
char mode = 'e'; //d = decrypt, e = encrypt
for (int i = 0; i < argc; i++){
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-?"))
show_help(argv[0]);
if (!strcmp(argv[i], "-d"))
mode = 'd';
if (!strcmp(argv[i], "-e"))
mode = 'e';
if (!strcmp(argv[i], "-i"))
strcpy(in_file_path, argv[i + 1]);
if (!strcmp(argv[i], "-o"))
strcpy(out_file_path, argv[i + 1]);
if (!strcmp(argv[i], "-k"))
strcpy(key, argv[i + 1]);
}
FILE *in_file;
FILE *out_file;
if (!strcmp(in_file_path, "-"))
in_file = stdin;
else
in_file = fopen(in_file_path, "r");
if (!in_file){
fprintf(stderr, "Could not open input file: %s\n", in_file_path);
return -1;
}
if (!strcmp(out_file_path, "-"))
out_file = stdout;
else
out_file = fopen(out_file_path, "w");
if (!out_file){
fprintf(stderr, "Could not open output file: %s\n", out_file_path);
}
char *kp;
kp = key; // it starts with the first character of the key and when it
// reaches the last character of key(not the NULL character) it
// again goes to the first character.
int key_len = strlen(key);
fseek(in_file, 0, SEEK_END);
unsigned size = ftell(in_file);
fseek(in_file, 0, SEEK_SET);
while (size--){
fputc(crypt_chr(fgetc(in_file), *kp, mode), out_file);
kp = (char) (kp - key) == key_len? key : ++kp; // a little complex, no?
// ^ if the difference between kp and key is key_len, this means that
// the kp pointer is on the last character, so key will be assigned to
// kp and moves it to the first character, if it's not pointing to the
// last character then just add one to it(points to the next character)
}
fclose(in_file);
fclose(out_file);
return 0;
}
@farooqkz
Copy link
Author

Here's Wikipedia article about vigenere cipher.

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