0

I have a normal string :

var string = "Hello i'm fine"

I want to encode it in a short encoded string.

And i want to retrieve the original long string by decoding the short string.

Base64 don't suit me because the base64 output length is larger than the original string length.

So i look for a way to do that in javascript and in php.

An idea ?

totoaussi
  • 712
  • 1
  • 11
  • 27
  • what do you define a _"long string"_ and a _"short string"_? – CodeGodie Mar 28 '16 at 15:34
  • I make a mistake. I want to mean "normal string" to a "short string". "short string" is an encoded string. The length of the encoded string must be shorter than the normal string. – totoaussi Mar 28 '16 at 15:38
  • 1
    i googled and this came up: http://stackoverflow.com/questions/11076221/php-encrypt-decrypt-short-string have you tried this yet? – CodeGodie Mar 28 '16 at 15:39
  • http://stackoverflow.com/questions/959957/php-short-hash-like-url-shortening-websites – CodeGodie Mar 28 '16 at 15:41
  • base64 don't suit me because the base64 output is longer than the normal string. – totoaussi Mar 28 '16 at 15:54
  • As per pigeonhole principle it is not possible . I google it and found this http://programmers.stackexchange.com/questions/119181/what-type-of-encoding-can-i-use-to-make-a-string-shorter . Hope this helps. – paraS elixiR Mar 28 '16 at 16:15

1 Answers1

1

For your example it's not so easy to short the string (see below). But if your input is a longer string, you can use a compress function in combination with base64 encoding:

<?php
$input = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt.";

$encoded = base64_encode(gzdeflate($input));
$decoded = gzinflate(base64_decode($encoded));
var_dump($encoded);
var_dump($decoded);

The input has 489 characters, the encoded just 372!

string(372) "PVHZTcQwEG1lCojSA4IfJEQPxh7CIF87x9bPc7LLhyUf7xx/DOVGMi0alVGHkolTauwb5dGNs7MHK6UiUyxLP4ir+E4v3Dl1gFobZVCVI2oiPtgvoX9ES2Zpp1c42MgiRj35uAXTZOzkK4xAaunoeCtYM6mHCnfcju5sGyiWEURJpUiOCk4L2+ltdM50i9ToG7mAjOoqWRg2nDd41MpLYxly4ELZBVlusdDGbafPqIh+1oWSX4lPAHGX9nSZXJh+w3xs9K2YhCzanetGqQr0/bK8R53hyfmcBh41x07v/RR7CujP6BktAgBpk7UI+AlsTG2Nxegunni7CI+Q+CTJHo+y6HOFaqOu46Pa8nJYK7n0LCU6vusP"
string(489) "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt."

For shorter strings it is maybe possible by choosing character sets smartly. A short input character set and a large output character set can reduce the length of a string. Read this: Shortest possible encoded string with decode possibility (shorten url) using only PHP

In Javascript the concept is the same, but maybe you need a library for compression.

Please remember: This is no encryption only encoding! Do not use this for hiding information

Community
  • 1
  • 1
Timo
  • 106
  • 1
  • 4