views:

174

answers:

5

Hi,

I want to compress/transform a string as new string.

i.e.: input string:

USERNAME/REGISTERID

output string after compress:

<some-string-in-UTF8-format>

output string after decompress:

USERNAME/REGISTERID

There are some compress or hash method for this transformation?

I prefer some solution using Java or an algorithm with basic process steps.

I already read and try to use Huffman transformation, but the compressed output are composed by bytes outbound UTF-8 charset.

Thank,

And Past

+1  A: 

You could use ZipOutputStream.

 ByteArrayOutputStream result = new ByteArrayOutputStream();
 new ZipOutputStream(result).write("myString".getBytes());
 byte[] bytes = result.toByteArray();

You just have to figure out the right string encoding. This case be done with a Base64 representation.

Thomas Jung
A: 

if you have database ids for your identifiers as your names suggests, why not using this number as encoding ? (put it as string if you like).

You shouldn't hope to get better compression using compression algorithms as they all need some headers and the header size by itself is probably longer than your input string.

kriss
I haven't a database for keys. The compression are not for data transfer reduction, but a simple and naive obfuscation of original data.Thank,And Past
apast
+1  A: 

See iconv and mb_convert_encoding. For encoding, maybe consider base64_encode.

karim79
+1  A: 

Take a look at Base64, commons-codec, etc.

Commons-code provides a very simple Base64 class to use.

You can't use a hash function as hashing functions are typically meant to be one-way only: i.e. given a MD5 or SHA1 hash, you should not be able to decode it to find out what the source message was.

matt b
Really, hash functions are one-way.Thanks!And Past
apast
@matt: it is possible to use a hashing function for that purpose even if it is a one way function, you just have to keep in some persistant structure (database, file, whatsoever) server side the backward correspondance string used in key to user/registerid... ok, if you do it that way it could even be randomly picked numbers or strings.
kriss
A: 

It looks like someone is asking you to obfuscate username/password combinations. This is probably not a good idea, since it suggests security where there is none. You might as well implement a ROT13 encryption for this and use double ROT13 to decrypt.

Adriaan Koster