views:

169

answers:

3

I am looking to encode some text that could be 1 charchter long or or 10,000 or infinite and decode (reverse the algorithm).

I am looking something like MD5 on PHP, but reversable, as MD5 is one way.

This could be server side or JavaScript. If both, then it's even better.

+1  A: 

For compression

In Javascript, http://rumkin.com/tools/compression/compress_huff.php

Also have a look at http://stackoverflow.com/questions/994667/javascript-text-compression-decompression

For encryption

In PHP, you can use mcrypt:

http://www.php.net/manual/en/function.mcrypt-encrypt.php

http://www.php.net/manual/en/function.mcrypt-decrypt.php

Sample code (from above site):

<?php
class Cipher {
    private $securekey, $iv;
    function __construct($textkey) {
        $this->securekey = hash('sha256',$textkey,TRUE);
        $this->iv = mcrypt_create_iv(32);
    }
    function encrypt($input) {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB, $this->iv));
    }
    function decrypt($input) {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv));
    }
}

$cipher = new Cipher('secret passphrase');

$encryptedtext = $cipher->encrypt("hide me");
echo "->encrypt = $encryptedtext<br />";

$decryptedtext = $cipher->decrypt($encryptedtext);
echo "->decrypt = $decryptedtext<br />";

var_dump($cipher);
?> 
Alec Smart
The OP clarified that s?he doesn't want encryption, but compression.
Joey
Oh. Question has changed completely.
Alec Smart
updated answer...
Alec Smart
sorry mate but this compression is rather silly if there is 3050 characters on the string to be encrypted, you end up with 5000+ characters therefore it is not making it smaller in size... infact it makes it larger.
Val
A: 

There are implementations of rot13 for PHP and JS, as well as URL encoding (PHP, JS).

David Dorward
but isn't rot13 and the like, obsolete
Vivek Bernard
Obsolete? No. They do perfectly well for the things they have been used for for the past century.
David Dorward
+1  A: 

If you want to compress string, see this question for info on JavaScript implementation of LZW, Huffman, LZ77 and others. I'm pretty sure that there are similar libraries in PHP.

Anton Gogolev