-3

I need to get a shorter characters using my encrypted link. This is is the encoded characters Im currently getting using my code: data=xvXTyQe6cthuC0GdQcOvCR5PKSlFiMqLBt7tM0zbxHs%3D. I want to achieve shorter characters than this.

What I have done:

Given below is related part of my code:

private Map<String, Object> getEUGenericDataMapFields(Map<String, Object> data , Document doc) {

String imeiOrSerial = org.apache.commons.lang3.StringUtils.isNotBlank(doc.getIMEINumber()) ? doc.getIMEINumber() : doc.getDeviceSerialNo();
String link = imeiOrSerial + PolicyConstants.REGEX_PIPE + PolicyConstants.RENEWAL_SMS;

    public static String encrypt(String strToEncrypt, String secret) {
        try {
            Key secretKey = generateKey(secret);
            Cipher cipher = Cipher.getInstance(PolicyConstants.AES);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
        } catch (Exception e) {
            log.error("Error while encrypting: " + e);
        }
        return null;
    }

link = EncryptionUtil.encrypt(link, Config.getSecretKey());
    data.put(PolicyConstants.DATA_LINK, URLEncoder.encode(link, StandardCharsets.UTF_8.toString()));
return data;
}

Result:

data=xvXTyQe6cthuC0GdQcOvCR5PKSlFiMqLBt7tM0zbxHs%3D (almost 50 characters)

Expected result: less than 5 characters if possible.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
Sham
  • 1
  • 5
    Please let me know if you find an encryption algorithm that is both secure and *also* a great compression algorithm. Or, you know, keep it to yourself and become immensely rich. – Jorn Aug 10 '23 at 11:28
  • 1
    Now, if you're looking for a *hash* instead of encryption, you can make those as short as you like. The shorter it is, the higher the chance of collisions. And of course unlike encryption, hashes are not reversible (you cannot obtain the original data). – Jorn Aug 10 '23 at 11:35
  • I’m afraid that less than five characters isn’t possible – Tim Moore Aug 10 '23 at 12:20
  • Are you trying to encrypt ot to compress? Those are two different things. – aled Aug 10 '23 at 16:12

1 Answers1

0

There are certainly ways to encode fields to a minimum amount of bits, especially if the values in the data are not randomized. Compression doesn't work that well for small data sizes as the overhead (to indicate the compression mechanism and such) will likely negate any result.

The best you can do using encryption is to break even, see Format Preserving Encryption (FPE) for that. The resulting ciphertext is fully randomized and cannot be compressed, so the compression must be performed beforehand.

After FPE is performed on the binary data then you can perform base conversion to encode the value (seen as large integer) into an index, and finally in characters.


However, your data consists of large identifiers, so the chance that you can compress those to 5 characters or less is unlikely to the extreme, even if you'd use all possible printable Unicode characters for the ciphertext.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263