title: argv to get hex data in order to decrypt. String to unsigned char
body: <p>I'm coding a program which should get a key and an encrypted data ( unsigned char HEXs) in order to return the decrypted info.
  My code is the following:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include "aes.h"
  int main(int argc, unsigned char * argv[])
  {
      int i=1;
      //unsigned char ptext[16] = "Attack at down!";
      unsigned char ptext[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x03,0x15,0x02,0x01,0x00,0x19};
      //    unsigned char ctext[16]={0x9b,0x54,0xd4,0x9e,0xdc,0x96,0x69,0x30,0xf7,0xc5,0xd4,0x25,0x39,0x77,0xbe,0xb8};
      //unsigned char ctext[16]={0x54,0x9b,0x9e,0xd4,0x96,0xdc,0x30,0x69,0xc5,0xf7,0x25,0xd4,0x77,0x39,0xb8,0xbe};
      //unsigned char ctext[16]={0x51,0x0e,0xa4,0x12,0xa0,0x6f,0xdd,0x4e,0x4f,0x17,0x7c,0xe3,0x9d,0x0e,0x28,0xea};    
      unsigned char ctext[16]={0x2E,0x45,0x58,0x36,0xA8,0xAC,0x79,0x7E,0xC4,0xDF,0x51,0xDB,0xC4,0x88,0x28,0xC0};
  
      unsigned char key[KEY_128];
      //unsigned char key[KEY_128] ={0x0f,0x15,0x71,0xc9,0x47,0xd9,0xe8,0xf3,0x0f,0x15,0x71,0xc9,0x47,0xd9,0xe8,0xf3};
      unsigned char decptext[16];
      unsigned char test=0xFF;
  
      //gcc main.c aes.c -o decripter
      //sh decripter key encrypted --&gt; return (decrypted)
      printf("\n test %x",test);       
      for(i=0;i&lt;16;i++)
      {
          //How can I pass what I get from argv to the key unsigned char?? 
          key[i]=argv[i+1];
          printf("Key pos %d data-&gt;%s",i,key[i]);
      }  
  
      aes_ctx_t *ctx;
  
      init_aes();
      ctx = aes_alloc_ctx(key, sizeof(key));
  
      //Encrypted
      //aes_encrypt(ctx, ptext, ctext);
      //    printf("%s",ctext);
  
      aes_decrypt(ctx,ctext,decptext);
      for(i=0;i&lt;16;i++)
          printf("\n Decrypted data, position in array-&gt; %d data-&gt;  %x \n",i, decptext[i]);
  
      aes_free_ctx(ctx);
      return 0;
  }
  </code></pre>
  
  <p>This time I only tried to get the Key. As you can see, I tried also to use unsigned char *argv[] because I thought that I'd get an unsigned char but the only way to print what I get from argv is with %s, so it's a string and I can't find the way to pass it to unsigned char key[16] (128 bits = 16*8bit per HEX).
  To execute the program I do it like this:</p>
  
  <pre><code>./prueba 0x0f 0x15 0x71 0xc9 0x47 0xd9 0xe8 0xf3 0x0f 0x15 0x71 0xc9 0x47 0xd9 0xe8 0xf3
  </code></pre>
  
  <p>Any help will be really welcome.</p>
  
  <p>Thanks.</p>
  
  <p>I'm trying with your code like this and it works perfect THX ALL:</p>
  
  <pre><code> #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;stdint.h&gt;
  
  int main(int argc, unsigned char * argv[])
  {
  unsigned int aes_uint[16];
  unsigned int cipher_uint[16];
  
  int comerce;
  
  uint8_t aes_data[16];
  uint8_t ciphertext[16];
  char temp[16][3]; // For 5 hex values if you want more char temp[10][5] for 10 values
  char temp2[16][3]; // For 5 hex values if you want more char temp[10][5] for 10 values
  
  char tempcomerce[2][3]; // For 5 hex values if you want more char temp[10][5] for 10 values
  
  int i,j,k,c;
  
   for(i=0,c=0;argv[1][i];i++,c++){         // To parse the input argument to diff hex values
              for(j=i,k=0; j&lt;(i+2) ;j++,k++){
              temp[c][k]=argv[1][j];
              }
              i=j-1;
              temp[c][k] = '\0';
      }
  
      for(i=0;i&lt;c;i++) // where c stores the number hex values you have passed through argument
      {
              sscanf(temp[i],"%x",&amp;aes_uint[i]);
              aes_data[i] = (unsigned char)aes_uint[i];
              printf("AES pos %d data-&gt;%x\n",i,aes_data[i]);
      }
  
      for(i=0,c=0;argv[2][i];i++,c++){         // To parse the input argument to diff hex values
              for(j=i,k=0; j&lt;(i+2) ;j++,k++){
              temp2[c][k]=argv[2][j];
              }
              i=j-1;
              temp2[c][k] = '\0';
      }
  
      for(i=0;i&lt;c;i++) // where c stores the number hex values you have passed through argument
      {
              sscanf(temp2[i],"%x",&amp;cipher_uint[i]);
              ciphertext[i] = (unsigned char)cipher_uint[i];
              printf("Cipther pos %d data-&gt;%x\n",i,ciphertext[i]);
      }
  
      sscanf(argv[3],"%d",&amp;comerce);
      printf("Comerce--&gt;%d\n",comerce );
  
  
  
      // do your stuff        
  
  
  return 0;
  }
  </code></pre>
  
  <p>I introduce:</p>
  
  <pre><code>./prueba 52871D6B9D600046D721C97990EF42FA 52871D6B9D600046D720D47390EE42BC 56748
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>    AES pos 0 data-&gt;52
  AES pos 1 data-&gt;87
  AES pos 2 data-&gt;1d
  AES pos 3 data-&gt;6b
  AES pos 4 data-&gt;9d
  AES pos 5 data-&gt;60
  AES pos 6 data-&gt;0
  AES pos 7 data-&gt;46
  AES pos 8 data-&gt;d7
  AES pos 9 data-&gt;21
  AES pos 10 data-&gt;c9
  AES pos 11 data-&gt;79
  AES pos 12 data-&gt;90
  AES pos 13 data-&gt;ef
  AES pos 14 data-&gt;42
  AES pos 15 data-&gt;fa
  Cipther pos 0 data-&gt;52
  Cipther pos 1 data-&gt;87
  Cipther pos 2 data-&gt;1d
  Cipther pos 3 data-&gt;6b
  Cipther pos 4 data-&gt;9d
  Cipther pos 5 data-&gt;60
  Cipther pos 6 data-&gt;0
  Cipther pos 7 data-&gt;46
  Cipther pos 8 data-&gt;d7
  Cipther pos 9 data-&gt;20
  Cipther pos 10 data-&gt;d4
  Cipther pos 11 data-&gt;73
  Cipther pos 12 data-&gt;90
  Cipther pos 13 data-&gt;ee
  Cipther pos 14 data-&gt;42
  Cipther pos 15 data-&gt;bc
  Comerce--&gt;56748
  </code></pre>
  
  <p>Iván Gómez</p>