Demo: xor-based encryption (2006)

If only I had published this earlier I would be rich
This commit is contained in:
kougyokugentou 2021-10-24 16:15:43 -07:00
parent af5f8e9bff
commit e4c8dea6f2

127
demos/enc.c Normal file
View File

@ -0,0 +1,127 @@
/* enc.c Convert a string, each character into binary. Take "key"
* and convert to binary. xor character binary with key binary to make
* the new encrypted character. Key will be obtained through input.
* (c) 2006 Shaun Marquardt.
*/
#include <stdio.h>
#include <stdlib.h> //exit //strtol
#include <conio.h> //clrscr //getche
#include <string.h> //strlen for htoa
#include <ctype.h> //toupper for htoa
#define TRUE 1
#define FALSE !TRUE
void htoa( char *input );
long strtol(const char *s, char **endp, int base); //strtol is in stdlib.h
int main(int argc, char *argv[])
{
char input_str[100];
char key_str[100];
char enc_str[100];
char input_bits;
char key_bits;
char enc_bits[8];
char *bp;
int p = 0;
clrscr();
printf("Enter the string to be encrypted: ");
gets(input_str);
printf("\nDebug: input_str = %s",input_str);
printf("\nEnter Encryption Key: ");
for(p=0; input_str[p] != NULL; p++)
key_str[p] = getche();
printf("\nDebug: key_str = %s",key_str);
printf("\n\n========Encrypt.========\n\n");
sprintf(input_bits, "%ld", strtol(input_str, &bp, 2)); //ascii ->bin
sprintf(key_bits, "%ld", strtol(key_str, &bp, 2));
printf("\nDebug: input_bits = %s\n", input_bits);
printf("Debug: key_bits = %s\n", key_bits);
/* bitwise math-o-rama */
for(p=0; input_bits[p] != NULL; p++)
{
if (input_bits[p] == key_bits[p])
enc_bits[p] = 1;
enc_bits[p] = 0;
}
sprintf(enc_str, "%ld", strtol(enc_bits, &bp, 16)); //bin ->hex
htoa(enc_str);
printf("\nDone. Press the enter key to exit the program.");
getch();
exit(0);
}
/* my stolen htoa (c) me */
void htoa( char *input )
{
unsigned char a;
char hex[5];
char buf[5];
int x = 0, col = 0;
int done;
hex[0] = '\0';
if( (strlen(input) % 2) != 0 )
{
printf("Hex characters must be entered in pairs.\n");
getch();
exit(1);
}
printf("Hex - \"%s\"\n",input);
printf("--== Output ==--\n\n");
printf("\"");
done = FALSE;
while(!done)
{
if(input[x] == '\0') //End of input, done.
{
printf("\"\n");
done = TRUE;
break;
}
if(input[x] == ' ') //Space character? Print out a space.
{
printf("%c", ' ');
x++;
}
sprintf(hex, "%c%c", toupper(input[x]), toupper(input[x+1])); //Get Hex Char
x+=2;
for(a=32;a<128;a++)
{ //Go through list of characters
sprintf(buf, "%X",a);
if(!strcmp(buf, hex)) //Matching, print character
printf("%c",a);
else //Not Matching, continue
continue;
col++;
if(col >= 79)
{
col = 0;
printf("\n\r");
}
break;
} //end "a" for loop
} //end while
return;
} //end htoa