XOR Encrypt Shellcode

XOR encryption

C Code to XOR encrypt contents of a file and output in "C" format.

#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include<string.h>

void XOR(char * data, size_t data_len, char * key, size_t key_len) {
	int j;
	j = 0;
	for (int i = 0; i < data_len; i++) {
		if (j == key_len - 1) j = 0;

		data[i] = data[i] ^ key[j];
		j++;
	}
}

int main(int argc, char **argv)
{	
	if(argc < 2)
	{
		printf("Format: ./xor.exe filename");
		exit(0);
	}
	
	FILE* fp;
	fp = fopen(argv[1], "r");
	
	if (!fp)
	{
		printf("File does not exist\n");
		exit(0);
	}
	
	int i =0;
	// Enter the key for encryption
	char key[]="SuperSecretKey";
	
	// Seek file pointer to the end and calc file size
	fseek(fp, 0, SEEK_END);
   	int len = ftell(fp);
   	//Move the file pointer back to the beginning
   	rewind(fp);
	unsigned char data[len]={};
	char ch=0;
	
	do {
        ch = fgetc(fp);
        data[i]=ch;
        i++;
    } while (ch != EOF);
    
	//printf("\nCopied data:\n%s\n", data);

	XOR((char *) data, sizeof data, key, sizeof key);
	printf("XORed data:\n%s\n", data);
	
	//Print in Hex format
	printf("{");
	for(i=0;i<sizeof data; i++)
	{
		printf("0x%02X", data[i]);
		if(i>0 && i!=sizeof data-1)
		{
			printf(", ");
		}
		if(i==sizeof data-1)
		{
			printf("};");
		}
	}
    
	//Decrypting
	//XOR((char* )data, sizeof data, key, sizeof key);
	//printf("%s\n", data);
	fclose(fp);
    return 0;
}

Python code to XOR encrypt content of a file and output in "C" format.

XOR Decryption & Execution

Last updated