0

My back end in Spring after a transaction must send a link to a user with some metadata encrypted and put in url in order to open directly the app when the user click on it.

My code is this:

    String id = "5d2583a5-2f0e-4660-95fe-7775e9660c9a";
    Map<String, String> data = new HashMap<>();
    data.put("id", id);
    data.put("status", "SUCCESSFUL");
    data.put("type", "apk");
    
    //convert to json string
    String jsonString = new Gson().toJson(data);
    
    System.out.println(jsonString);
    AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
    textEncryptor.setPassword(id);
    
    String myEncryptedText = textEncryptor.encrypt(jsonString);
    System.out.println(myEncryptedText);
    
    //we try to short the data and send it => impossible to revert the myEncryptedText => we want to change this part
    String sha256hex = Hashing.sha1()
              .hashString(myEncryptedText, StandardCharsets.UTF_8)
              .toString();
    
    Assert.assertEquals("we should have ", 40, sha256hex.length());
    System.out.println(sha256hex);

The first encrypted value with myEncryptedText returns a long string eX8H14Gg1detgGb97ZWF0cIHz7LTxKeLynuZC1Dok7Zl5jTeLMcEulFroD0MZ2xVyc87ETORtoKGmuLl2AwdPKp90DTWbV9uoQJZkBIWlvOb5O/9M+WZg83wP5KjqtRh1AKLAgSoWWG2IzwBkWE4QV4/bUmTlDmXLzYD8z6/Txg= b86e21d1117ea756b5c8b4da3fc0feb61b6d68fa

I tried to use sha1, to make it shorter but we notice that it is impossible to revert it in the 2nd app, so we are looking for another solution to short to max 50 characters the myEncryptedText, and get the full character on the client side.

How could we do it easily ? Thanks

Teddy Kossoko
  • 1,168
  • 3
  • 20
  • 48
  • You consider *that* a long string? By the way, you could use a compression algorithm, but compressing short strings [is not really easy](https://stackoverflow.com/a/3649538/133203) – Federico klez Culloca May 30 '22 at 08:03
  • @FedericoklezCulloca it is a long string because we will have finally something like this sens by sms so it has cost `https://myapp.com/transaction/eX8H14Gg1detgGb97ZWF0cIHz7LTxKeLynuZC1Dok7Zl5jTeLMcEulFroD0MZ2xVyc87ETORtoKGmuLl2AwdPKp90DTWbV9uoQJZkBIWlvOb5O/9M+WZg83wP5KjqtRh1AKLAgSoWWG2IzwBkWE4QV4/bUmTlDmXLzYD8z6/Txg= b86e21d1117ea756b5c8b4da3fc0feb61b6d68fa` – Teddy Kossoko May 30 '22 at 08:16
  • 3
    Another possibility would be to save the long string and the checksum inside a table on a db, send the url with the checksum to the user and have the webapp load the data corresponding to that checksum from the db – Federico klez Culloca May 30 '22 at 08:30
  • 1
    Check if this helps: https://medium.com/geekculture/create-your-own-url-shorteners-with-spring-boot-redis-289c9000f747. It uses Redis but you can adapt to use other mechanism. – pringi May 31 '22 at 09:09

0 Answers0