开发者

How to create the $key value for mcrypt_decrypt?

开发者 https://www.devze.com 2023-01-07 07:47 出处:网络
I\'m trying to decrypt a string using mcrypt_decrypt, but I\'m not sure how to get the key into a \'string\' type:

I'm trying to decrypt a string using mcrypt_decrypt, but I'm not sure how to get the key into a 'string' type:

$key = array(-2, -2, -2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2);
pkcs5_unpad(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv));

the key w开发者_如何学运维as generated using a random key generator, I used it on the encryption side (which happens to be java):

byte[] key = new byte[] { -2, -2, -2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };

bytes are signed, so having negative numbers should be legal - I'm just not sure how to use this with mcrypt_decrypt since it wants a 'string' type for $key?

Thanks


You can use chr() to convert the byte values in ASCII characters:

$strkey = "";

foreach($key as $char) {
    $strkey .= chr($char);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消