I want to take a long alphanumeric string, toss it into something like MD5 and have it spit out 32 characters (more or less).
However, I want to be able to convert it back to the original...
Is this possible? :nuts:
Not unless your original string has 32 or less characters
Encoding can be done in several ways. The easiest is a simple Caesar Cipher. With one of these, each letter is advanced by a certain number of characters. At the end of the list, it loops back to the start.
So if a becomes c, c becomes e and so on. z becomes b.
So the message "Hello, world" would become "Jgnnq yqtnf".
However, these are pretty easy to figure out. Famous systems such as the Enigma machine are based on this simple concept (although it does it multiple times and has the effect that it maps each character to some seemingly random character.
To implement this, I would get an ord value for each character in a string, map it to something else (that nothing else is mapped to!!!) and convert it back into ASCII.
Or there are built in constructs in most languages. In PHP, you can use base64_encode. However, it's pretty easy to decode.
The most ideal solution is to use a custom made base conversion. Depending on the medium you intend to distribute the encoding string, more bits is better. Add in characters such as <>?
~;'#[]{} etc on top of the base64 characters.
In short, there is
no way of making an encoded string shorter than the original unless a different character set encoding is used. And then, the encoded set must contain more characters.
If you need to send people secret messages or anything like that, you could always store messages in a database on a server, giving out only a message number and a password...