I've noticed that both openssl and mcrypt functions in PHP accept data to be encrypted 开发者_如何学运维as strings only. This means a value of (int)1234567890 is converted and encrypted as a 10-byte string when it was originally just a 4-byte int. Are there any encryption methods (bundled with PHP or as an external function/class) that allows for the encryption of integers?
No. If you want to encrypt your integer as the 4-byte binary representation, then you have to preconvert it:
$bin = pack("L", $int); // unsigned 4-byte integer
xyz_crypt(...)
And unpack
this again after decryption.
mcrypt operates on strings, not integers, so you'll need to use pack()
to, well, pack an integer into a 4-byte string. (The inverse function is, sensibly enough, unpack()
.)
精彩评论