mcrypt blowfish php slightly different results when compared to java and .net

后端 未结 1 911
生来不讨喜
生来不讨喜 2021-02-10 09:26

Here is some example code with altered key values and payload:

$key = \'/4rTInjwg/H/nA==\';
$key = base64_decode($key);

$data = \'val=100|val=200|val=300|val=40         


        
1条回答
  •  故里飘歌
    2021-02-10 09:58

    Figured it out, simple pkcs5 padding fixed the issue.

    ... snip  ...
    
    $data = 'val=100|val=200|val=300|val=400|val=500|val=600|val=700|val=800|val=900|';
    $data.= 'val2=100|val2=200|val2=300|val2=400|val2=500|val2=600|val2=700|val2=800|val2=900|';
    $data.= 'val3=100|val3=200|val3=300|val3=400|val3=500|val3=600|val3=700|val3=800|val3=900|';
    $data.= 'val4=100|val4=200|val4=300|val4=400|val4=500|val4=600|val4=700|val4=800|val4=900|';
    
    $blocksize = mcrypt_get_block_size('blowfish', 'ecb'); // get block size
    $pkcs = $blocksize - (strlen($data) % $blocksize); // get pkcs5 pad length
    $data.= str_repeat(chr($pkcs), $pkcs); // append pkcs5 padding to the data
    
    // encrypt and encode
    $res = base64_encode(mcrypt_ecb(MCRYPT_BLOWFISH,$key, $data, MCRYPT_ENCRYPT));
    

    0 讨论(0)
提交回复
热议问题