Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- unsigned int find_key(const char* string)
- {
- char* plain_text = (char*)malloc(sizeof(char)*512);
- char* encoded_text = (char*)malloc(sizeof(char)*512);
- if(*string == 0)
- {
- return -1;
- }
- //ottengo plain text
- //<plain_text1>;<encoded_text1>
- //salto il primo carattere e arrivo fino a >
- int index = 1;
- int plain_text_index = 0;
- int encoded_text_index = 0;
- while(string[index] != '>');
- {
- plain_text[plain_text_index] = string[index];
- plain_text_index++;
- index++;
- }
- index += 3; //salto tre caratteri, mi trovavo a '>' e mi sposto ';'-'<'
- while(string[index] != '>')
- {
- encoded_text[encoded_text_index] = string[index];
- encoded_text_index++;
- index++;
- }
- int slices = ( plain_text_index - 1 ) / 4;
- if( encoded_text_index != plain_text_index )
- {
- //il numero di byte e' diverso, cio' credo sia un errore( check this plis )
- //se le stringhe sono di dimensione diversa non dovrebbe essere possibile che uno xor sulla prima string possa portare alla seconda e viceversa
- return -1;//possibile ciclio infinito fino a overflow memoria / riconoscimento stringa chiave incorretta, meglio uscire
- }
- unsigned int* array = (unsigned int*)plain_text;
- unsigned int* encoded_array = (unsigned int*) encoded_text;
- unsigned int* encoded_try_array = (unsigned int*)malloc( sizeof(unsigned int)* 512/4 );
- unsigned int chiave = 1;
- int key_found = 0;
- int wrong_slice_found = 0;
- while( !key_found ){
- key_found = 1;
- for(int i = 0; i < slices; i++)
- {
- encoded_try_array[i] = plain_text[i] ^ chiave;
- }
- for(int i = 0; i < slices; i++)
- {
- if(encoded_try_array[i] != encoded_array[i])
- {
- key_found = 0;
- break;
- }
- }
- if( !key_found)
- chiave++;
- if(chiave == (unsigned int)-1) return -1;
- }
- return chiave;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement