Skip to content

Instantly share code, notes, and snippets.

@lukakostic
Created October 1, 2018 15:16
Show Gist options
  • Save lukakostic/9c1040396e035a173ace567260fae720 to your computer and use it in GitHub Desktop.
Save lukakostic/9c1040396e035a173ace567260fae720 to your computer and use it in GitHub Desktop.
Start args: key mode(encrypt/decrypt) input.txt output.txt OR input.txt output.txt (will encrypt and print the random key used)
#include<stdio.h>
#include<stdlib.h> //rand & srand
#include<time.h> //to seed srand
int main(int argc, char * argv[])
{
FILE * file;
long length; //file length
char * text = 0;
char mode = 'd'; //e = encryption, d = decryption
int key;
char * input;
char * output;
//(location),key,mode,input,output
if (argc == 5) {
sscanf_s(argv[1], "%d", &key);
if (argv[2][0] == 'e' || argv[2][0] == 'E') mode = 'e'; //better than strcmp
input = argv[3];
output = argv[4];
}
//(location),input,output //this is encryption because you cant decrypt without a key anyways. Generates a random key.
else if (argc == 3) {
srand(time(NULL)); key = rand();
mode = 'e';
input = argv[1];
output = argv[2];
}
else {
printf("Wrong number of arguments. Must be either 4 or 2 (KMIO) or (IO)");
return 2;
}
//read input
fopen_s(&file, input, "r");
if (!file) return 1;
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
text = malloc((length + 1) * sizeof(char));
memset(text, 127, length + 1);
if (text) fread(text, 1, length, file);
fclose(file);
//encrypt/decrypt
if (text)
{
srand(key);
for (int i = 0; i < (length + 1); i++) {
int r = rand() % 97; // random
char p = text[i]; //current char
char p1 = 0; //new char
if (mode == 'e') { //Encrypt
if (p == '\t') p = 0;
else if (p == '\n') p = 1;
p1 = (p - 30)%99 + r;
}
else { //Decrypt
p1 = (p + 30 - r);
if (p1 == 0) p1 = '\t';
if (p1 == 1) p1 = '\n';
}
text[i] = p1;
}
//write output
fopen_s(&file, output, "w");
if (!file) return 1;
for (int i = 0; i < (length + 1); i++) {
if (text[i] == 0 || text[i] > 126)break;
fprintf(file, "%c", text[i]);
}
fclose(file);
}
printf("key:%d", key);
//while(1){}
return 0;
}
#include<stdio.h>
#include<stdlib.h> //rand & srand
#include<time.h> //to seed srand
int main(int argc, char * argv[])
{
FILE * file;
long length; //file length
char * text = 0;
char mode = 'd'; //e = encryption, d = decryption
int key;
char * input;
char * output;
//(location),key,mode,input,output
if (argc == 5) {
sscanf_s(argv[1], "%d", &key);
if (argv[2][0] == 'e' || argv[2][0] == 'E') mode = 'e'; //better than strcmp
input = argv[3];
output = argv[4];
}
//(location),input,output //this is encryption because you cant decrypt without a key anyways. Generates a random key.
else if (argc == 3) {
srand(time(NULL)); key = rand();
mode = 'e';
input = argv[1];
output = argv[2];
}
else {
printf("Wrong number of arguments. Must be either 4 or 2 (KMIO) or (IO)");
return 2;
}
//read input
fopen_s(&file, input, "r");
if (!file) return 1;
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
text = malloc((length + 1) * sizeof(char));
memset(text, 127, length + 1);
if (text) fread(text, 1, length, file);
fclose(file);
//encrypt/decrypt
if (text)
{
srand(key);
for (int i = 0; i < (length + 1); i++) {
int r = rand() % 97; // random
char p = text[i]; //current char
char p1 = 0; //new char
if (mode == 'e') { //Encrypt
if (p == '\t') p = 0;
else if (p == '\n') p = 1;
p1 = (p - 30)%99 + r;
}
else { //Decrypt
p1 = (p + 30 - r);
if (p1 == 0) p1 = '\t';
if (p1 == 1) p1 = '\n';
}
text[i] = p1;
}
//write output
fopen_s(&file, output, "w");
if (!file) return 1;
for (int i = 0; i < (length + 1); i++) {
if (text[i] == 0 || text[i] > 126)break;
fprintf(file, "%c", text[i]);
}
fclose(file);
}
printf("key:%d", key);
//while(1){}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment