简体   繁体   中英

Reversible string compression PHP/C++

I would like to obfuscate some short text data, and make this compression learnable/memorizable.

So I'm looking for an algorithm achievable in PHP to compress a string (~25 characters long) into a ~8 character string, then reversible in C++.

Does anyone have an algo name or another idea ?

EDIT : Everything is lowcase, with two specials characters.

As the text data only consists of lowercase letters and two special characters, so we have only 28 different characters to consider.

We can design the hash function by property of bit representation. With all possible combination of 5 bits, we can uniquely represent 32 different symbols. So, to represent 28 different symbols, we only need 5 bits for each symbol.

a => 00000
b => 00001
c => 00010
......
......
......
y => 11000
z => 11001
special-character-1 => 11010
special-character-2 => 11011

With this encoding scheme, we only need 25 * 5 = 125 bits to represent the complete text data, which is 125 / 8 ~ 16 bytes or 16 characters (sorry its not 8 characters).

Now, you can retrieve the actual string from this 16 characters hash by applying the reverse mapping.

If you're satisfied with 16 characters reversible hashing, I can provide C++ implementation.

Impossible.

If we assume that the original strings only contains letters AZ, there are 26 25 ≈ 4.25 x 10 37 (42 billion billion billion billion) possible input strings.

If we then generously allow the eight-character outputs to contain any letter, uppercase or lowercase, or digit (26 + 26 + 10 = 62 characters total), there are 62 8 ≈ 2.18 x 10 14 (218 million billion) possible outputs.

This is roughly 10 23 times fewer! By the pigeonhole principle , the compression scheme you're asking for is impossible -- there are many times more possible input strings than outputs, so there's no way to reversibly turn every one of the input strings into an output and back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM