Code:
/*
Encrypts a file using a permutation cipher.
The permutation is simply ^ with a key [0-255].
*/
#include <stdio.h>
#include <stdlib.h>
void usage(char *);
main(int argc, char *argv[])
{
FILE *encryptedout;
FILE *plainin;
int key;
char *plainfilename, *encryptedfilename;
if (argc != 4) usage(argv[0]);
plainfilename = argv[1];
encryptedfilename = argv[2];
key = atoi(argv[3]);
plainin = fopen(plainfilename,"r");
encryptedout = fopen(encryptedfilename,"w");
int c;
while((c = getc(plainin)) != EOF)
{
// The following line is the encryption algorithm
char ec = (char) (c^key);
putc(ec,encryptedout);
}
close(plainin);
close(encryptedout);
}
void usage(char * commandname)
{
printf("USAGE: %s plaintextfile encryptedfile key\n",commandname);
printf(" Where plaintextfile is the file you wish to encrypt\n");
printf(" and the encrypted version is placed in encryptedfile.\n");
printf(" key takes a value in the range (0-255).\n");
exit(0);
}
As you can see from the code the problem is that i cant come up with some sort of loop which allows me to try 255 keys without the manual brute force method . Ive had a look into the Bash manual but cant seem to find anything specific to what im looking for and unfortunately pipping is too complicated for me to fully understand given my current knowledge of C.
I need to come up with some way to do do 255 keys without having to manually try it , or even if i have to manually type it the there should be some kind of indication showing me that the file contains the unencypted file .. I can cheat around this as I already know what the key for my file is, so if there is some way i can get a detection of a sequence of words within the file then it will know it is unencrypted.
Is there anyway to overcome this problem ?