Created
May 8, 2015 05:16
-
-
Save forrestbthomas/61b0482923fddf952ccf to your computer and use it in GitHub Desktop.
Fixed XOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <math.h> | |
char convert_int_to_hex(int arg) | |
{ | |
char result; | |
switch(arg) | |
{ | |
case 0: result = '0'; break; | |
case 1: result = '1'; break; | |
case 2: result = '2'; break; | |
case 3: result = '3'; break; | |
case 4: result = '4'; break; | |
case 5: result = '5'; break; | |
case 6: result = '6'; break; | |
case 7: result = '7'; break; | |
case 8: result = '8'; break; | |
case 9: result = '9'; break; | |
case 10: result = 'a'; break; | |
case 11: result = 'b'; break; | |
case 12: result = 'c'; break; | |
case 13: result = 'd'; break; | |
case 14: result = 'e'; break; | |
case 15: result = 'f'; break; | |
} | |
return result; | |
} | |
char convert_bin_to_int(char arg[]) | |
{ | |
unsigned long arg_len = strlen(arg) - 1; | |
int i; | |
int j; | |
int result = 0; | |
char final[512]; | |
for (i = 0; i <= arg_len; i++) | |
{ | |
if (arg[i] == 49) | |
{ | |
result += pow(2, (arg_len - (i))); | |
} | |
} | |
return convert_int_to_hex(result); | |
} | |
void binary_to_hex(char* xor_string) | |
{ | |
char four_bit[5]; | |
char final_result[256]; | |
unsigned long xor_string_len = strlen(xor_string); | |
int i = 0; | |
int j = 0; | |
int k = 0; | |
for (i = 0; i <= xor_string_len; i++) { | |
if (j == 4) | |
{ | |
final_result[k] = convert_bin_to_int(four_bit); | |
k++; | |
j = 0; | |
} | |
four_bit[j] = xor_string[i]; | |
j++; | |
} | |
final_result[xor_string_len] = '\0'; | |
printf("%s\n", final_result); | |
} | |
void binary_xor(char first[], char second[]) | |
{ | |
unsigned long arg_len = strlen(first) - 1; | |
char xor_result[512]; | |
int i; | |
for (i = 0; i <= arg_len; i++) | |
{ | |
if (first[i] != second[i]) | |
{ | |
xor_result[i] = '1'; | |
} | |
else | |
{ | |
xor_result[i] = '0'; | |
} | |
} | |
binary_to_hex(xor_result); | |
} | |
char* convert_to_binary(char arg) | |
{ | |
char* result; | |
switch(arg) | |
{ | |
case '0': result = "0000"; break; | |
case '1': result = "0001"; break; | |
case '2': result = "0010"; break; | |
case '3': result = "0011"; break; | |
case '4': result = "0100"; break; | |
case '5': result = "0101"; break; | |
case '6': result = "0110"; break; | |
case '7': result = "0111"; break; | |
case '8': result = "1000"; break; | |
case '9': result = "1001"; break; | |
case 'A': result = "1010"; break; | |
case 'B': result = "1011"; break; | |
case 'C': result = "1100"; break; | |
case 'D': result = "1101"; break; | |
case 'E': result = "1110"; break; | |
case 'F': result = "1111"; break; | |
case 'a': result = "1010"; break; | |
case 'b': result = "1011"; break; | |
case 'c': result = "1100"; break; | |
case 'd': result = "1101"; break; | |
case 'e': result = "1110"; break; | |
case 'f': result = "1111"; break; | |
} | |
return result; | |
} | |
char* convert_hex(char arg[], char* result) | |
{ | |
int i = 0; | |
for (i = 0; i < strlen(arg); i++) | |
{ | |
if (i == 0) | |
{ | |
strcpy(result, convert_to_binary(arg[i])); | |
} | |
else | |
{ | |
strcat(result, convert_to_binary(arg[i])); | |
} | |
} | |
return result; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char result_one[512]; | |
char result_two[512]; | |
char* string_one = convert_hex(argv[1], result_one); | |
char* string_two = convert_hex(argv[2], result_two); | |
binary_xor(string_one, string_two); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment