开发者

Encrypt Integers in PHP

开发者 https://www.devze.com 2023-04-02 04:45 出处:网络
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

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().)

0

精彩评论

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

关注公众号