Help with C Xor encrypting

All I know that is Im suppose to turn both keys and the string into raw data
and xor them

The return of unhex is the same for some reason for string and hex
thats why Im getting my encrypted text as 000000....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

char* unHex ( char* hexString );

int main(int argc, char *argv[]) {
	
	char clearTextMessages[100] = "Ayy Lmao You decrypted this good job mang.";
	char hexKey[200] = "1bee38739e57cc6da711046fa493ef1a79b3e0d0383da453eedfd51a1cacf0669127a69fefbc9b63748e";
	char hexCipherText[200];
	char cipherTextChar[200];
	int stringLength;
	int counter;
	char *hex;
	char *string;
	char hexciphers[200];
	//printf("Enter a string to encrypt (less than 100 char)\n");
	//fgets(clearTextMessages, 99, stdin);
	//clearTextMessages[stringLength-1] = '\0'; 
	stringLength = strlen(clearTextMessages);



	 for(counter = 0; counter < stringLength; counter++)
	 {
		sprintf(&hexCipherText[counter*2], "%02x", (unsigned char) clearTextMessages[counter]);
	 }
	
	string = unHex(hexCipherText);
	hex = unHex(hexKey);
	

	 for(counter = 0; counter < stringLength*2; counter++)
{
	hexciphers[counter] = string[counter] ^ hex[counter];
}

	 for(counter = 0; counter < stringLength; counter++)
	 {
		sprintf(&cipherTextChar[counter*2], "%02x", (unsigned char) hexciphers[counter]);
	 }
	 printf("\n\n%s", cipherTextChar);



/*
FILE *fp;

fp = fopen("CipherAndKey.txt","w");

fprintf(fp,"The key is %s\n\n", hexKey);

fprintf(fp,"The Message is %s\n\n", cipherTextChar);



fclose(fp);
*/

	
	return 0;
}

char* unHex ( char* hexString )
{
    static unsigned char rawText [ 100 ] = { 0 } ;
    unsigned int tmp = 0 ;
    int i ;
    for ( i = 0 ; i < strlen ( hexString ) / 2 ; i++ )
    {
        sscanf ( &hexString[2*i], "%02x", &tmp ) ;
        rawText[i] = ( unsigned char ) ( tmp & 0x000000ff ) ;
    }
    
    return rawText ;
}



so i changed my unhex function I still have problem my string is not a raw data

char* unHex ( char* hexString )
{
char *rawText;
rawText = malloc(500 * sizeof(char));
unsigned int tmp = 0 ;
int i ;
for ( i = 0 ; i < strlen ( hexString ) / 2 ; i++ )
{
sscanf ( &hexString[2*i], "%02x", &tmp ) ;
rawText[i] = ( unsigned char ) ( tmp & 0x000000ff ) ;
}
rawText[i] = '\0';
return rawText ;
}
Topic archived. No new replies allowed.