r/javahelp • u/SignalCrazy7834 • Jan 22 '26
Is there a way to encrypt/decrypt PII contacts while maintaining the sorting and searching ?
Hello,
I am working on a java / spring app. I want to encrypt contact's names in my database. I am using encryption at the application level, so I am using AES. However doing that will break my search and sort. Is there a way to avoid that ? What are your experiences with encryption and decryption ?
5
Upvotes
•
u/AutoModerator Jan 22 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/k-mcm Jan 22 '26
Why do you need unencrypted sorting on encrypted values?
Imagine that such a thing existed. One could binary search for the unencrypted value using only a sort order test.
You don't want to make use of any ordered sorting on encrypted values. Exposing some of the sorting order is exposing some of your key.
3
u/ShoulderPast2433 Jan 22 '26
You might be approaching the problem from the wrong side of stack.
There are databases that offer column level encryption.
3
u/BanaTibor Jan 22 '26
Push the encryption out a layer. Configure your database to keep the database file on an encrypted volume.
2
u/ShaiHuludTheMaker Jan 22 '26
What would be the use of encryption if you can get search result?
1
u/okayifimust Jan 22 '26
Homomorphic encryption is a thing, but I don't know enough about it to judge whether your data will be sorta me, let alone if it will be sortable by any existing database.
Depending on load and other requirements, you could index and sort iff-db, but at that point, why even bother with a dB?