PHP 2 way string encryption with character limit

cimmer used Ask the Experts™
I have a string that may vary in length somewhere between 20 and 80 characters and will only contain a-z, dashes, periods and 0-9


Its basically the productid-itemtype-domainname
Most of these come well under 50 characters in total length but a domain name can be ordered with up to 63 characters (plus the .com) in length alone.

This example string is 54 characters long. The problem is that I need to store this as 50 characters or less and I need to be able to reverse the encoding or encryption so I can read it without knowing what it is or entering it initially to be hashed.

Basically I need something like like what the php function base64_encode and base64_decode does but with a resulting character length limit of 50...

Is this possible?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Hello cimmer.

I think that you can solve your problem using a PHP implementation of the Huffman Compression
Algorithm. It  will reduce the size of texts by about 43%. =P

So a 80 characters string will be converted in a 44.31 (Ok... 45) Characters string in most of the cases.

Here a couple of Good and Ready to Use implementations of the Huffman compression

In the first page you will find a String compression/decompression Example.

And here a little bit info about Huffman Compression Algorithm:

Remember: If my answer solve your problem give me the points, BUT say thanks to Mr. Huffman:  ;)



huffman seemed promising but my test using the script from seems to be actually increasing the string length about 43% ... any ideas why?  does it only work on large strings?

I tried it on a few different string from 8 in length up to 80 and they all got larger.

Most Valuable Expert 2011
Top Expert 2016

Can I ask why you need to make a long character string into a 50 character string?  There may be a better way to skin this cat.  

Where will the encode / decode algorithm run?

With a-z, 0-9 and two punctuations you get 38 unique characters.  There is no case-sensitivity (all lower case) and no blank to be used, right?

Thanks and regards, ~Ray
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Most Valuable Expert 2011
Top Expert 2016
Since you need more than 32 characters in your "alphabet" your next increment is 64 characters - this fits in 6 bits.  Since each byte is 8 bits long, you can, in theory, get 4 of the 6-bit representations into 3 of the 8-bit bytes.  This means that you would be able to shrink 80 bytes of clear text into 60 bytes of binary compressed representation. That means your long strings fail the 50-byte test.  In addition to that issue you have the risk that the compression-generated "characters" would contain signal codes that might play havoc with your ability to transmit the data over HTTP.  The PHP function base64_encode is used to make such data safe for internet transmission, but it increases the length of the data, requiring about 33% more space than the original data.

Anyway, those are just some of my thoughts on this question.  Hope you can find a way to transmit and store longer strings.  If your string length changes to something that is realistic and you want an encryption algorithm to obscure the data during transmission, please post a new question and I will be glad to help you with that.  

Best regards, ~Ray


Right... a-z, 0-9 and two punctuations = 38 unique characters.  There is no case-sensitivity (all lower case) and no blank to be used...

well I basically make my own Order Item ID which is sent to an API to place an order for a domain name. The API limits the length of this ID at 50 and then returns whether order was successful or not. This is the only way I can pass what it is I actually ordered so that I can later read the items on the orders from their system. The 50 limit is set in stone on the API. This problem only occurs when a long domain name is ordered which is like 1 out of 100 orders.

Their documentation didnt mention this limit in it so now Im trying to work with the system in place by just compressing the id when passed and uncompressing when read.


i think im just gonna cut the id off at 50... then add some code after the order is successful to look up the full domain name. Extra work for no reason.. =(    Thanks for the info though. Good Stuff.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial