Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top