Search code examples
phpencryptioncrypt

PHP: Minify crypted value


Is it possible, to transform a crypted string (similar to md5), without loosing the ability of decoding it back again?

0TJyj0wX4ZCrsYlANG8QgwBHnGzGU7kr1BEkolr6tY0OdowpA==

Better: 73a4842f36b4n237m64as23

Solution

  • A hash is, by definition, one-way. You would not be able to hash your crypted string and then "un-hash" it to obtain the crypted string again.

    You can, however, attempt to "minify" the crypted string by:

    • Using a lossless compression algorithm -- though you're unlikely to gain much with this approach since encrypted data doesn't compress well, and for certain input sets compression can actually increase the storage requirements.
    • Compress the data before encrypting it. Depending on the size of input, it's possible to reduce the size by a few percent over encryption alone (depending on the algorithms), but typically not by more than 5-10% (again, this answer points this out)
    • Re-encoding the data with a higher "base" (i.e. your string is Base64, you could switch to Base85) -- but this only results in a 7% savings