1

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

0TJyj0wX4ZCrsYlANG8QgwBHnGzGU7kr1BEkolr6tY0OdowpA==

Better: 73a4842f36b4n237m64as23
hakre
  • 193,403
  • 52
  • 435
  • 836
mate64
  • 9,876
  • 17
  • 64
  • 96
  • By nature, a hash is one direction. You're looking for a transformation (or potentially encryption.) – Corbin Mar 21 '12 at 20:54
  • If you want to decode it we are probably talking about encryption. How secure do you want it to be? – Tchoupi Mar 21 '12 at 21:00
  • You could just encode/decode and compare the first (or last) bytes returned by `crypt`, but you would get less secure with each byte you remove...otherwise you can only go with Justin's answer I think. – Maarten Bodewes Mar 21 '12 at 22:08
  • I have a suspicion that the problem you are trying to solve might be approached in a different way than trying to compress encrypted data. I'd suggest posting a new question with some more detail about what you're trying to achieve. If you do so, link to it here in the comments. – Justin ᚅᚔᚈᚄᚒᚔ Mar 22 '12 at 15:13

3 Answers3

2

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
Community
  • 1
  • 1
Justin ᚅᚔᚈᚄᚒᚔ
  • 15,081
  • 7
  • 52
  • 64
  • 1
    Lossless compression won't work too well on hashed or encrypted data either, the data will look like random data to the compression algorithm. It's worse than trying to zip a JPEG or MP3 file. – Maarten Bodewes Mar 21 '12 at 22:03
  • @owlstead: +1 True, I should have noted that in the answer... edit time! – Justin ᚅᚔᚈᚄᚒᚔ Mar 22 '12 at 14:52
  • Because msec doesn't tell where he need it -> He could encode it binary – mabe.berlin Apr 05 '12 at 15:06
  • @mabe: Technically speaking, the data *is* in binary -- the string is just a representation of that binary data. The encoding is what specifies how that binary data is represented: hexadecimal (base16), base64, base85. The phrase "encode in binary" doesn't make a lot of sense, unless I'm misunderstanding your meaning. – Justin ᚅᚔᚈᚄᚒᚔ Apr 05 '12 at 15:29
0

you can use RSA to encrypt you data see this, it talks about to crypt in php

Jayyrus
  • 12,961
  • 41
  • 132
  • 214
0

Best way to encrypt in PHP is by using the Mcrypt extension. http://www.php.net/manual/en/book.mcrypt.php

Example for encryption and decryption.

I do recommend also to base64 encode the result of encryption.

Minification does not make sense for small data (ex. passwords).

Dragos
  • 138
  • 4
  • I don't think the generic encryption/decryption samples are useful. I would +1 if you would have just posted the last sentence. – Maarten Bodewes Mar 21 '12 at 22:05