4

I'm using this code:

$url = "http://www.webtoolkit.info/javascript-base64.html";
print base64_encode($url);

But the result is very long: "aHR0cDovL3d3dy53ZWJ0b29sa2l0LmluZm8vamF2YXNjcmlwdC1iYXNlNjQuaHRtbA=="

There is a way to transform long string to short encryption and to be able to transform?

for example:

new_encrypt("http://www.webtoolkit.info/javascript-base64.html")
Result: "431ASDFafk2"
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
user1123379
  • 334
  • 3
  • 5
  • 13
  • 2
    Does the result need to be unique? Do you need reversibility? Can you keep a table with the mapping? Is the transformation secret/keyed? – CodesInChaos Feb 27 '12 at 13:12
  • 2
    That doesn't encrypt the data at all. It just lets you represent binary data in ASCII (which is pointless here as you have ASCII to start with). – Quentin Feb 27 '12 at 13:12
  • 1
    What are you trying to achieve? Are you trying to make data safe for transport? (What data? What transport?) or are you trying to keep data secret from someone (who? The visitor? A third party?) – Quentin Feb 27 '12 at 13:13

6 Answers6

4

encoding is not encrypting. If you're depending on this for security then you're in for a very nasty shock in the future.

Base 64 encoding is intended for converting data that's 8 bits wide into a format that can be sent over a communications channel that uses 6 or 7 bits without loss of data. As 6 bits is less than 8 bits the encoded string is obviously going to be longer than the original.

GordonM
  • 31,179
  • 15
  • 87
  • 129
2

This q/a might have what you're looking for: An efficient compression algorithm for short text strings

It actually links here: http://github.com/antirez/smaz/tree/master

I did not test it, just found the links.

Community
  • 1
  • 1
TecBrat
  • 3,643
  • 3
  • 28
  • 45
0

You can be creative and just do some 'stuff' to encrypt the url so that it is not easy quess able but encode / decode able..

like reverse strings...
or have a random 3 letters, your string encoded with base64 or just replace letters for numbers or numbers for letters and then 3 more random letters.. once you know the recipe, you can do and undo it.

    $keychars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $length = 2;
    $randkey = "";
    $randkey2 = "";
    for ($i=0;$i<$length;$i++)  $randkey .= substr($keychars, rand(1, strlen($keychars) ), 1);
gabrielkolbe
  • 145
  • 9
0

First off, base64 is an encoding standard and it is not meant to encrypt data, so don't use that. The reason your data is so much longer is that for every 6 bits in the input string, base64 will output 8 bits.

There is no form of encryption that will directly output a shortened string. The result will be just as long in the best case.

A solution to that problem would be to gzip your string and then encrypt it, but with your URL the added data for the zip format will still end up making your output longer than the input.

Overv
  • 8,433
  • 2
  • 40
  • 70
0

There are a many different algorithms for encrypting/decryption. You can take a look at the following documentation: http://www.php.net/manual/en/function.mcrypt-list-algorithms.php (this uses mcrypt with different algorithms).

...BUT, you can't force something to be really small (depends on the size you want). The encrypted string needs to have all the information available to be able to decrypt it. Anyways, a base64-string is not that long (compared with really secure salted hashes for example).

I don't see the problem.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
0

Well... you could try using md5() or uniqid().

The first one generate the md5 hash of your string.

md5("http://www.webtoolkit.info/javascript-base64.html");

http://php.net/manual/en/function.md5.php

The second one generates a 13 unique id and then you can create a relation between your string and that id.

http://php.net/manual/en/function.uniqid.php

P.S. I'm not sure of what you want to achieve but these solutions will probably satisfy you.

siannone
  • 6,617
  • 15
  • 59
  • 89
  • Well, so the uniqid solution will be ok if you want a short result. But doing this way you will also need to create a relationship between your url and the uniqid result and store this relationship somewhere (e.g. DB). – siannone Feb 27 '12 at 13:23