Patrick Vogt Patrick Vogt - 6 days ago 6
Java Question

Compress string to fix length with java

How could I reduce a string lossless to a fix length of 19 characters?

My strings look like the following: MEUCIQCE72XHhVwjnTUc2YAlV0FaCnMIAcDBDXzmyXlQh1hskgIgehf6X74JUajs77qcZrREjErYKMS08kQr9ijpSVqwqOk=

Is any algorithm or method in java existing to solve my problem?

Thank you in advance for your help!

EDIT:
I have some data. For example: String1, String2, String3 and String4.
Now I have to concatenate these four strings and sign it with DSA. I would like to display the signature but it must not longer then 19 characters! Is this possible?

Answer

The anser to your question is simply "you can't".

You haven't specified the length of the signature, but if it's 320 bits that's 40 bytes.

It is in the nature of encryption and signing that the output looks completely random. That's why the bytes that represent the signature data are very unlikely to be losslessly compressible at all.

That said, you probably already figured out that Base64 is not bad in making binary data printable (up to 75% length-efficient). Normally, signatures are displayed as hexadecimal (50% length-efficient). This however shows that you'd need even more than the 40 bytes binary signature length to display something legible.

So, there's no way to accomplish what you asked for.

However, you may not actually need it. If you only need a way to display any representative value to verify the integrity and authenticity of the message, and are not bound by any standard algorithm, you could just take the signature and feed it to a secure hash function (SHA-256 or whatever). The output of the hash function can then be used instead of the signature's value with the added benefit that you may simply truncate the hash to any desired length without breaking its security; of course, the security will degrade with shorter length however.

In 19 hex digits you can represent 76 bits, 114 bits fit into 19 Base64 digits. So if you can live with a "signature" of only 114 bits that may be a way.