Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: aurquiel en 4 Agosto 2017, 03:29 am



Título: Tratar de descifrar texto XOR
Publicado por: aurquiel en 4 Agosto 2017, 03:29 am
Hola me he topado con un desafio para descifrar una cadena de texto, el cual es el siguiente:

Challenge 'Didactic XOR Cipher 3' [Crypto]
Here is a string of bytes encoded in hex:
31cf55aa0c91fb6fcb33f34793fe00c72ebc4c88fd57dc6ba71e71b759d83588
This sequence has been encrypted with a cipher that works as follows. The first byte has been XOR'd with a byte we'll call 'b'. There is a another component to the key, a byte we'll call 'x'. Each time a byte gets XOR'd by b, the following change is executed:
b = (b + x) % 256
In other words, the cipher byte changes with each character encrypted.

Bien me he puesto a programar una rutina que vaya probando las soluciones y que solo me imprima caracteres ascii pertenecientes a texto, pero en la salida no he podido ver texto coherente hast ahora.

Si b y x pueden tener 256 valores cada uno, entonces la posibilidad de combinaciones es de 65536 combinaciones posibles. Voy tomando cada representacion de 8 Bytes de la cadena de texto y probando haciendoles XOR a ver si caen dentro del campo de letras del código ascii y mandandolos a imprimir

He aqui la rutina que he programado:

Código
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.  const char* string = "31cf55aa0c91fb6fcb33f34793fe00c72ebc4c88fd57dc6ba71e71b759d83588";
  8.  
  9.  char* myByte = (char*)malloc(3); //allocate memory for the bye to read.
  10.  
  11.   for(int b=0; b<256; b++)
  12.  {
  13.    for(int x=0; x<256; x++)
  14.    {
  15.  
  16.      int bTest = b; //saving the value of b for not chaging inside the other for cicle :)
  17.  
  18.      for(int i=0; i<strlen(string); i+=2)
  19.      {
  20.  
  21. strncpy(myByte,string+i,2);
  22.  
  23. //Tranform to hex number
  24. char *end;
  25. unsigned long int number = strtoul(myByte,&end,16);
  26.  
  27. if ( ( (bTest^number)<64 && ( (bTest^number)!=32)  ) || (bTest^number)>122 )
  28.  break;
  29.  
  30. printf  ("%c",(char)(bTest^number));
  31.  
  32. bTest = (bTest + x) % 256;
  33.  
  34.      } //End of myByte
  35.  
  36.    } //End of x
  37.  
  38.  } //End of b
  39.  
  40.  free(myByte); //Free memory allocation
  41.  
  42.  return 0;
  43. }

Solo obtengo pura basura a la salida caractres ascii osea texto pero sin ningun sentido.


Título: Re: Tratar de descifrar texto XOR
Publicado por: engel lex en 4 Agosto 2017, 03:40 am
recomendación... solo imprime si cada char del resultado es mayor o igual que '0' y menor o igual que 'z'

el error creo que setá en la conversión de hex a numero


Título: Re: Tratar de descifrar texto XOR
Publicado por: aurquiel en 4 Agosto 2017, 03:52 am
Si eres curioso correlo  ;D ;D ;D

Gracias por la ayuda amigos

Código
  1.    #include <stdio.h>
  2.    #include <string.h>
  3.    #include <stdlib.h>
  4.  
  5.    int main(int argc, char* argv[])
  6.    {
  7.      const char* string = "31cf55aa0c91fb6fcb33f34793fe00c72ebc4c88fd57dc6ba71e71b759d83588";
  8.  
  9.      char* myByte = (char*)malloc(3); //allocate memory for the bye to read.
  10.  
  11.       for(int b=0; b<256; b++)
  12.      {
  13.        for(int x=0; x<256; x++)
  14.        {
  15.  
  16.          int bTest = b; //saving the value of b for not chaging inside the other for cicle :)
  17.  char* myTestResult = (char*)malloc(strlen(string));
  18.  char flag = 0;
  19.  
  20.          for(int i=0; i<strlen(string); i+=2)
  21.          {
  22.  
  23.    strncpy(myByte,string+i,2);
  24.  
  25.    //Tranform to hex number
  26.    char *end;
  27.    unsigned long int number = strtoul(myByte,&end,16);
  28.  
  29.    if ( (bTest^number)<32 || (bTest^number)>122 )
  30.    {
  31.      flag = 1;
  32.      free(myTestResult);
  33.      break;
  34.    }
  35.    else
  36.    {
  37.      *(myTestResult+strlen(myTestResult)) = bTest^number;
  38.      *(myTestResult+strlen(myTestResult)+1) = '\0';
  39.    }
  40.  
  41.    bTest = (bTest + x) % 256;
  42.  
  43.          } //End of myByte
  44.  
  45.  if (!flag)
  46.  {
  47.    printf("%s\n",myTestResult);
  48.    free(myTestResult);
  49.  }
  50.  else
  51.  {
  52.    flag = 0;
  53.  }
  54.  
  55.        } //End of x
  56.  
  57.      } //End of b
  58.  
  59.      free(myByte); //Free memory allocation
  60.  
  61.      return 0;
  62.    }