Search code examples
phpencryptionopensslmcrypt

Encrypt Integers in PHP


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?


Solution

  • 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.