Search code examples
phppack

variable with pack function ("c*",


i have a script who call a key in file and i want integrate it in pack function with variable but doesn't work

=> keyfile.txt

100,102,52,57,49,51,49,51,48,49,101,102,97,99,49,52,57,56,102,57,53,99,99,49,98,97,57,98,51,48,99,99

to integrated in php file

  $open = fopen("php\key\keyfile.txt", "r");
  $clef=fgets($open,255);
  $td  = mcrypt_module_open('rijndael-128', '', 'ecb', '');
  $iv  = str_repeat("\x00", 16);

  $key = pack("C*",$clef); ## **not working**

  $key = $key . str_repeat("\x00", 32 - strlen($key));
  mcrypt_generic_init($td, $key, $iv);
  $key = mcrypt_generic($td, $key);
  $key = str_repeat(substr($key, 0, 16), 2);
  mcrypt_generic_deinit($td);

i don't know why please help


Solution

  • PHP thinks like this that way:

    $key = pack("C*", "100,102...");
    

    Instead of this:

    $key = pack("C*", 100,102,...);
    

    You can use call_user_func_array() to get what you want:

    $chr = "C*";
    $int = explode(',', $clef);
    
    $key = call_user_func_array('pack', array_merge(array($chr), $int));